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

Free Support Forum - aspose.com

Getting invalid DOC and PDF files after upgrading

We have been running 14.6 version of Aspose Words for Java and I just tried upgrading to a newer version and our process no longer generates files. I tried 14.12, 15.3(which was recommended in response to a bug we reported awhile back), 15.8, and 15.11 all are giving the same results. I can go back to 14.6 and everything works correctly again. I have attached a sample RTF file that our software creates and the function that we use to convert the system created RTF code over. The function is being passed the (encoded rtf string, pdf, none) in the call to the function below, but when the attached RTF is ran through it the pdf gives an error message of Error: the document is damaged and cannot be repaired. Adobe Reader could not open because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn’t correctly decoded). and if we output a Doc format we get a blank doc. So any assistance you can give would be appreciated as I would like to update, but at this point it seems like that isn’t possible.

import java.awt.Color;
import java.io.*;
import com.aspose.words.;
import org.apache.commons.codec.binary.*;

public class rtfStringConvert {

    public String doRtfStringConvert (String rtfString, String newFormat, String note) throws Exception{
        // License stuff
        License license = new License();
        license.setLicense("/var/java/Aspose.Words.lic");
        // Convert the string into an array of bytes and pass it into a new memory stream
        InputStream rtfStream = new ByteArrayInputStream(Base64.decodeBase64(rtfString));
        // This may be useful in the future but disabling for now
        //FileFormatInfo info = FileFormatUtil.detectFileFormat(rtfStream);
        //if (info.getLoadFormat() != LoadFormat.RTF) {
        //throw new Exception("The RTF string passed is invalid.");
        // System.err.println("***invalid format***");
        //}
        // rtfStream.reset();
        // Create new Document from inputStream
        Document rtfDoc = new Document(rtfStream);
        // Set Fonts
        FontSettings.setFontsFolder("/var/java/fonts/", false);
        // Create output stream
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //Adds watermark for blank forms
        if ( !note.equals("none") ) {
            // Create TEXT_BOX to hold text
            Shape noteBox = new Shape(rtfDoc, ShapeType.TEXT_BOX);
            noteBox.setWrapType(WrapType.NONE);
            noteBox.setWidth(550);
            noteBox.setHeight(35);
            noteBox.setTop(-35); //Sets vertical position; should be equal to or greater than height
            noteBox.setHorizontalAlignment(HorizontalAlignment.CENTER);
            noteBox.setStrokeColor(Color.WHITE); // make border/stroke blend in with background
            // Create paragraph styles for watermark
            Style noteParaStyle = rtfDoc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1");
            noteParaStyle.getFont().setName("Arial");
            noteParaStyle.getFont().setSize(9);
            noteParaStyle.getFont().setColor(Color.RED);
            // Let’s create a new paragraph for the textbox manually and align it in the center. Make sure we add the new nodes to the textbox as well.
            noteBox.appendChild(new Paragraph(rtfDoc));
            Paragraph notePara = noteBox.getFirstParagraph();
            notePara.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
            notePara.getParagraphFormat().setStyle(noteParaStyle);
            // Add some text to the paragraph.
            Run run = new Run(rtfDoc);
            run.setText(note);
            notePara.appendChild(run);
            // Append the textbox to the first paragraph in the body.
            rtfDoc.getFirstSection().getBody().getFirstParagraph().appendChild(noteBox);
        }
        // Create TOC if it exists
        rtfDoc.updateFields();
        // Convert to the desired format
        if ( newFormat.equals("pdf") ) {
            //If file is water marked then we want to set permissions so they can only print. JD - 7-15-2014
            if ( !note.equals("none") ) {
                PdfSaveOptions saveOptions = new PdfSaveOptions();
                // Create encryption details and set owner password.
                PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails("", "Timbob@1", PdfEncryptionAlgorithm.RC_4_128);
                // Start by disallowing all permissions.
                encryptionDetails.setPermissions(PdfPermissions.DISALLOW_ALL);
                // Extend permissions to allow editing or modifying annotations.
                encryptionDetails.setPermissions(PdfPermissions.PRINTING);
                saveOptions.setEncryptionDetails(encryptionDetails);
                rtfDoc.save(outStream, saveOptions);
            } else {
                rtfDoc.save(outStream, SaveFormat.PDF);
            }
        } else if ( newFormat.equals("doc") ) {
            rtfDoc.save(outStream, SaveFormat.DOC);
        } else if ( newFormat.equals("docx") ) {
            rtfDoc.save(outStream, SaveFormat.DOCX);
        } else if ( newFormat.equals("html") ) {
            rtfDoc.save(outStream, SaveFormat.HTML);
        } else if ( newFormat.equals("png") ) {
            rtfDoc.save(outStream, SaveFormat.PNG);
        }

        // Convert outStream data to Base64 String so we can pass back to Perl
        String outString = new String(Base64.encodeBase64Chunked(outStream.toByteArray()));
        return outString;
    }
}

Hi there

Thanks for your inquiry. We have tested the scenario and have not found the shared issue while using Aspose.Words for Java 15.11.0. Please make sure that you are using Aspose.Words for Java 15.11.0.

If you still face problem, please share following detail for investigation purposes.

  • Please attach the output Word and Pdf files that shows the undesired behavior.
  • Please share the value of ‘note’ string. We used simple empty string for testing.
  • Please share your environment detail e.g Operating System, Jdk version etc

I re-downloaded and replace my 14.6 version with the 15.11.0 version as you directed and I am still getting the issue. I have attached a sample of rtf data that is being passed into the function along with the pdf file that is not properly opening. We are using Perl as our interface and I have included the full code with the Perl and Java code visible. The value for the note string that is being passed is ‘none’ though I tried with a different string ‘test this’ and I had the same results on the final file output. Again all the below if working fine with 14.6 version of your software. Any assistance would be appreciated.

As far as environment we are running:

Redhat Server 5.7
Perl 5.8.8
Java 1.7.0
use MIME::Base64;
use Inline (
Java => <<‘END_OF_JAVA_CODE’,
import java.awt.Color;
import java.io.*;
import com.aspose.words.;
import org.apache.commons.codec.binary.*;

public class rtfStringConvert {

    public String doRtfStringConvert (String rtfString, String newFormat, String note) throws Exception{
        // License stuff
        License license = new License();
        license.setLicense("/var/java/Aspose.Words.lic");
        // Convert the string into an array of bytes and pass it into a new memory stream
        InputStream rtfStream = new ByteArrayInputStream(Base64.decodeBase64(rtfString));
        // This may be useful in the future but disabling for now
        //FileFormatInfo info = FileFormatUtil.detectFileFormat(rtfStream);
        //if (info.getLoadFormat() != LoadFormat.RTF) {
        //throw new Exception("The RTF string passed is invalid.");
        // System.err.println("***invalid format***");
        //}
        // rtfStream.reset();
        // Create new Document from inputStream
        Document rtfDoc = new Document(rtfStream);
        // Set Fonts
        FontSettings.setFontsFolder("/var/java/fonts/", false);
        // Create output stream
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //Adds watermark for blank forms
        if ( !note.equals("none") ) {
            // Create TEXT_BOX to hold text
            Shape noteBox = new Shape(rtfDoc, ShapeType.TEXT_BOX);
            noteBox.setWrapType(WrapType.NONE);
            noteBox.setWidth(550);
            noteBox.setHeight(35);
            noteBox.setTop(-35); //Sets vertical position; should be equal to or greater than height
            noteBox.setHorizontalAlignment(HorizontalAlignment.CENTER);
            noteBox.setStrokeColor(Color.WHITE); // make border/stroke blend in with background
            // Create paragraph styles for watermark
            Style noteParaStyle = rtfDoc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1");
            noteParaStyle.getFont().setName("Arial");
            noteParaStyle.getFont().setSize(9);
            noteParaStyle.getFont().setColor(Color.RED);
            // Let’s create a new paragraph for the textbox manually and align it in the center. Make sure we add the new nodes to the textbox as well.
            noteBox.appendChild(new Paragraph(rtfDoc));
            Paragraph notePara = noteBox.getFirstParagraph();
            notePara.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
            notePara.getParagraphFormat().setStyle(noteParaStyle);
            // Add some text to the paragraph.
            Run run = new Run(rtfDoc);
            run.setText(note);
            notePara.appendChild(run);
            // Append the textbox to the first paragraph in the body.
            rtfDoc.getFirstSection().getBody().getFirstParagraph().appendChild(noteBox);
        }

        // Create TOC if it exists
        rtfDoc.updateFields();
        // Convert to the desired format
        if ( newFormat.equals("pdf") ) {
            //If file is water marked then we want to set permissions so they can only print. JD - 7-15-2014
            if ( !note.equals("none") ) {
                PdfSaveOptions saveOptions = new PdfSaveOptions();
                // Create encryption details and set owner password.
                PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails("", "Timbob@1", PdfEncryptionAlgorithm.RC_4_128);
                // Start by disallowing all permissions.
                encryptionDetails.setPermissions(PdfPermissions.DISALLOW_ALL);
                // Extend permissions to allow editing or modifying annotations.
                encryptionDetails.setPermissions(PdfPermissions.PRINTING);
                saveOptions.setEncryptionDetails(encryptionDetails);
                rtfDoc.save(outStream, saveOptions);
            } else {
                rtfDoc.save(outStream, SaveFormat.PDF);
            }
        } else if ( newFormat.equals("doc") ) {
            rtfDoc.save(outStream, SaveFormat.DOC);
        } else if ( newFormat.equals("docx") ) {
            rtfDoc.save(outStream, SaveFormat.DOCX);
        } else if ( newFormat.equals("html") ) {
            rtfDoc.save(outStream, SaveFormat.HTML);
        } else if ( newFormat.equals("png") ) {
            rtfDoc.save(outStream, SaveFormat.PNG);
        }
        // Convert outStream data to Base64 String so we can pass back to Perl
        String outString = new String(Base64.encodeBase64Chunked(outStream.toByteArray()));
        return outString;
    }
}
END_OF_JAVA_CODE
CLASSPATH => ‘/var/java/Aspose.Words.jdk16.jar:/var/java/commons-codec-1.6.jar’,
SHARED_JVM => 1,
DIRECTORY => ‘/var/www/html/TempDocfjyrnnvfwsms/’,
);
package AsposeWordsInline;
sub convertRtfString {
my ($rtfString, $format, $note) = @_;
my $inlineClass = new rtfStringConvert();
if (!$note) {
$note = ‘none’;
}
my $newFile = $inlineClass->doRtfStringConvert(MIME::Base64::encode_base64($rtfString), $format, $note);
return MIME::Base64::decode_base64($newFile);
}

I also thought it might be good to attached the expected results of what we are getting when we are using the 14.6 version of your software. Maybe see if something stands out to you on your end the difference between the two pdf outputs. The attached is what I get running all the same code with the only difference being the version of Aspose.Words is 14.6.

Hi there

Thanks for sharing the detail. The shared ‘Aero Mist 401(k) Plan.DeferralAgreement.pdf’ has 0kb file size. Could you please attach this Pdf again along with output Word Doc file here for our reference?

We have tested the scenario at Ubuntu using following code example. There is no issue with output document. Please check attached Pdf document for your kind reference.

Document rtfDoc = new Document(MyDir + "Aero+Mist+401(k)+Plan.DeferralAgreement.rtf");
// Set Fonts
FontSettings.setFontsFolder("/var/java/fonts/", false);
// Create TEXT_BOX to hold text
Shape noteBox = new Shape(rtfDoc, ShapeType.TEXT_BOX);
noteBox.setWrapType(WrapType.NONE);
noteBox.setWidth(550);
noteBox.setHeight(35);
noteBox.setTop(-35); //Sets vertical position; should be equal to or greater than height
noteBox.setHorizontalAlignment(HorizontalAlignment.CENTER);
noteBox.setStrokeColor(Color.WHITE); // make border/stroke blend in with background
                                        // Create paragraph styles for watermark
Style noteParaStyle = rtfDoc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1");
noteParaStyle.getFont().setName("Arial");
noteParaStyle.getFont().setSize(9);
noteParaStyle.getFont().setColor(Color.RED);
// Let's create a new paragraph for the textbox manually and align it in the center. Make sure we add the new nodes to the textbox as well.
noteBox.appendChild(new Paragraph(rtfDoc));
Paragraph notePara = noteBox.getFirstParagraph();
notePara.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
notePara.getParagraphFormat().setStyle(noteParaStyle);
// Add some text to the paragraph.
Run run = new Run(rtfDoc);
run.setText("test this");
notePara.appendChild(run);
// Append the textbox to the first paragraph in the body.
rtfDoc.getFirstSection().getBody().getFirstParagraph().appendChild(noteBox);
// Create TOC if it exists
rtfDoc.updateFields();
PdfSaveOptions saveOptions = new PdfSaveOptions();
// Create encryption details and set owner password.
PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails("", "Timbob@1", PdfEncryptionAlgorithm.RC_4_128);
// Start by disallowing all permissions.
encryptionDetails.setPermissions(PdfPermissions.DISALLOW_ALL);
// Extend permissions to allow editing or modifying annotations.
encryptionDetails.setPermissions(PdfPermissions.PRINTING);
saveOptions.setEncryptionDetails(encryptionDetails);
rtfDoc.save(MyDir + "Out.pdf", saveOptions);
rtfDoc.save(MyDir + "Out.doc", SaveFormat.DOC);

I figured out some of the problem I was having and found another related issue that with the same code and an update. The previous issue was due to my own neglect to update my license key file on my development side which is why no versions after 14.6 were working. I addressed that issue and am able to update to 15.9 with no problems, but 15.11 is still having issues. The sample file you sent showed part of the problem where the text formatting is messed up from what it is on version 15.9 and before. 15.10 & 15.11 seem to be responded the same from what I can tell with the problem. I have attached a couple samples and named them in a manner to show the problem better between the 15.9 and 15.11 versions of the software.

Sample1 - This is the previous RTF file I sent you and was able to get it to generate on my end in both Doc and Pdf formats, but you will see that the formatting gets messed up in the 15.11 version. The 15.9 displays the same as it did in our 14.6 version.

Sample2 - This file will not generate a Doc or Pdf in 15.11 and gives a FileCurruptedException on the console log. I tried opening the RTF sample directly and passing through the code as a data stream both cases produce the same results. The bad sample files are 0 bytes since it fails during the saving process. Both versions are generating properly in 15.9.

If you could look into this issue so we can upgrade to the latest version of the software that would be great. I will be running 15.9 for the time being which at least fixes the previous problem we were having.

Hi Jeremy,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issues at our side. For the sake of correction, we have logged these problems in our issue tracking system as follow:

WORDSNET-12830 : RTF to Doc/Pdf conversion issue with content position

WORDSNET-12831 : Aspose.Words.FileCorruptedException is thrown while loading Rtf

You will be notified via this forum thread once these issues are resolved. We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-12831) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

The issues you have found earlier (filed as WORDSNET-12830) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.