Esempio n. 1
0
 private static function removePageBreaks($doc)
 {
     // Retrieve all paragraphs in the document.
     $nodeType = Java("com.aspose.words.NodeType");
     $paragraphs = $doc->getChildNodes($nodeType->PARAGRAPH, true);
     $paragraphs_count = $paragraphs->getCount();
     $paragraphs_count = java_values($paragraphs_count);
     // echo "<pre>";
     // echo java_inspect($paragraphs); exit;
     $i = 0;
     while ($i < $paragraphs_count) {
         $paragraphs = $doc->getChildNodes($nodeType->PARAGRAPH, true);
         $para = $paragraphs->get($i);
         if ($para->getParagraphFormat()->getPageBreakBefore()) {
             $para->getParagraphFormat()->setPageBreakBefore(false);
         }
         $runs = $para->getRuns()->toArray();
         foreach ($runs as $run) {
             //echo "<pre>"; echo java_inspect($run); exit;
             $controlChar = Java("com.aspose.words.ControlChar");
             if (java_values($run->getText()->contains($controlChar->PAGE_BREAK))) {
                 $run_text = java_values($run->getText());
                 $run_text = str_replace($controlChar->PAGE_BREAK, "", $run_text);
                 $run->setText($run_text);
             }
         }
         $i++;
     }
 }
 public static function removeComments()
 {
     $args = func_get_args();
     $doc = $args[0];
     if (isset($args[1]) && !empty($args[1])) {
         $authorName = $args[1];
     }
     // Collect all comments in the document
     $nodeType = Java("com.aspose.words.NodeType");
     $comments = $doc->getChildNodes($nodeType->COMMENT, true);
     $comments_count = $comments->getCount();
     // Look through all comments and remove those written by the authorName author.
     $i = $comments_count;
     $i = $i - 1;
     while ($i >= 0) {
         $comment = $comments->get($i);
         //echo "<PRE>"; echo java_inspect($comment); exit;
         if (isset($authorName)) {
             if (java_values($comment->getAuthor()->equals($authorName))) {
                 $comment->remove();
             }
         } else {
             $comment->remove();
         }
         $i--;
     }
 }
 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 runsByStyleName($doc, $styleName)
 {
     // Create an array to collect runs of the specified style.
     $runsWithStyle = new Java("java.util.ArrayList");
     // Get all runs from the document.
     $nodeType = Java("com.aspose.words.NodeType");
     $runs = $doc->getChildNodes($nodeType->RUN, true);
     // Look through all runs to find those with the specified style.
     $runs = $runs->toArray();
     foreach ($runs as $run) {
         if (java_values($run->getFont()->getStyle()->getName()->equals($styleName))) {
             $runsWithStyle->add($run);
         }
     }
     return $runsWithStyle;
 }
 public static function untangleRowBookmark($doc)
 {
     $bookmarks = $doc->getRange()->getBookmarks();
     $bookmarks_count = java_values($bookmarks->getCount());
     $x = 0;
     while ($x < 8) {
         $bookmark = $bookmarks->get($x);
         //$row = new Java("com.aspose.words.Row");
         $row1 = $bookmark->getBookmarkStart()->getAncestor(Java("com.aspose.words.Row"));
         $row2 = $bookmark->getBookmarkEnd()->getAncestor(Java("com.aspose.words.Row"));
         // If both rows are found okay and the bookmark start and end are contained
         // in adjacent rows, then just move the bookmark end node to the end
         // of the last paragraph in the last cell of the top row.
         if (java_values($row1) != null && java_values($row2) != null && java_values($row1->getNextSibling()) == java_values($row2)) {
             $row1->getLastCell()->getLastParagraph()->appendChild($bookmark->getBookmarkEnd());
         }
         $x++;
     }
 }
 /**
  * This method is called by the Aspose.Words find and replace engine for each match.
  * This method highlights the match string, even if it spans multiple runs.
  */
 public static function replacing($e)
 {
     // This is a Run node that contains either the beginning or the complete match.
     $currentNode = $e->getMatchNode();
     // The first (and may be the only) run can contain text before the match,
     // in this case it is necessary to split the run.
     if (java_values($e->getMatchOffset()) > 0) {
         $currentNode = ReplaceEvaluatorFindAndHighlight::splitRun($currentNode, $e->getMatchOffset());
     }
     // This array is used to store all nodes of the match for further highlighting.
     $runs = new Java("java.util.ArrayList");
     // Find all runs that contain parts of the match string.
     $remainingLength = $e->getMatch()->group()->length();
     while (java_values($remainingLength) > 0 && java_values($currentNode) != null && java_values($currentNode->getText()->length()) <= java_values($remainingLength)) {
         $runs->add($currentNode);
         $remainingLength = java_values($remainingLength) - java_values($currentNode->getText()->length());
         // Select the next Run node.
         // Have to loop because there could be other nodes such as BookmarkStart etc.
         do {
             $currentNode = $currentNode->getNextSibling();
             $nodeType = Java("com.aspose.words.NodeType");
         } while (java_values($currentNode) != null && java_values($currentNode->getNodeType()) != java_values($nodeType->RUN));
     }
     // Split the last run that contains the match if there is any text left.
     if (java_values($currentNode) != null && java_values($remainingLength) > 0) {
         ReplaceEvaluatorFindAndHighlight::splitRun($currentNode, $remainingLength);
         $runs->add($currentNode);
     }
     // Now highlight all runs in the sequence.
     $runs = $runs->toArray();
     $color = Java("java.awt.Color");
     foreach ($runs as $run) {
         $run->getFont()->setHighlightColor('yellow');
     }
     // Signal to the replace engine to do nothing because we have already done all what we wanted.
     $replaceAction = Java("com.aspose.words.ReplaceAction");
     return $replaceAction->SKIP;
 }
Esempio n. 7
0
 /**
  * Must be called at the end.
  */
 function end($autoflush = true)
 {
     if (!$this->noend) {
         if ($this->evalTag != java_values(Java("javax.servlet.jsp.tagext.Tag")->EVAL_BODY_INCLUDE)) {
             $this->pc->pc->popBody();
         }
     }
     $this->clazz->doEndTag();
     if ($autoflush) {
         $this->pc->out->flush();
         echo java_values($this->pc->response->getBufferContents());
     }
     return true;
 }
 public static function paragraphsByStyleName($doc, $styleName)
 {
     // Create an array to collect paragraphs of the specified style.
     $paragraphsWithStyle = new Java("java.util.ArrayList");
     // Get all paragraphs from the document.
     $nodeType = Java("com.aspose.words.NodeType");
     $paragraphs = $doc->getChildNodes($nodeType->PARAGRAPH, true);
     $paragraphs_count = $paragraphs->getCount();
     $paragraphs_count = java_values($paragraphs_count);
     // Look through all paragraphs to find those with the specified style.
     $i = 0;
     while ($i < $paragraphs_count) {
         $paragraphs = $doc->getChildNodes($nodeType->PARAGRAPH, true);
         $paragraph = $paragraphs->get($i);
         if (java_values($paragraph->getParagraphFormat()->getStyle()->getName()->equals($styleName))) {
             $paragraphsWithStyle->add($paragraph);
         }
         $i++;
     }
     return $paragraphsWithStyle;
 }