public static void main(String[] args) throws Exception { // The path to the documents directory. gDataDir = Utils.getDataDir(KeepSourceTogether.class); Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc"); Document srcDoc = new Document(gDataDir + "TestFile.Source.doc"); // Set the source document to appear straight after the destination document's content. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS); // Iterate through all sections in the source document. for (Paragraph para : (Iterable<Paragraph>) srcDoc.getChildNodes(NodeType.PARAGRAPH, true)) { para.getParagraphFormat().setKeepWithNext(true); } dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); dstDoc.save(gDataDir + "TestDcc.KeepSourceTogether Out.doc"); System.out.println("Documents appended successfully."); }
public static void main(String[] args) throws Exception { // The path to the documents directory. String dataDir = Utils.getDataDir(InsertNestedFields.class); Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert few page breaks (just for testing) for (int i = 0; i < 5; i++) builder.insertBreak(BreakType.PAGE_BREAK); // 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 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"); System.out.println("Nested fields inserted into the document successfully."); }
public class CloneDocument { public static final String dataDir = Utils.getSharedDataDir(AccessingDocumentProperties.class) + "Document/"; public static void main(String[] args) throws Exception { Document doc = new Document(dataDir + "Document.doc"); Document clone = doc.deepClone(); } }
public static void main(String[] args) throws Exception { // The path to the documents directory. String dataDir = Utils.getDataDir(DocumentBuilderMoveToNode.class); // Open the document. Document doc = new Document(dataDir + "DocumentBuilder.doc"); DocumentBuilder builder = new DocumentBuilder(doc); builder.moveTo(doc.getFirstSection().getBody().getLastParagraph()); doc.save(dataDir + "output.doc"); }
public static void main(String[] args) throws Exception { // The path to the documents directory. String dataDir = Utils.getDataDir(DocumentBuilderSetPageSetupAndSectionFormatting.class); // Open the document. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getPageSetup().setOrientation(Orientation.LANDSCAPE); builder.getPageSetup().setLeftMargin(50); builder.getPageSetup().setPaperSize(PaperSize.PAPER_10_X_14); doc.save(dataDir + "output.doc"); }
public static void main(String[] args) throws Exception { // The path to the documents directory. String dataDir = Utils.getDataDir(AsposeFindnReplace.class); Document doc = new Document(dataDir + "replaceDoc.doc"); // Replaces all 'sad' and 'mad' occurrences with 'bad' doc.getRange().replace("sad", "bad", false, true); // Replaces all 'sad' and 'mad' occurrences with 'bad' doc.getRange().replace(Pattern.compile("[s|m]ad"), "bad"); doc.save(dataDir + "AsposeReplaced.doc", SaveFormat.DOC); System.out.println("Done."); }
public static void main(String[] args) throws Exception { // The path to the documents directory. String dataDir = Utils.getDataDir(ConvertFieldsInParagraph.class); Document doc = new Document(dataDir + "TestFile.doc"); // Pass the appropriate parameters to convert all IF fields to static text that are encountered // only in the last // paragraph of the document. FieldsHelper.convertFieldsToStaticText( doc.getFirstSection().getBody().getLastParagraph(), FieldType.FIELD_IF); // Save the document with fields transformed to disk. doc.save(dataDir + "TestFileParagraph Out.doc"); System.out.println("Converted fields in the paragraph with text successfully."); }
public static void main(String[] args) throws Exception { // The path to the documents directory. String dataDir = Utils.getDataDir(FindAndReplace.class); // Open the document. Document doc = new Document(dataDir + "ReplaceSimple.doc"); // Check the text of the document System.out.println("Original document text: " + doc.getRange().getText()); Pattern regex = Pattern.compile("_CustomerName_", Pattern.CASE_INSENSITIVE); // Replace the text in the document. doc.getRange().replace(regex, "James Bond"); // Check the replacement was made. System.out.println("Document text after replace: " + doc.getRange().getText()); // Save the modified document. doc.save(dataDir + "ReplaceSimple Out.doc"); System.out.println("Text found and replaced successfully."); }
public static void main(String[] args) throws Exception { // The path to the documents directory. String dataDir = Utils.getDataDir(DocumentBuilderSetParagraphFormatting.class); // Open the document. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); ParagraphFormat paragraphFormat = builder.getParagraphFormat(); paragraphFormat.setAlignment(ParagraphAlignment.CENTER); paragraphFormat.setLeftIndent(50); paragraphFormat.setRightIndent(50); paragraphFormat.setSpaceAfter(25); paragraphFormat.setKeepTogether(true); builder.writeln( "I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping."); builder.writeln( "I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like."); doc.save(dataDir + "output.doc"); }
public static void main(String[] args) throws Exception { // The path to the documents directory. String dataDir = Utils.getDataDir(CheckFormat.class); String supportedDir = dataDir + "OutSupported" + File.separator; String unknownDir = dataDir + "OutUnknown" + File.separator; String encryptedDir = dataDir + "OutEncrypted" + File.separator; String pre97Dir = dataDir + "OutPre97" + File.separator; File[] fileList = new java.io.File(dataDir).listFiles(); // Loop through all found files. for (File file : fileList) { if (file.isDirectory()) continue; // Extract and display the file name without the path. String nameOnly = file.getName(); System.out.print(nameOnly); // Check the file format and move the file to the appropriate folder. String fileName = file.getPath(); FileFormatInfo info = FileFormatUtil.detectFileFormat(fileName); // Display the document type. switch (info.getLoadFormat()) { case LoadFormat.DOC: System.out.println("\tMicrosoft Word 97-2003 document."); break; case LoadFormat.DOT: System.out.println("\tMicrosoft Word 97-2003 template."); break; case LoadFormat.DOCX: System.out.println("\tOffice Open XML WordprocessingML Macro-Free Document."); break; case LoadFormat.DOCM: System.out.println("\tOffice Open XML WordprocessingML Macro-Enabled Document."); break; case LoadFormat.DOTX: System.out.println("\tOffice Open XML WordprocessingML Macro-Free Template."); break; case LoadFormat.DOTM: System.out.println("\tOffice Open XML WordprocessingML Macro-Enabled Template."); break; case LoadFormat.FLAT_OPC: System.out.println("\tFlat OPC document."); break; case LoadFormat.RTF: System.out.println("\tRTF format."); break; case LoadFormat.WORD_ML: System.out.println("\tMicrosoft Word 2003 WordprocessingML format."); break; case LoadFormat.HTML: System.out.println("\tHTML format."); break; case LoadFormat.MHTML: System.out.println("\tMHTML (Web archive) format."); break; case LoadFormat.ODT: System.out.println("\tOpenDocument Text."); break; case LoadFormat.OTT: System.out.println("\tOpenDocument Text Template."); break; case LoadFormat.DOC_PRE_WORD_97: System.out.println("\tMS Word 6 or Word 95 format."); break; case LoadFormat.UNKNOWN: default: System.out.println("\tUnknown format."); break; } // Now copy the document into the appropriate folder. if (info.isEncrypted()) { System.out.println("\tAn encrypted document."); fileCopy(fileName, new File(encryptedDir, nameOnly).getPath()); } else { switch (info.getLoadFormat()) { case LoadFormat.DOC_PRE_WORD_97: fileCopy(fileName, new File(pre97Dir + nameOnly).getPath()); break; case LoadFormat.UNKNOWN: fileCopy(fileName, new File(unknownDir + nameOnly).getPath()); break; default: fileCopy(fileName, new File(supportedDir + nameOnly).getPath()); break; } } } // ExEnd }