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): 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/" # Load the document. doc = Document(dataDir + "tableDoc.doc") # Get the first table in the document. firstTable = doc.getChild(NodeType.TABLE, 0, True) # We will split the table at the third row (inclusive). row = firstTable.getRows().get(2) # Create a new container for the split table. table = firstTable.deepClone(False) # Insert the container after the original. firstTable.getParentNode().insertAfter(table, firstTable) # Add a buffer paragraph to ensure the tables stay apart. firstTable.getParentNode().insertAfter(Paragraph(doc), firstTable) currentRow = "" while currentRow != row: currentRow = firstTable.getLastRow() table.prependChild(currentRow) doc.save(dataDir + "SplitTable.doc") print "Done."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' # Load the document. doc = Document(dataDir + "tableDoc.doc") # Get the first table in the document. firstTable = doc.getChild(NodeType.TABLE, 0, True) # We will split the table at the third row (inclusive). row = firstTable.getRows().get(2) # Create a new container for the split table. table = firstTable.deepClone(False) # Insert the container after the original. firstTable.getParentNode().insertAfter(table, firstTable) # Add a buffer paragraph to ensure the tables stay apart. firstTable.getParentNode().insertAfter(Paragraph(doc), firstTable) currentRow = '' while (currentRow != row): currentRow = firstTable.getLastRow() table.prependChild(currentRow) doc.save(dataDir + "SplitTable.doc") print "Done."
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document(dataDir + 'Document.doc') doc.save(dataDir + 'Document_Out.doc') print "Document saved."
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document(dataDir + 'Document.doc') doc.save(dataDir + 'Document.pdf') print "Converted document to PDF."
def __init__(self): dataDir = Settings.dataDir + "loading_saving/" # The encoding of the text file is automatically detected. doc = Document(dataDir + "LoadTxt.txt") # Save as any Aspose.Words supported format, such as DOCX. doc.save(dataDir + "LoadTxt_Out.docx") print "Process Completed Successfully"
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "trackDoc.doc") doc.acceptAllRevisions() doc.save(dataDir + "AcceptChanges.doc", SaveFormat.DOC) print "Done."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "TestFile.doc") self.insert_watermark_text(doc, "CONFIDENTIAL") doc.save(dataDir + "Watermark.doc") print "Watermark added to the document successfully."
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 + 'quickstart/' dstDoc = Document(dataDir + 'TestFile.Destination.doc') srcDoc = Document(dataDir + 'TestFile.Source.doc') dstDoc.appendDocument(srcDoc,ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(dataDir + 'AppendDocuments.doc') print ("Documents appended successfully.")
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 keep_source_formatting(self): """ Shows how to append a document to the end of another document using no additional options. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Append the source document to the destination document using no extra options. dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.KeepSourceFormatting Out.docx")
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "RemoveField.doc") field = doc.getRange().getFields().get(0) # Calling this method completely removes the field from the document. field.remove() doc.save(dataDir + "RemoveField Out.docx") print "Field removed from the document successfully."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "document.doc") doc.protect(ProtectionType.READ_ONLY) #doc.protect(ProtectionType.ALLOW_ONLY_COMMENTS) #doc.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS) #doc.protect(ProtectionType.ALLOW_ONLY_REVISIONS) doc.save(dataDir + "ProtectDocument.doc", SaveFormat.DOC) print "Done."
def use_destination_styles(self): """ Shows how to append a document to another document using the formatting of the destination document. """ # Load the documents to join. dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Append the source document using the styles of the destination document. dstDoc.appendDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES) # Save the joined document to disk. dstDoc.save(self.dataDir + "TestFile.UseDestinationStyles Out.doc")
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document(dataDir + "MailMerge.doc"); # Fill the fields in the document with user data. doc.getMailMerge().execute( ["FullName", "Company", "Address", "Address2", "City"], ["James Bond", "MI5 Headquarters", "Milbank", "", "London"]) # Saves the document to disk. doc.save(dataDir + "MailMerge Result Out.docx") print "Mail merge performed successfully."
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document(dataDir + "MailMerge.doc") # Fill the fields in the document with user data. doc.getMailMerge().execute( ["FullName", "Company", "Address", "Address2", "City"], ["James Bond", "MI5 Headquarters", "Milbank", "", "London"]) # Saves the document to disk. doc.save(dataDir + "MailMerge Result Out.docx") print "Mail merge performed successfully."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' # Open the document. doc = Document(dataDir + "TestRemoveBreaks.doc") # Remove the page and section breaks from the document. # In Aspose.Words section breaks are represented as separate Section nodes in the document. # To remove these separate sections the sections are combined. self.remove_page_breaks(doc) self.remove_section_breaks(doc) # Save the document. doc.save(dataDir + "TestRemoveBreaks Out.doc")
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 __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document(dataDir + 'ReplaceSimple.doc') # Check the text of the document. print "Original document text: " + doc.getRange().getText() # Replace the text in the document. doc.getRange().replace("_CustomerName_", "James Bond", False, False) # Check the replacement was made. print "Document text after replace: " + doc.getRange().getText() doc.save(dataDir + 'ReplaceSimple_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 __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 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 copy_bookmarked_text(self): # Load the source document. srcDoc = Document(self.dataDir + "Template.doc") # This is the bookmark whose content we want to copy. srcBookmark = srcDoc.getRange().getBookmarks().get("ntf010145060") # We will be adding to this document. dstDoc = Document() # Let's say we will be appending to the end of the body of the last section. dstNode = dstDoc.getLastSection().getBody() # It is a good idea to use this import context object because multiple nodes are being imported. # If you import multiple times without a single context, it will result in many styles created. importer = NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) # Do it once. self.append_bookmarked_text(importer, srcBookmark, dstNode) # Do it one more time for fun. self.append_bookmarked_text(importer, srcBookmark, dstNode) # Save the finished document. dstDoc.save(self.dataDir + "Template Out.doc")
def __init__(self): dataDir = Settings.dataDir + 'rendering_printing/' # Open the document. doc = Document(dataDir + "TestFile.doc") # Save the document as multipage TIFF. doc.save(dataDir + "TestFile Out.tiff") # Create an ImageSaveOptions object to pass to the Save method options = ImageSaveOptions(SaveFormat.TIFF) options.setPageIndex(0) options.setPageCount(2) options.setTiffCompression(TiffCompression.CCITT_4) options.setResolution(160) doc.save(dataDir + "TestFileWithOptions Out.tiff", options) "Document saved as multi page TIFF successfully."
def __init__(self): self.dataDir = Settings.dataDir + 'programming_documents/' self.copy_bookmarked_text() # Load a document. doc = Document(self.dataDir + "TestDefect1352.doc") # This perform the custom task of putting the row bookmark ends into the same row with the bookmark starts. self.untangle_row_bookmarks(doc) # Now we can easily delete rows by a bookmark without damaging any other row's bookmarks. self.delete_row_by_bookmark(doc, "ROW2") # This is just to check that the other bookmark was not damaged. if doc.getRange().getBookmarks().get("ROW1").getBookmarkEnd() is None: raise ValueError('Wrong, the end of the bookmark was deleted.') # Save the finished document. doc.save(self.dataDir + "TestDefect1352 Out.doc")
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 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): dataDir = Settings.dataDir + 'quickstart/' # Open the stream. Read only access is enough for Aspose.Words to load a document. stream = FileInputStream(dataDir + 'Document.doc') # Load the entire document into memory. doc = Document(stream) # You can close the stream now, it is no longer needed because the document is in memory. stream.close() # ... do something with the document # Convert the document to a different format and save to stream. dstStream = ByteArrayOutputStream() doc.save(dstStream, SaveFormat.RTF) output = FileOutputStream(dataDir + "Document Out.rtf") output.write(dstStream.toByteArray()) output.close() print "Document loaded from stream and then saved successfully."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' # Open the document. doc = Document(dataDir + "TestFile.doc") #ExStart #ExId:ProcessComments_Main #ExSummary: The demo-code that illustrates the methods for the comments extraction and removal. # Extract the information about the comments of all the authors. comments = self.extract_comments(doc) for comment in comments: print comment # Remove all comments. self.remove_comments(doc) print "All comments are removed!" # Save the document. doc.save(dataDir + "Comments.doc")