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 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"); }
function run() { $name = java_values(java_context()->getAttribute("name", 100)); // engine scope $out = new Java("java.io.FileOutputStream", "{$name}.out", true); $Thread = java("java.lang.Thread"); $nr = java_values(java_context()->getAttribute("nr", 100)); echo "started thread: {$nr}\n"; for ($i = 0; $i < 10; $i++) { $out->write(ord("{$nr}")); $Thread->yield(); } $out->close(); }
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(); }
/** * * @param string * The name of a {@link Property} of $this {@link Node} * @param mixed * The value to be assigned * @param int|null * The type of the {@link Property} (Optional: NULL if not * specified). * @return object * A {@link Property} object * @throws {@link ValueFormatException} * If <i>$value</i> cannot be converted to the specified type or * if the property already exists and is multi-valued. * @throws {@link VersionException} * If this node is versionable and checked-in or is non-versionable but * its nearest versionable ancestor is checked-in and this implementation * performs this validation immediately instead of waiting until * {@link save()}. * @throws {@link LockException} * If a lock prevents the setting of the property and this implementation * performs this validation immediately instead of waiting until * {@link save()}. * @throws {@link ConstraintViolationException} * If the change would violate a node-type or other constraint and this * implementation performs this validation immediately instead of * waiting until {@link save()}. * @throws {@link RepositoryException} * If another error occurs. * @see PHPCR_Node::setProperty() */ public function setProperty($name, $value, $type = 1) { $isNew = true; if ($this->hasProperty($name)) { $isNew = false; } $filename = null; switch ($type) { case PHPCR_PropertyType::BINARY: $pr = new Java("javax.jcr.PropertyType"); $type = $pr->BINARY; $strlen = strlen($value); // keep it in memory, if small if ($strlen < 500) { $out = new Java("java.io.ByteArrayOutputStream"); $arr = array(); for ($i = 0; $i < $strlen; $i++) { $val = ord(substr($value, $i, 1)); if ($val >= 128) { $val = $val - 256; } $arr[] = $val; } $out->write($arr); $value = new Java("java.io.ByteArrayInputStream", $out->toByteArray()); } else { $filename = tempnam(sys_get_temp_dir(), "jrcr"); chmod($filename, 0666); file_put_contents($filename, $value); $value = new Java("java.io.FileInputStream", $filename); } break; case PHPCR_PropertyType::DATE: $pr = new Java("javax.jcr.PropertyType"); $type = $pr->DATE; //$ValueFactory = new Java("javax.jcr.ValueFactory"); $cal = new Java("java.util.Calendar"); $val = $cal->getInstance(); if ($value instanceof DateTime) { $val->setTimeInMillis($value->format('U') * 1000); } else { $val->setTimeInMillis($value * 1000); } $value = $val; break; } if (!is_object($value) && $type) { $jrprop = $this->JRnode->setProperty($name, $value, $type); } elseif ($value instanceof jr_cr_node) { $jrprop = $this->JRnode->setProperty($name, $value->JRnode); } else { $jrprop = $this->JRnode->setProperty($name, $value); } if (null === $jrprop) { throw new PHPCR_RepositoryException("Couldn't create new property"); } if ($filename) { unlink($filename); } $property = new jr_cr_property($this, $jrprop); $this->addPropertyToList($property); if ($isNew) { $property->setNew(true); } $property->setModified(true); $this->setModified(true); if ($this->session->cache) { $this->session->cache->clean(Zend_Cache::CLEANING_MODE_ALL); } }
/** * @param $path * @return Java object Input Stream */ public function createJavaInputStream($path) { $handle = fopen($path, "r"); $contents = fread($handle, filesize($path)); fclose($handle); $out = new Java("java.io.ByteArrayOutputStream"); $arr = array(); $strlen = strlen($contents); for ($i = 0; $i < $strlen; $i++) { $val = ord(substr($contents, $i, 1)); if ($val >= 128) { $val = $val - 256; } $arr[] = $val; } $out->write($arr); $value = new Java("java.io.ByteArrayInputStream", $out->toByteArray()); return $value; }