def __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document() builder = DocumentBuilder(doc) builder.writeln('Hello World!') doc.save(dataDir + 'HelloWorld.docx') print "Document saved."
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 __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document() builder = DocumentBuilder(doc) pageSetup = builder.getPageSetup() pageSetup.setTopMargin(0.5) pageSetup.setBottomMargin(0.5) pageSetup.setLeftMargin(0.5) pageSetup.setRightMargin(0.5) pageSetup.setHeaderDistance(0.2) pageSetup.setFooterDistance(0.2) doc.save(dataDir + "PageBorders.docx") print "Done."
def extract_content_using_field(self, doc): """ Shows how to extract content between a specific field and paragraph in the document using the ExtractContent method. """ # Use a document builder to retrieve the field start of a merge field. builder = DocumentBuilder(doc) # Pass the first boolean parameter to get the DocumentBuilder to move to the FieldStart of the field. # We could also get FieldStarts of a field using GetChildNode method as in the other examples. builder.moveToMergeField("Fullname", False, False) # The builder cursor should be positioned at the start of the field. startField = builder.getCurrentNode() endPara = doc.getFirstSection().getChild(NodeType.PARAGRAPH, 5, True) # Extract the content between these nodes in the document. Don't include these markers in the extraction. extractedNodes = self.extract_contents(startField, endPara, False) # Insert the content into a new separate document and save it to disk. dstDoc = self.generate_document(doc, extractedNodes) dstDoc.save(self.dataDir + "TestFile.Fields Out.pdf")
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document() builder = DocumentBuilder(doc) builder.insertImage(dataDir + "background.jpg"); builder.insertImage(dataDir + "background.jpg", RelativeHorizontalPosition.MARGIN, 100, RelativeVerticalPosition.MARGIN, 200, 200, 100, WrapType.SQUARE) doc.save(dataDir + "InsertImage.docx") print "Done."
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' # Demonstrates how to insert fields and update them using Aspose.Words. # First create a blank document. doc = Document() # Use the document builder to insert some content and fields. builder = DocumentBuilder(doc) # Insert a table of contents at the beginning of the document. builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u") builder.writeln() # Insert some other fields. builder.write("Page: ") builder.insertField("PAGE") builder.write(" of ") builder.insertField("NUMPAGES") builder.writeln() builder.write("Date: ") builder.insertField("DATE") # Start the actual document content on the second page. builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE) # Build a document with complex structure by applying different heading styles thus creating TOC entries. builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1) builder.writeln("Heading 1") builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2) builder.writeln("Heading 1.1") builder.writeln("Heading 1.2") builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1) builder.writeln("Heading 2") builder.writeln("Heading 3") # Move to the next page. builder.insertBreak(BreakType.PAGE_BREAK) builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2) builder.writeln("Heading 3.1") builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3) builder.writeln("Heading 3.1.1") builder.writeln("Heading 3.1.2") builder.writeln("Heading 3.1.3") builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2) builder.writeln("Heading 3.2") builder.writeln("Heading 3.3") print "Updating all fields in the document." # Call the method below to update the TOC. doc.updateFields() doc.save(dataDir + "Document Field Update Out.docx") print "Fields updated in the document successfully."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document() builder = DocumentBuilder(doc) # Insert few page breaks (just for testing) i = 0 while(i < 5): builder.insertBreak(BreakType.PAGE_BREAK) i = i + 1 # Move DocumentBuilder cursor into the primary footer. builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY) # We want to insert a field like this: # { IF {PAGE} <> {NUMPAGES} "See Next Page" "Last Page" } field = builder.insertField("IF ") builder.moveTo(field.getSeparator()) builder.insertField("PAGE") builder.write(" <> ") builder.insertField("NUMPAGES") builder.write(" \"See Next Page\" \"Last Page\" ") # Finally update the outer field to recalcaluate the final value. Doing this will automatically update # the inner fields at the same time. field.update() doc.save(dataDir + "InsertNestedFields Out.docx")
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document() builder = DocumentBuilder(doc) # Insert few page breaks (just for testing) i = 0 while (i < 5): builder.insertBreak(BreakType.PAGE_BREAK) i = i + 1 # Move DocumentBuilder cursor into the primary footer. builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY) # We want to insert a field like this: # { IF {PAGE} <> {NUMPAGES} "See Next Page" "Last Page" } field = builder.insertField("IF ") builder.moveTo(field.getSeparator()) builder.insertField("PAGE") builder.write(" <> ") builder.insertField("NUMPAGES") builder.write(" \"See Next Page\" \"Last Page\" ") # Finally update the outer field to recalcaluate the final value. Doing this will automatically update # the inner fields at the same time. field.update() doc.save(dataDir + "InsertNestedFields Out.docx")
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "document.doc") builder = DocumentBuilder(doc) #Shows how to access the current node in a document builder. curNode = builder.getCurrentNode() curParagraph = builder.getCurrentParagraph() # Shows how to move a cursor position to a specified node. builder.moveTo(doc.getFirstSection().getBody().getLastParagraph()) # Shows how to move a cursor position to the beginning or end of a document. builder.moveToDocumentEnd() builder.writeln("This is the end of the document.") builder.moveToDocumentStart() builder.writeln("This is the beginning of the document.") doc.save(dataDir + "MovingCursor.doc") print "Done."
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' # Demonstrates how to insert fields and update them using Aspose.Words. # First create a blank document. doc = Document() # Use the document builder to insert some content and fields. builder = DocumentBuilder(doc) # Insert a table of contents at the beginning of the document. builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u") builder.writeln() # Insert some other fields. builder.write("Page: ") builder.insertField("PAGE") builder.write(" of ") builder.insertField("NUMPAGES") builder.writeln() builder.write("Date: ") builder.insertField("DATE") # Start the actual document content on the second page. builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE) # Build a document with complex structure by applying different heading styles thus creating TOC entries. builder.getParagraphFormat().setStyleIdentifier( StyleIdentifier.HEADING_1) builder.writeln("Heading 1") builder.getParagraphFormat().setStyleIdentifier( StyleIdentifier.HEADING_2) builder.writeln("Heading 1.1") builder.writeln("Heading 1.2") builder.getParagraphFormat().setStyleIdentifier( StyleIdentifier.HEADING_1) builder.writeln("Heading 2") builder.writeln("Heading 3") # Move to the next page. builder.insertBreak(BreakType.PAGE_BREAK) builder.getParagraphFormat().setStyleIdentifier( StyleIdentifier.HEADING_2) builder.writeln("Heading 3.1") builder.getParagraphFormat().setStyleIdentifier( StyleIdentifier.HEADING_3) builder.writeln("Heading 3.1.1") builder.writeln("Heading 3.1.2") builder.writeln("Heading 3.1.3") builder.getParagraphFormat().setStyleIdentifier( StyleIdentifier.HEADING_2) builder.writeln("Heading 3.2") builder.writeln("Heading 3.3") print "Updating all fields in the document." # Call the method below to update the TOC. doc.updateFields() doc.save(dataDir + "Document Field Update Out.docx") print "Fields updated in the document successfully."