def __init__(self): self.dataDir = Settings.dataDir + 'programming_documents/' # Create a blank document. doc = Document() builder = DocumentBuilder(doc) # The number of pages the document should have. numPages = 4 # The document starts with one section, insert the barcode into this existing section. self.insert_barcode_into_footer(builder, doc.getFirstSection(), 1, HeaderFooterType.FOOTER_PRIMARY) i = 1 while (i < numPages) : # Clone the first section and add it into the end of the document. cloneSection = doc.getFirstSection().deepClone(False) cloneSection.getPageSetup().setSectionStart(SectionStart.NEW_PAGE) doc.appendChild(cloneSection) # Insert the barcode and other information into the footer of the section. self.insert_barcode_into_footer(builder, cloneSection, i, HeaderFooterType.FOOTER_PRIMARY) i = i + 1 # Save the document as a PDF to disk. You can also save this directly to a stream. doc.save(self.dataDir + "InsertBarcodeOnEachPage.docx") print "Aspose Barcode Inserted..."
def join_continuous(self): """ Shows how to append a document to another document so the content flows continuously. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Make the document appear straight after the destination documents content. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS) # Append the source document using the original styles found in the source document. dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.JoinContinuous Out.doc")
def join_new_page(self): """ Shows how to append a document to another document so it starts on a new page. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Set the appended document to start on a new page. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE) # Append the source document using the original styles found in the source document. dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.JoinNewPage Out.doc")
def restart_page_numbering(self): """ Shows how to append a document to another document with page numbering restarted. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Set the appended document to appear on the next page. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE) # Restart the page numbering for the document to be appended. srcDoc.getFirstSection().getPageSetup().setRestartPageNumbering(1) dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.RestartPageNumbering Out.doc")
def unlink_headers_footers(self): """ Shows how to append a document to another document so headers and footers do not continue from the destination document. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Even a document with no headers or footers can still have the LinkToPrevious setting set to True. # Unlink the headers and footers in the source document to stop this from continuing the headers and footers # from the destination document. srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(0) dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.UnlinkHeadersFooters Out.doc")
def generate_document(self,srcDoc,nodes): # Create a blank document. dstDoc = Document() # Remove the first paragraph from the empty document. dstDoc.getFirstSection().getBody().removeAllChildren() # Import each node from the list into the new document. Keep the original formatting of the node. importer = NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) for node in nodes: importNode = importer.importNode(node, True) dstDoc.getFirstSection().getBody().appendChild(importNode) # Return the generated document. return dstDoc
def link_headers_footers(self): """ Shows how to append a document to another document and continue headers and footers from the destination document. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Set the appended document to appear on a new page. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE) # Link the headers and footers in the source document to the previous section. # This will override any headers or footers already found in the source document. srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(1) dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.LinkHeadersFooters Out.doc")
def remove_source_headers_footers(self): """ Shows how to remove headers and footers from a document before appending it to another document. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Remove the headers and footers from each of the sections in the source document. for section in srcDoc.getSections().toArray(): section.clearHeadersFooters() # Even after the headers and footers are cleared from the source document, the "LinkToPrevious" setting # for HeadersFooters can still be set. This will cause the headers and footers to continue from the destination # document. This should set to false to avoid this behaviour. srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(0) dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.RemoveSourceHeadersFooters Out.doc")
def __init__(self): # Create a new document. doc = Document() # Creates and adds a paragraph node to the document. para = Paragraph(doc) # Typed access to the last section of the document. section = doc.getLastSection() section.getBody().appendChild(para) # Next print the node type of one of the nodes in the document. nodeType = doc.getFirstSection().getBody().getNodeType() print "NodeType: " + Node.nodeTypeToString(nodeType)
def different_page_setup(self): """ Shows how to append a document to another document continuously which has different page settings. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Set the source document to continue straight after the end of the destination document. # If some page setup settings are different then this may not work and the source document will appear # on a new page. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS) # To ensure this does not happen when the source document has different page setup settings make sure the # settings are identical between the last section of the destination document. # If there are further continuous sections that follow on in the source document then this will need to be # repeated for those sections as well. srcDoc.getFirstSection().getPageSetup().setPageWidth(dstDoc.getLastSection().getPageSetup().getPageWidth()) srcDoc.getFirstSection().getPageSetup().setPageHeight(dstDoc.getLastSection().getPageSetup().getPageHeight()) srcDoc.getFirstSection().getPageSetup().setOrientation(dstDoc.getLastSection().getPageSetup().getOrientation()) dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.DifferentPageSetup Out.doc")