Problem with Adobe Reader after we replace some text with Aspose library

Hello, we use code sample from Developer Guide to replace text token in PDF (which is generated from MS Word). We update PDF and save it. It usually open correctly. But if we stream PDF from web server to browser IE 11 Acrobat Reader window is opened and randomly it shows general error dialog:


Adobe Reader
An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem.

Problem is that this happens randomly. This did not happen before we started to update our PDF with Aspose PDF. We use latest version 11.5.0. We set font ExpertSans-Regular which is embedded and isSubset returns true.

Is there any way we could diagnose problem? I attach PDF sample.
Test_publication_3.pdf is generated from MS Word
Barclays_Test_Credit_Publication_123.pdf is created when we replace text using Aspose PDF. It sometimes causes problem as described above.

Note: I also filed separate report about growing file size during this operation which was accepted as bug here. Is it possible it could be somehow connected? PDF file size grows after simple replacement

Here is our sample code based on Developer Guide:
PdfReplacementResult result = new PdfReplacementResult();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
Document pdfDocument = new com.aspose.pdf.Document(documentStream);
for (String placeholderToReplace : textReplacementMap.keySet()) {
String textReplacement = textReplacementMap.get(placeholderToReplace);
TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber(placeholderToReplace);
TextSearchOptions textSearchOptions = new com.aspose.pdf.TextSearchOptions(true);
textFragmentAbsorber.setTextSearchOptions(textSearchOptions);
pdfDocument.getPages().accept(textFragmentAbsorber);
com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
for (com.aspose.pdf.TextFragment textFragment : (Iterable<com.aspose.pdf.TextFragment>) textFragmentCollection) {
String foundText = textFragment.getText();
// Replace only when replacement text is different from original text
if (!foundText.equals(textReplacement)) {
textFragment.setText(textReplacement);
// Set other font for JP
Font font = FontRepository.findFont(“ExpertSans-Regular”);
textFragment.getTextState().setFont(font);
}
}
}
pdfDocument.optimize();
pdfDocument.save(outputStream);

Hi Marek,

Thanks for contacting support. I have tested the scenario using Aspose.Pdf for Java 11.5.0 in Eclipse Juno project running over Windows 7 (x64) with JDK 1.7 and I am unable to notice any issue. During my testing, I have used Arial font as I did not have ExpertSans-Regular over my system. Can you please share some details regarding your working environment so that we can again try replicating the issue in our environment.

mslama2:
Note: I also filed separate report about growing file size during this operation which was accepted as bug here. Is it possible it could be somehow connected? PDF file size grows after simple replacement

We have already managed to reproduce the issue on above stated forum thread and once the problem is resolved, you will be updated in same thread. Furthermore, as both issues are separate, so its better to keep them apart in separate threads.

We are sorry for your inconvenience.

[Java]

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

try {
    Document pdfDocument = new com.aspose.pdf.Document("c:/pdftest/Test_publication_3.pdf");

    String textReplacement = "16 May 2016, 14:21 GMT";

    com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber("12 February 2016, 14:30 GMT");
    com.aspose.pdf.TextSearchOptions textSearchOptions = new com.aspose.pdf.TextSearchOptions(true);

    textFragmentAbsorber.setTextSearchOptions(textSearchOptions);

    pdfDocument.getPages().accept(textFragmentAbsorber);

    com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();

    for (com.aspose.pdf.TextFragment textFragment : (Iterable<com.aspose.pdf.TextFragment>) textFragmentCollection) {
        String foundText = textFragment.getText();

        // Replace only when replacement text is different from original text
        if (!foundText.equals(textReplacement)) {
            textFragment.setText(textReplacement);

            // Set other font for JP
            com.aspose.pdf.Font font = com.aspose.pdf.FontRepository.findFont("Arial");
            textFragment.getTextState().setFont(font);
        }
    }

    pdfDocument.optimize();
    pdfDocument.save("c:/pdftest/TextReplaced_11_5_0.pdf");

} catch (Exception ex) {
    System.out.println(ex);
}

I added our font ExpertSans-Regular as attachment to original post. Please test with this.

FYI: I tested the same scenario with Arial font and it seems ok ie. I cannot reproduce issue I see with Expert Sans Regular font. What I do:

1.Create simple document in MS Word 2007 using only Arial font.
2.Save it as PDF (I also enabled PDF/A mode because without it Word does not embed Arial font subset.)
3.I replace text using Aspose lib and Arial font. I used setSubset(false). (But I must say it generates the same file size even if I ommit setSubset(false).
4.I test output file and found no issues.

From this it looks like issue might be with our font. I attached our font yesterday.

Hi Marek,


Thanks for sharing the resource files.

I have again tested the scenario using recently shared font file and I am still unable to notice any issue. Can you please share some details regarding your working environment, so that we can again try replicating the issue in our environment. We are sorry for this inconvenience.

I tested more and it looks it happens only for Calibri font. When I replace text in Test_publication_3 I get file which fails with Adobe PDF Reader plugin in IE11. Opening in standalone Adobe Reader is ok ie. problem is only in plugin. I keep the same font and the same font size (it returns font size 11.04).


I perform 2 replacements:
“Produced: 12 February 2016, 14:30 GMT” to “Produced: 16 May 2016, 14:21 GMT”
“Released: 16 February 2016, 16:00 GMT” to "Released: 16 May 2016, 14:21 GMT"

For Arial or Expert Sans Regular it is ok. I will test some other replacements to see if it is specific only to this case or not.

We use Win7 and Linux (RH EL). On Linux output file size is a little bit different but result is the same (ie. test with opening file in IE 11 plugin).

Is it possible to enable some logging to see what happens and find out what is difference between output in our and your environment? Today I aslo true with latest Adobe PDF Reader plugin 15.xxx and result is the same. I filed question on Adobe Reader forum but they say I should ask here first so not sure if they will help to diagnose why Reader plugin shows error wiht updated PDF.

Cannot delete

Hi Marek,


Thanks for sharing the details.

In my earlier attempt, I tried viewing the resultant PDF documents in Adobe Reader application. However I am working on testing the scenario with Adobe plugin and will let you know.

I have some new info. My colleague tried to open updated PDF file in Adobe Acrobat 9.5.0 on Win7 and he gets following error dialog:

----
Acrobat was unable to make this document accessible because of the following error: Bad PDF; error in processing fonts. [1]

Please note that some pages of this document may have beed changed.
Because of this failure, you are advised not to save these changes.
----

Hi Marek,


Thanks for sharing the details.

I am trying to replicate the issue using above stated Adobe Reader version and will get back to you soon.

After investigation I found way how to reproduce locally. It has the same cause as in issue I reported Problem with PDF after adding image


It happens when I set http header response.setHeader(“Accept-ranges”, “bytes”); when I stream PDF file to plugin. I will also attach maven project (JettyTest.zip) which uses Jetty to stream file to plugin.
In test you must update path to actual file on filesystem in FileHandler.java. Sample handles 2 URLs: localhost:8080 this page contains link to file localhost:8080/file which opens PDF file in plugin.

Note: Jetty 9 used in test requires JDK8.

Hi Marek,


Thanks for sharing the details.

As shared in other forum thread, we are working on replicating the issue in our environment using latest information and will get back to you soon.