Hi,
Hi,
Please find below the sample program and the doc sample file attached for your testing.
import com.aspose.words.;
import java.io.;
import java.util.Date;
public class TestAsposeSave {
public static void main(String[] args) throws Throwable {
new TestAsposeSave().loadASPOSELicense();
new MergeDoc().start();
new ConvertPDF().start();
}
public static class MergeDoc extends Thread {
public void run(){
try {
Document completeDocument = getNewDocument();
Document asposeDocument = getDocument(“d:\Document1.doc”);
appendDoc(completeDocument, asposeDocument, true);
System.out.println(“merging doc 1 over”);
asposeDocument = getDocument(“d:\Document2.doc”);
appendDoc(completeDocument, asposeDocument, true);
System.out.println(“merging doc 2 over”);
asposeDocument = getDocument(“d:\Document3.doc”);
appendDoc(completeDocument, asposeDocument, true);
System.out.println(“merging doc 3 over”);
DocumentBuilder docBuilder = getDocBuilder(completeDocument);
insertReportIdentifier(docBuilder, “abcd”);
System.out.println(“TOC Update Started.”);
insertTOCAtBookMark(completeDocument, “TOC”, “Table of Contents”);
System.out.println(“TOC Update over”);
System.out.println(“Before saving Document :”+ new Date());
saveDocument(completeDocument, “d:\Document.doc”);
System.out.println(“After saving Document :”+ new Date());
} catch (Throwable throwable) {
throwable.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
public static class ConvertPDF extends Thread {
public void run(){
try {
InputStream blobInputStream = new FileInputStream(“d:\Test.doc”);
System.out.println(“Loading doc file for PDF conversion complete.”);
PdfSaveOptions saveOpt = new PdfSaveOptions();
//Diaply TOC in word as bookmarks up to level(section level) 9
saveOpt.setHeadingsOutlineLevels(9);
Document doc = new Document(blobInputStream);
blobInputStream.close();
System.out.println(“Conversion of PDF started.”);
ByteArrayOutputStream pdfOS = new ByteArrayOutputStream();
doc.save(pdfOS, saveOpt);
System.out.println(“Conversion of PDF Over.”);
FileOutputStream fos = new FileOutputStream(“d:\Test.PDF”);
fos.write(pdfOS.toByteArray());
fos.close();
pdfOS.close();
} catch (Throwable throwable) {
throwable.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
/
* Returns a reference to a blank document
*
* @return Document
* @throws Exception
*/
public static Document getNewDocument() throws Exception {
Document newDoc = new Document();
return newDoc;
}
/
* Gets a document for a given file path
*
* @param filePath
* @return Document
* @throws Exception
*/
public static Document getDocument(String filePath) throws Exception {
Document newDoc = new Document(filePath);
return newDoc;
}
/
* Converts Word Output Stream into Pdf Input Stream
*
* @param blobInputStream
* @return is ByteArrayInputStream
* @throws Exception
*/
public static InputStream processPdfStreamFromWordStream(InputStream blobInputStream) throws Exception{
PdfSaveOptions saveOpt = new PdfSaveOptions();
//Diaply TOC in word as bookmarks up to level(section level) 9
saveOpt.setHeadingsOutlineLevels(9);
Document doc = new Document(blobInputStream);
blobInputStream.close();
ByteArrayOutputStream pdfOS = new ByteArrayOutputStream();
doc.save(pdfOS, saveOpt);
blobInputStream = new ByteArrayInputStream(pdfOS.toByteArray());
pdfOS.close();
return blobInputStream;
}
/
* Saves the given section document
*
* @param document
* @param filePath
*/
public static void saveDocument(Document document, String filePath) throws Exception {
if (null != document) {
document.save(filePath);
}
}
/
* Inserts the given Report Identifier as footer in the given document
*
* @param builder
* @param reportIdentifier
* @return DocumentBuilder
* @throws Throwable
*/
public static DocumentBuilder insertReportIdentifier(DocumentBuilder builder, String reportIdentifier) throws Throwable {
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.write(reportIdentifier);
return builder;
}
/
* Appends a document to another.
*
* @param dstDoc – Destination document
* @param srcDoc – Source document
* @param includeSection - if true the sections from srcDoc will be copied as it is, else only the internal nodes will be copied
* @throws Throwable
*/
public static void appendDoc(Document dstDoc, Document srcDoc,
boolean includeSection) throws Throwable {
// Loop through all sections in the source document.
// Section nodes are immediate children of the Document node so we can
// just enumerate the Document.
if (includeSection) {
for (Section srcSection : srcDoc.getSections()) {
Node dstNode = dstDoc.importNode(srcSection, true,
ImportFormatMode.USE_DESTINATION_STYLES);
dstDoc.appendChild(dstNode);
}
} else {
//find the last paragraph of the last section
Node node = dstDoc.getLastSection().getBody().getLastParagraph();
if (node == null) {
node = new Paragraph(srcDoc);
dstDoc.getLastSection().getBody().appendChild(node);
}
if ((node.getNodeType() != NodeType.PARAGRAPH)
& (node.getNodeType() != NodeType.TABLE)) {
throw new Exception(“Use appendDoc(dstDoc, srcDoc, true) instead of appendDoc(dstDoc, srcDoc, false)”);
}
insertDocumentAfterNode(node, dstDoc, srcDoc);
}
}
/
* Inserts content of the external document after the specified node.
* Section breaks and section formatting of the inserted document are
* ignored. Node in the destination document
* after which the content should be inserted. This node should be a block
* level node (paragraph or table).
*/
public static void insertDocumentAfterNode(Node insertAfterNode, Document mainDoc,
Document srcDoc) throws Throwable {
// Make sure that the node is either a pargraph or table.
if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH)
& (insertAfterNode.getNodeType() != NodeType.TABLE))
throw new Exception(“The destination node should be either a paragraph or table.”);
//We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.getParentNode();
//Remove empty paragraphs from the end of document
while (null != srcDoc.getLastSection().getBody().getLastParagraph() &&
!srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes()) {
srcDoc.getLastSection().getBody().getLastParagraph().remove();
}
NodeImporter importer = new NodeImporter(srcDoc, mainDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
//Loop through all sections in the source document.
int sectCount = srcDoc.getSections().getCount();
for (int sectIndex = 0; sectIndex < sectCount; sectIndex++) {
Section srcSection = srcDoc.getSections().get(sectIndex);
//Loop through all block level nodes (paragraphs and tables) in the body of the section.
int nodeCount = srcSection.getBody().getChildNodes().getCount();
for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++) {
Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex);
Node newNode = importer.importNode(srcNode, true);
dstStory.insertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
/
* Returns a reference to a builder for a given document
*
* @param document
* @return DocumentBuilder
* @throws Exception
*/
public static DocumentBuilder getDocBuilder(Document document) throws Exception {
DocumentBuilder newDocBuilder = new DocumentBuilder(document);
return newDocBuilder;
}
private void loadASPOSELicense() throws Exception {
System.out.println(this.getClass().getName());
InputStream asposeLicW = this.getClass().getClassLoader().
getResourceAsStream(“com/ags/resources/Aspose.Total.Java.lic”);
com.aspose.words.License licenseW = new com.aspose.words.License();
licenseW.setLicense(asposeLicW);
InputStream asposeLicC = this.getClass().getClassLoader().
getResourceAsStream(“com/ags/resources/Aspose.Total.Java.lic”);
com.aspose.cells.License licenseC = new com.aspose.cells.License();
licenseC.setLicense(asposeLicC);
}
/**
* This method inserts a TOC field at the bookmark
* If the bookmark name is null the TOC is added at the begining of the Document
* Standard TOC field codes are used \o “1-3” \h \z \u
*
* @param document Document
* @param bookMarkName - name of the bookmark
* @param heading – heading to put as the TOC heading
* @throws Throwable Throwable
*/
public static void insertTOCAtBookMark(Document document, String bookMarkName, String heading) throws Throwable {
if (document == null) return;
//create a builder
DocumentBuilder builder = new DocumentBuilder(document);
// delete the content of the bookmark before inserting.
Bookmark bm = document.getRange().getBookmarks().get(bookMarkName.trim());
if (bm != null) {
bm.setText("");
}
if (bookMarkName != null && bookMarkName.trim().length() > 0) {
//move to the bookmark
boolean moveResult = builder.moveToBookmark(bookMarkName);
//if bookmark not found move to begining of document
if (!moveResult) {
builder.moveToDocumentStart();
}
} else {
builder.moveToDocumentStart();
}
//if a heading is needed add it here
if (heading != null && heading.trim().length() > 0) {
builder.getFont().setBold(true);
builder.writeln(heading);
//reset the font
builder.setBold(false);
}
//now add the TOC
builder.insertTableOfContents("\o “1-3” \h \z \u");
document.getRange().updateFields();
}
}
regards,
Gopal
Guys,
Hi Donald,
Hi Gopal,
Hi,
Hi Gopal,
Thanks for the additional information. Combining small Word documents into one huge document is what not recommended in Aspose.Words. As an option, you can try converting each small document to PDF format and then by using Aspose.PDF merge these small files into a single big PDF. For more information, please read the following link:
I hope, this will improve the performance.
Best Regards,
Hi,
Hi
Gopal,
Hi Gopal,
Sorry for the delayed response.
I am a representative from Aspose.Pdf product family team. Please note that we have a product named Aspose.Pdf.Kit for Java which provides the capability to manipulate/edit the existing PDF documents and it also supports the capability to render Pdf documents into XPS as well as Image formats. Now concerning to your requirement or updating TOC, TOT and TOF, I am afraid currently this feature is not supported.
Currently Aspose.Pdf.Kit for Java supports the feature to replace the text inside PDF document but it does not support the feature to update existing hyper-links in the PDF document. For the sake of correction, this requirement is already logged in our issue tracking system as PDFKITJAVA-21234 in our issue tracking system. Our development team is working on supporting this feature and as soon as we have some updates regarding its implementation, we would be more than happy to update you with the status of correction. Please be patient and spare us little time. We are sorry for your inconvenience.
Besides this, if you need to create a TOC after merging the PDF files, you may consider following the approach to add a blank page after the title page, and programatically add the hyperlinks towards the pages where you would like to direct the TOC items. Please visit the following links for further details on
- Manipulate text and images in an existing PDF File
- Setting Font Attributes While Replacing Text
- Getting Font Size of the Text In the PDF File
- Manipulate Text Inside a Rectangle
- Working with the PdfContentEditor class
In case of any further query, please feel free to contact. We are sorry for your inconvenience.
Hi,
Hi Gopal,
Hi Gopal,
The issues you have found earlier (filed as WORDSNET-6208) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by mudassir.raza