Creating a new document from a file path

When i try to read a RTF document from file path and convert it into html document, it is taking longer time.
This line of code is taking some minutes to load document when the file is having some images.
Is there other way for that. Here is the code I m using for it. It works fine when document doesnot have any images in it and file size is small.

Document doc = new Document(filePath);
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, SaveFormat.HTML);

ByteArrayInputStream in = new ByteArrayInputStream(dstStream.toByteArray());

byte[] buf = new byte[4096];
int bytesRead = 0;
while ((bytesRead = in.read(buf)) > 0)
    htmlString += new String(buf, 0, bytesRead);

@dineshreddydepa,

Thanks for your inquiry. Please note that performance and memory usage all depend on complxity and size of the documents you are generating.

When a document is loaded into Aspose.Words DOM, it takes some time to load all resources first time. Upon the first time, Aspose.Words initializes common static resources, for example fonts installed on the PC. The second call of “new Document()” will not cause class loading.

Please ZIP and attach your input RTF document here for testing. We will investigate the issue on our side and provide you more information.

Please share the JDK version that you are using at your end. Thanks for your cooperation.

sample.zip (223.4 KB)

Initaillay we are reading RTF from that file path and converting into a string here is the code for that.

Document doc = new Document(filePath);
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, SaveFormat.RTF);

ByteArrayInputStream in = new ByteArrayInputStream(dstStream.toByteArray());

byte[] buf = new byte[4096];
int bytesRead = 0;
while ((bytesRead = in.read(buf)) > 0)
    rtfString += new String(buf, 0, bytesRead);

and again we we will convert to html string in some other places .here is the code for that.

ByteArrayInputStream is = new ByteArrayInputStream(rtfString.getBytes());
Document doc = new Document(is);
HtmlSaveOptions saveOptions = new com.aspose.words.HtmlSaveOptions(SaveFormat.HTML);
saveOptions.setPrettyFormat(true);
saveOptions.setExportImagesAsBase64(true);

ByteArrayOutputStream os = new ByteArrayOutputStream();
doc.save(os, saveOptions);

htmlString = new String(os.toByteArray(), StandardCharsets.UTF_8);

In need some optimization because it is taking lot of time.

@dineshreddydepa,

Thanks for sharing the detail. We are investigating this issue and will get back to you soon.

@dineshreddydepa,

Thanks for your patience. We have tested the scenario using latest version of Aspose.Words for Java 18.1 and have not found the shared issue. Please use Aspose.Words for Java 18.1.

The following Java code takes time and it is not related to Aspose.Words for Java.

byte[] buf = new byte[4096];
int bytesRead = 0;
while ((bytesRead = in.read(buf)) > 0)
    rtfString += new String(buf, 0, bytesRead);

Please execute the following code example to check the performance of Aspose.Words for Java.

String rtfString = "";
Document doc = new Document(MyDir + "sample.rtf");
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, SaveFormat.RTF);

long endTime = System.currentTimeMillis();
System.out.println("Total execution time: " + (endTime - startTime) + "ms");

endTime = System.currentTimeMillis();
System.out.println("Reading bytes data: " + (endTime - startTime) + "ms");

InputStream in = new FileInputStream(MyDir + "sample.rtf");
startTime = System.currentTimeMillis();
Document doc2 = new Document(in);
HtmlSaveOptions saveOptions = new com.aspose.words.HtmlSaveOptions(SaveFormat.HTML);
saveOptions.setPrettyFormat(true);
saveOptions.setExportImagesAsBase64(true);

ByteArrayOutputStream os = new ByteArrayOutputStream();
doc2.save(os, saveOptions);
endTime = System.currentTimeMillis();
System.out.println("Total execution time: " + (endTime - startTime) + "ms");

I have tried the code which you gave. Still its not coming. I have used the following code

String htmlString = "";
long startTime = System.currentTimeMillis();
InputStream in = new FileInputStream(filePath);
long endTime = System.currentTimeMillis();
System.out.println("Total execution time1: " + (endTime - startTime) + "ms");
Document doc2 = new Document(in);
endTime = System.currentTimeMillis();
System.out.println("Total execution time2: " + (endTime - startTime) + "ms");
HtmlSaveOptions saveOptions = new com.aspose.words.HtmlSaveOptions(SaveFormat.HTML);
saveOptions.setPrettyFormat(true);
saveOptions.setExportImagesAsBase64(true);

ByteArrayOutputStream os = new ByteArrayOutputStream();
doc2.save(os, saveOptions);
endTime = System.currentTimeMillis();
System.out.println("Total execution time3: " + (endTime - startTime) + "ms");
htmlString = new String(os.toByteArray(), StandardCharsets.UTF_8);

This is the output on my console:

Total execution time1: 0ms
email failed for System_Correspond 712386

When I tried to upload the same document in which i reduced the size of the image in it from 16 MB to 2Mb then it is working fine .This is the output on my console.

Total execution time1: 0ms
Total execution time2: 55ms
Total execution time3: 98ms
email failed for System_Correspond 712386

The problem is when there are images with larger size then we are not coming out of
Document doc2 = new Document(in);.

Good afternoon,

I have tried the code you have provided to us and we keep getting the same error over and over.
Could you provide me a different option or support to resolve this issue in our end?
If you are not able to provide any other options, my manager would like to escalate this issue with your superior and seek for a different solution.

Thank you for your time and cooperation.

@dineshreddydepa,

Thanks for your inquiry. We suggest you please use the latest version of Aspose.Words for Java 18.1. If you still face problem, please share your working environment e.g. operating system JDK version etc. We will investigate this issue on our side and provide you more information on this.

I’m using the latest version Aspose.Words 18.1. We are using java 7, 32-bit version and windows 10 Operating system. Still same problem.

Hello,
Can I get your superior email id ? My manager would like to speak with him about this issue.
Thanks!

@dineshreddydepa,

Thanks for sharing the detail.

We have tested the scenario on the same environment for shared RTF. We have not found the shared issue. It seems that you are using different document. Moreover, please make sure that you are facing issue with Aspose.Words’ code. If you are using different document, please share it here for further testing. Thanks for your cooperation.

We only provide support via forums. Please post your queries in Aspose.Words forum.

@dineshreddydepa,

We have tested again the same scenario using latest version of Aspose.Words for Java 18.2 with following code example. We have not faced any issue. We have tested this scenario at Windows 10 using jdk1.7.0_80 (32-bit). Please check the attached image for test results. Test Results.png (57.5 KB)

String htmlString = "";
long startTime = System.currentTimeMillis();
InputStream in = new FileInputStream(MyDir + "sample.rtf");
long endTime = System.currentTimeMillis();
System.out.println("Total execution time1: " + (endTime - startTime) + "ms");
Document doc2 = new Document(in);
endTime = System.currentTimeMillis();
System.out.println("Total execution time2: " + (endTime - startTime) + "ms");
HtmlSaveOptions saveOptions = new com.aspose.words.HtmlSaveOptions(SaveFormat.HTML);
saveOptions.setPrettyFormat(true);
saveOptions.setExportImagesAsBase64(true);

ByteArrayOutputStream os = new ByteArrayOutputStream();
doc2.save(os, saveOptions);
endTime = System.currentTimeMillis();
System.out.println("Total execution time3: " + (endTime - startTime) + "ms");
htmlString = new String(os.toByteArray(), StandardCharsets.UTF_8);

You are saving RTF to HTML string. We suggest you please use Node.toString method as shown below. Hope this helps you.

InputStream in = new FileInputStream(MyDir + "sample.rtf");
long endTime = System.currentTimeMillis();
System.out.println("Total execution time1: " + (endTime - startTime) + "ms");
Document doc2 = new Document(in);
endTime = System.currentTimeMillis();
System.out.println("Total execution time2: " + (endTime - startTime) + "ms");
HtmlSaveOptions saveOptions = new com.aspose.words.HtmlSaveOptions(SaveFormat.HTML);
saveOptions.setPrettyFormat(true);
saveOptions.setExportImagesAsBase64(true);

String htmlString = doc2.toString(saveOptions);

If you still face problem, please share the complete JDK version that you are using. Please also share the stack trace that you are getting.

If you are facing java.lang.OutOfMemoryError exception, we suggest you please increase the heap size at your end and use SaveOptions.MemoryOptimization property as shown below.

saveOptions.setMemoryOptimization(true);
String htmlString = doc2.toString(saveOptions);