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");
 }
Esempio n. 2
0
 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");
 }
 /**
  * Replaces all NUMPAGES fields in the document with PAGEREF fields. The replacement field displays the total number
  * of pages in the sub document instead of the total pages in the document.
  *
  * @param doc The combined document to process.
  */
 public static function convertNumPageFieldsToPageRef($doc)
 {
     // This is the prefix for each bookmark which signals where page numbering restarts.
     // The underscore "_" at the start inserts this bookmark as hidden in MS Word.
     // Create a new DocumentBuilder which is used to insert the bookmarks and replacement fields.
     $builder = new Java("com.aspose.words.DocumentBuilder", $doc);
     // Defines the number of page restarts that have been encountered and therefore the number of "sub" documents
     // found within this document.
     $subDocumentCount = 0;
     // Iterate through all sections in the document.
     $sections = $doc->getSections()->toArray();
     foreach ($sections as $section) {
         // This section has it's page numbering restarted so we will treat this as the start of a sub document.
         // Any PAGENUM fields in this inner document must be converted to special PAGEREF fields to correct numbering.
         if ($section->getPageSetup()->getRestartPageNumbering()) {
             // Don't do anything if this is the first section in the document. This part of the code will insert the bookmark marking
             // the end of the previous sub document so therefore it is not applicable for first section in the document.
             if (!java_values($section->equals($doc->getFirstSection()))) {
                 // Get the previous section and the last node within the body of that section.
                 $prevSection = $section->getPreviousSibling();
                 $lastNode = $prevSection->getBody()->getLastChild();
                 // Use the DocumentBuilder to move to this node and insert the bookmark there.
                 // This bookmark represents the end of the sub document.
                 $builder->moveTo($lastNode);
                 $builder->startBookmark(AppendDocument::BOOKMARK_PREFIX . $subDocumentCount);
                 $builder->endBookmark(AppendDocument::BOOKMARK_PREFIX . $subDocumentCount);
                 // Increase the subdocument count to insert the correct bookmarks.
                 $subDocumentCount++;
             }
         }
         // The last section simply needs the ending bookmark to signal that it is the end of the current sub document.
         if (java_values($section->equals($doc->getLastSection()))) {
             // Insert the bookmark at the end of the body of the last section.
             // Don't increase the count this time as we are just marking the end of the document.
             $lastNode = $doc->getLastSection()->getBody()->getLastChild();
             $builder->moveTo($lastNode);
             $builder->startBookmark(AppendDocument::BOOKMARK_PREFIX . $subDocumentCount);
             $builder->endBookmark(AppendDocument::BOOKMARK_PREFIX . $subDocumentCount);
         }
         // Iterate through each NUMPAGES field in the section and replace the field with a PAGEREF field referring to the bookmark of the current subdocument
         // This bookmark is positioned at the end of the sub document but does not exist yet. It is inserted when a section with restart page numbering or the last
         // section is encountered.
         $nodeType = new Java("com.aspose.words.NodeType");
         $nodes = $section->getChildNodes($nodeType->FIELD_START, true)->toArray();
         foreach ($nodes as $node) {
             $fieldStart = $node;
             $fieldType = new Java("com.aspose.words.FieldType");
             if (java_values($fieldStart->getFieldType()) == java_values($fieldType->FIELD_NUM_PAGES)) {
                 // Get the field code.
                 $fieldCode = AppendDocument::getFieldCode($fieldStart);
                 // Since the NUMPAGES field does not take any additional parameters we can assume the remaining part of the field
                 // code after the fieldname are the switches. We will use these to help recreate the NUMPAGES field as a PAGEREF field.
                 $fieldCode = java_values($fieldCode);
                 $fieldSwitches = str_replace(AppendDocument::NUM_PAGES_FIELD_NAME, '', $fieldCode);
                 $fieldSwitches = trim($fieldSwitches);
                 // Inserting the new field directly at the FieldStart node of the original field will cause the new field to
                 // not pick up the formatting of the original field. To counter this insert the field just before the original field
                 $previousNode = $fieldStart->getPreviousSibling();
                 // If a previous run cannot be found then we are forced to use the FieldStart node.
                 if (java_values($previousNode) == null) {
                     $previousNode = $fieldStart;
                 }
                 // Insert a PAGEREF field at the same position as the field.
                 $builder->moveTo($previousNode);
                 $message = java_values(AppendDocument::PAGE_REF_FIELD_NAME) . ' ' . java_values(AppendDocument::BOOKMARK_PREFIX) . java_values($subDocumentCount) . ' ' . java_values($fieldSwitches);
                 $newField = $builder->insertField(java_values($message));
                 // The field will be inserted before the referenced node. Move the node before the field instead.
                 $previousNode->getParentNode()->insertBefore($previousNode, $newField->getStart());
                 // Remove the original NUMPAGES field from the document.
                 AppendDocument::removeField($fieldStart);
             }
         }
     }
 }