Overwriting pdf

Hi, I’m using aspose to convert some files from several different other formats into .pdf in this case. Up to this point, it’s all good. The problem comes when after converting the documents whith aspose.words I try to optimize the size of the images within those .pdf using aspose.pdf and its OptimizationOptions. In any case, to put it simple, the issue is that whenever I try to open a file convert it, then save it to a path and then load it into another document, optimize it and finally save it again overwriting the first one aspose does strange things like saving a blank file. However if instead the second time I save the document to a different location the process it’s done correctly. I’ve looked for a method to close() whichever stream aspose creates but I haven’t seen any, don’t know if this is done automatically or not or if I’m doing something wrong.

As an example code, if I do this aspose creates a blank file:
com.aspose.words.Document doc = new com.aspose.words.Document(“c:/users/prog2/desktop/trololo.rtf”);
doc.save(“c:/users/prog2/desktop/trololo.pdf”);
com.aspose.pdf.Document doc1 = new com.aspose.pdf.Document(“c:/users/prog2/desktop/trololo.pdf”);
doc1.save(“c:/users/prog2/desktop/trololo.pdf”);
But if I do this, it works fine:
com.aspose.words.Document doc = new com.aspose.words.Document(“c:/users/prog2/desktop/trololo.rtf”);
doc.save(“c:/users/prog2/desktop/trololo.pdf”);
com.aspose.pdf.Document doc1 = new com.aspose.pdf.Document(“c:/users/prog2/desktop/trololo.pdf”);
doc1.save(“c:/users/prog2/desktop/tralala.pdf”);

I’ve also tried to put some wait times between to make sure that instructions do not step each other or something but still nothing.

Hi Sergio,


Thanks for your inquiry. We are looking into it and will update you soon.

Best Regards,

Hi Sergio,


You may consider using close(…) method of Document class to free the resources being hold by com.aspose.pdf.Document instance. Furthermore, its recommended to save the output in separate file, instead we load the contents from one file and then try overwriting the contents in same document.

Hi Nayyer,


I don’t think I quite understood you, what exactly do you mean by using the close(…) method? I tried something like:

com.aspose.words.Document doc = new com.aspose.words.Document(“c:/users/prog2/desktop/trololo.rtf”);
doc.save(“c:/users/prog2/desktop/trololo.pdf”);
doc.close();
com.aspose.pdf.Document doc1 = new com.aspose.pdf.Document(“c:/users/prog2/desktop/trololo.pdf”);
doc1.save(“c:/users/prog2/desktop/trololo.pdf”);

But the close() method doesn’t exist for the Document object, at least not the way I’m using it, don’t know if there’s some other way to close the stream, sorry. Well maybe something I just thought while writing this. Maybe I could open the stream manually and pass it to the aspose document object instead of passing the path to the file, then I guess my code would be the owner of the stream and then I should be able to close it, I guess, don’t really know. But this would be a simple workaround not an actual solution. Regarding saving the output in a separate file… I need to overwrite the first file since I don’t want it lingering around there and since I can’t delete it either for the same reason I can’t overwrite it.

Hi Sergio,

Yes, it is better to save RTF to PDF as stream and then load the PDF from stream instead of loading from the disk. This way the PDF file will be saved to disk only once and you will not see this issue. Following code can be used for this purpose.

com.aspose.words.Document doc = new com.aspose.words.Document("c:/users/prog2/desktop/trololo.rtf");

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />

doc.save(byteArrayOutputStream, com.aspose.words.SaveFormat.PDF);

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

com.aspose.pdf.Document doc1 = new com.aspose.pdf.Document(byteArrayInputStream);

doc1.save("c:/users/prog2/desktop/trololo.pdf");

Best Regards,

Sergio PC:
Hi, I’m using aspose to convert some files from several different other formats into .pdf in this case. Up to this point, it’s all good. The problem comes when after converting the documents whith aspose.words I try to optimize the size of the images within those .pdf using aspose.pdf and its OptimizationOptions. In any case, to put it simple, the issue is that whenever I try to open a file convert it, then save it to a path and then load it into another document, optimize it and finally save it again overwriting the first one aspose does strange things like saving a blank file.
Hi Sergio,

Thanks for sharing the details.

I
have tested the scenario and I am able to reproduce the same problem. For the
sake of correction, I have logged it in our issue tracking system as PDFNEWJAVA-34790. We
will investigate this issue in details and will keep you updated on the status
of a correction. <o:p></o:p>

We apologize for your inconvenience.

The issues you have found earlier (filed as PDFNEWJAVA-34790) have been fixed in Aspose.Pdf for Java 10.4.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.