We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Word Save not working

Hi,

I have a multi threaded application which processes multiple word documents in parallel, some times these documents are huge in size. when two threads calls document save() for two different documents object, API is not responding for both the threads. Kindly let me know what is the issue.

Regards,
Gopal
Hi Gopal,
Thanks for your inquiry. Unfortunately, it is difficult to say what the problem is without the documents and code. Could you please attach your input Word documents, you're getting this problem with, here for testing? Also, please share your code as well. I will investigate the issue on my side and provide you more information.
Best regards,

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,

Any update for the below issue.
regards,
Gopal

Hi Donald,


Thanks for your inquiry. We’re working over your query and will get back to you soon.

Best Regards,

Hi Gopal,


Thanks for your inquiry.

While using the latest version of Aspose.Words i.e. 11.2.0, I managed to reproduce this issue on my side. The system hangs during rendering Test.doc to PDF. I have logged this issue in our bug tracking system as WORDSNET-6208. Your request has also been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Moreover, the document you are trying to convert to PDF is simply too large. I would suggest you always use few small documents instead of one huge document.

Please let me know if I can be of any further assistance.

Best Regards,

Hi,

My word output pages are always in 1000+ pages. is there a better way to convert word to PDF.

Like breaking into smaller chunks & converting to PDF & then stitch all the PDF into single PDF.
or
convert many word documents into PDF and make it as single document and then update the TOC, TOT , TOF & page numbers.

regards,
Gopal

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:
http://www.aspose.com/docs/display/pdfnet/Concatenate+PDF+Files

I hope, this will improve the performance.

Best Regards,

Hi,

I need to update TOC, TOT and TOF after merging all the word dos into one single word doc. Can i achieve the same in PDF merging ?

regards,
Gopal

Hi
Gopal,


Thanks for your inquiry and sorry for the delayed response. Sure, once you have merged all the documents into one Word document, you can explicitly update all fields in the document (e.g. rebuild TOC) by using Document.updateFields method.

Regarding your question about Aspose.Pdf, I will move your request in Aspose.Pdf forum. My colleagues from Aspose.Pdf component team will answer you shortly.

Best Regards,

gopalhr:
I need to update TOC, TOT and TOF after merging all the word dos into one single word doc. Can i achieve the same in PDF merging ?
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


In case of any further query, please feel free to contact. We are sorry for your inconvenience.

Hi,

I have another question, when i manipulate a 200+ MB file word document and call document.save(filePath), API is not responding. I have enough free RAM & hard disk while processing. kindly let me know if there is any limitation.

regards,
Gopal

Hi Gopal,


Thanks for your inquiry. Please note that there is no size limit of document you can generate using Aspose.Words. The only limit is the amount of RAM available on your side. Could you please compress your document, you’re getting this problem with, in .rar or .zip format and share it via SkyDrive, DropBox or any free file sharing service. Once we have your input Word document, we will investigate it on our side and provide you more information.

Best Regards,

Hi Gopal,


Thanks for being patient.

Regarding WORDSNET-6208, I like to share that we have now improved the performance of Aspose.Words to a great deal by carrying out improvements in layout engine. We have just released a new version of Aspose.Words 14.1.0 which contains these performance improvements. You can download this version from the following link:
http://www.aspose.com/community/files/72/java-components/aspose.words-for-java/entry524477.aspx

Please note that Aspose.Words builds a ‘page layout’ of the document when you first convert a document to PDF, XPS, image or print it. Roughly, Aspose.Words layouts 10 pages per second; so, the extra amount of time Aspose.Words takes to format a document into pages depends on the number of pages your Word document has.

We always strive to fix performance issues and you are likely to see more improvements in every release from now on. Please let us know if this is acceptable for you so that we can close this issue.

Best regards,

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