public static function main() { // The path to the documents directory. $dataDir = "/usr/local/apache-tomcat-8.0.22/webapps/JavaBridge/Aspose_Words_Java_For_PHP/src/programmingwithdocuments/workingwithfields/insertnestedfields/data/"; $doc = new Java("com.aspose.words.Document"); // Document(); $builder = new Java("com.aspose.words.DocumentBuilder", $doc); // DocumentBuilder(doc); // Insert few page breaks (just for testing) $breakType = Java("com.aspose.words.BreakType"); for ($i = 0; $i < 5; $i++) { $builder->insertBreak($breakType->PAGE_BREAK); } // Move DocumentBuilder cursor into the primary footer. $headerFooterType = Java("com.aspose.words.HeaderFooterType"); $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"); }
public static function extractContentBetweenBlockLevelNodes() { //ExStart //ExId:ExtractBetweenNodes_BetweenNodes //ExSummary:Shows how to extract the content between a paragraph and table using the ExtractContent method. // Load in the document $doc = new Java("com.aspose.words.Document", ExtractContent::$gDataDir . "TestFile.doc"); $nodeType = Java("com.aspose.words.NodeType"); $startPara = $doc->getLastSection()->getChild($nodeType->PARAGRAPH, 2, true); $endTable = $doc->getLastSection()->getChild($nodeType->TABLE, 0, true); // Extract the content between these nodes in the document. Include these markers in the extraction. $extractedNodes = ExtractContent::ExtractContents($startPara, $endTable, true); // Lets reverse the array to make inserting the content back into the document easier. $collections = new Java("java.util.Collections"); $collections->reverse($extractedNodes); while (java_values($extractedNodes->size()) > 0) { // Insert the last node from the reversed list $endTable->getParentNode()->insertAfter($extractedNodes->get(0), $endTable); // Remove this node from the list after insertion. $extractedNodes->remove(0); } // Save the generated document to disk. $doc->save(ExtractContent::$gDataDir . "TestFile.DuplicatedContent Out.doc"); //ExEnd }
public static function saveToDisk() { // The path to the documents directory. $dataDir = "/usr/local/apache-tomcat-8.0.22/webapps/JavaBridge/Aspose_Words_Java_For_PHP/src/quickstart/loadandsavetodisk/data/"; // Load the document from the absolute path on disk. $doc = new Java("com.aspose.words.Document", $dataDir . "Document.doc"); // Save the document as DOCX document."); $doc->save($dataDir . "Document Out.docx"); }
public static function mailmerge() { // The path to the documents directory. $dataDir = "/usr/local/apache-tomcat-8.0.22/webapps/JavaBridge/Aspose_Words_Java_For_PHP/src/quickstart/simplemailmerge/data/"; $doc = new Java("com.aspose.words.Document", $dataDir . "Template.doc"); // Fill the fields in the document with user data. $doc->getMailMerge()->execute(array("FullName", "Company", "Address", "Address2", "City"), array("James Bond", "MI5 Headquarters", "Milbank", "", "London")); // Saves the document to disk. $doc->save($dataDir . "MailMerge Result Out.docx"); }
public static function AppendDocs() { // The path to the documents directory. $dataDir = "/usr/local/apache-tomcat-8.0.22/webapps/JavaBridge/Aspose_Words_Java_For_PHP/src/quickstart/appenddocuments/data/"; // Load the destination and source documents from disk. $dstDocObject = new Java("com.aspose.words.Document", $dataDir . "TestFile.Destination.doc"); $srcDocObject = new Java("com.aspose.words.Document", $dataDir . "TestFile.Source.doc"); $importFormatModeObject = new java('com.aspose.words.ImportFormatMode'); $sourceFormating = $importFormatModeObject->KEEP_SOURCE_FORMATTING; // Append the source document to the destination document while keeping the original formatting of the source document. $dstDocObject->appendDocument(java_values($srcDocObject), java_values($sourceFormating)); $dstDocObject->save($dataDir . "TestFile_Out.docx"); }
public static function printHelloWorld() { // The path to the documents directory. $dataDir = "/usr/local/apache-tomcat-8.0.22/webapps/JavaBridge/Aspose_Words_Java_For_PHP/src/quickstart/helloworld/data/"; // Create a blank document. $documentObject = new Java("com.aspose.words.Document"); // DocumentBuilder provides members to easily add content to a document. $builderBoject = new Java("com.aspose.words.DocumentBuilder", $documentObject); // Write a new paragraph in the document with the text "Hello World!" $builderBoject->writeln("Hello World!"); // Save the document in DOCX format. The format to save as is inferred from the extension of the file name. // Aspose.Words supports saving any document in many more formats. $documentObject->save($dataDir . "HelloWorld.docx"); }
public static function update() { // The path to the documents directory. $dataDir = "/usr/local/apache-tomcat-8.0.22/webapps/JavaBridge/Aspose_Words_Java_For_PHP/src/quickstart/updatefields/data/"; // Demonstrates how to insert fields and update them using Aspose.Words. // First create a blank document. $doc = new Java("com.aspose.words.Document"); // Use the document builder to insert some content and fields. $builder = new Java("com.aspose.words.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. $breakType = new Java("com.aspose.words.BreakType"); $builder->insertBreak($breakType->SECTION_BREAK_NEW_PAGE); // Build a document with complex structure by applying different heading styles thus creating TOC entries. $styleIdentifier = new Java("com.aspose.words.StyleIdentifier"); $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"); echo "Updating all fields in the document."; // Call the method below to update the TOC. $doc->updateFields(); $doc->save($dataDir . "Document Field Update Out.docx"); }
public static function replaceText() { // The path to the documents directory. $dataDir = "/usr/local/apache-tomcat-8.0.22/webapps/JavaBridge/Aspose_Words_Java_For_PHP/src/quickstart/findandreplace/data/"; // Open the document. $doc = new Java("com.aspose.words.Document", $dataDir . "ReplaceSimple.doc"); // Check the text of the document echo "Original document text: " . $doc->getRange()->getText() . "<BR>"; // Replace the text in the document. $doc->getRange()->replace("_CustomerName_", "James Bond", false, false); // Check the replacement was made. echo "Document text after replace: " . $doc->getRange()->getText(); // Save the modified document. $doc->save($dataDir . "ReplaceSimple Out.doc"); }
public static function main() { //$color = Java("java.awt.Color"); //echo java_values($color->YELLOW); exit; // The path to the documents directory. $dataDir = "/usr/local/apache-tomcat-8.0.22/webapps/JavaBridge/Aspose_Words_Java_For_PHP/src/programmingwithdocuments/usingfindandreplace/findandhighlight/data/"; $doc = new Java("com.aspose.words.Document", $dataDir . "TestFile.doc"); // We want the "your document" phrase to be highlighted. $pattern = new Java("java.util.regex.Pattern"); $regex = $pattern->compile("your document", $pattern->CASE_INSENSITIVE); $replaceEvaluation = new ReplaceEvaluatorFindAndHighlight(); $range = $doc->getRange(); $range->replace($regex, java_values($replaceEvaluation)); // Save the output document. $doc->save($dataDir . "TestFile Out.doc"); }
public static function saveToStream() { // The path to the documents directory. $dataDir = "/usr/local/apache-tomcat-8.0.22/webapps/JavaBridge/Aspose_Words_Java_For_PHP/src/quickstart/loadandsavetostream/data/"; // Open the stream. Read only access is enough for Aspose.Words to load a document. $stream = new Java("java.io.FileInputStream", $dataDir . "Document.doc"); // Load the entire document into memory. $doc = new Java("com.aspose.words.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 = new Java("java.io.ByteArrayOutputStream"); $SaveFormat = new Java("com.aspose.words.SaveFormat"); $doc->save($dstStream, $SaveFormat->RTF); $output = new Java("java.io.FileOutputStream", $dataDir . "Document Out.rtf"); $output->write($dstStream->toByteArray()); $output->close(); }
public static function main() { $dataDir = "/usr/local/apache-tomcat-8.0.22/webapps/JavaBridge/Aspose_Words_Java_For_PHP/src/programmingwithdocuments/workingwithbookmarks/copybookmarkedtext/data/"; // Load the source document. $srcDoc = new Java("com.aspose.words.Document", $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 = new Java("com.aspose.words.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. $importFormatMode = java('com.aspose.words.ImportFormatMode'); $importer = new Java("com.aspose.words.NodeImporter", $srcDoc, $dstDoc, $importFormatMode->KEEP_SOURCE_FORMATTING); // Do it once. CopyBookmarkedText::appendBookmarkedText($importer, $srcBookmark, $dstNode); // Do it one more time for fun. CopyBookmarkedText::appendBookmarkedText($importer, $srcBookmark, $dstNode); // Save the finished document. $dstDoc->save($dataDir . "Template Out.doc"); }
public static function autoFitTableToFixedColumnWidths($dataDir) { //ExStart //ExFor:Table.AutoFit //ExFor:AutoFitBehavior //ExId:DisableAutoFitAndUseFixedWidths //ExSummary:Disables autofitting and enables fixed widths for the specified table. // Open the document $doc = new Java("com.aspose.words.Document", $dataDir . "TestFile.doc"); $nodeType = new Java("com.aspose.words.NodeType"); $table = $doc->getChild($nodeType->TABLE, 0, true); // Disable autofitting on this table. $autoFitBehavior = new Java("com.aspose.words.AutoFitBehavior"); $table->autoFit($autoFitBehavior->AUTO_FIT_TO_CONTENTS); // Save the document to disk. $doc->save($dataDir . "TestFile.FixedWidth Out.doc"); //ExEnd $preferredWidthType = new Java("com.aspose.words.PreferredWidthType"); if (java_values($doc->getFirstSection()->getBody()->getTables()->get(0)->getPreferredWidth()->getType()) == java_values($preferredWidthType->AUTO)) { echo "PreferredWidth type is not auto <br />"; } if (java_values($doc->getFirstSection()->getBody()->getTables()->get(0)->getPreferredWidth()->getValue()) == 0) { echo "PreferredWidth value is not 0 <br />"; } if (java_values($doc->getFirstSection()->getBody()->getTables()->get(0)->getFirstRow()->getFirstCell()->getCellFormat()->getWidth()) == 0) { echo "Cell width is not correct. <br />"; } }
public static function DifferentPageSetup() { //ExStart //ExId:AppendDocument_DifferentPageSetup //ExSummary:Shows how to append a document to another document continuously which has different page settings. $dstDoc = new Java("com.aspose.words.Document", AppendDocument::$gDataDir . "TestFile.Destination.doc"); $srcDoc = new Java("com.aspose.words.Document", AppendDocument::$gDataDir . "TestFile.SourcePageSetup.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. $sectionStart = new Java("com.aspose.words.SectionStart"); $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()); $importFormatMode = new Java("com.aspose.words.ImportFormatMode"); $dstDoc->appendDocument($srcDoc, $importFormatMode->KEEP_SOURCE_FORMATTING); $dstDoc->save(AppendDocument::$gDataDir . "TestFile.DifferentPageSetup Out.doc"); //ExEnd }