Search code examples
pdfadobeasposeaspose.pdf

Adobe breaks stamped PDF when saving as new file / what is difference in Adobe 'save as' vs. Foxit Reader 'save as' feature


I'm reaching out to larger community of developers in seek of help to understand the real cause and possibly finding a fix. I have asked questions from Aspose, and they have also tracked the issue (PDFNET-42880) in their system. I think they are not going to investigate this anytime soon as it is very specific case. And now I am posting this here to ask more details about:

  1. What is difference in Adobe 'save as' vs. Foxit Reader 'save as' vs. Windows Reader 'save as' feature?

  2. Issues with Adobe product that are not so obvious to figure out. I don't even know what to ask :D

Link to their (Aspose) old forum: https://www.aspose.com/community/forums/thread/845549/removing-stamps-fails-after-saving-stamped-file-from-adobe-acrobat.aspx

Case: Created PDF with forms using OpenOffice (version 3.4.0), stamped with Aspose PDF, opened with Adobe Reader DC (or Adobe Acrobat XI), filled, saved as new file. Now this new file is fine, but when I try to remove stamps using Aspose (and replace with new stamp later), this is where things get interesting.

Files that I've tested with: https://1drv.ms/f/s!Auvpijam7a73iDzOqc6wZPuY9l81

  • Stamp_Location.png
  • OoPdfFormExample_WithStamp.pdf
  • OoPdfFormExample_WithStamp_StampRemoved.pdf
  • OoPdfFormExample_WithStamp_SavedFromFoxit.pdf
  • OoPdfFormExample_WithStamp_SavedFromFoxit_StampRemoved.pdf
  • OoPdfFormExample_WithStamp_SavedFromWindowsReader.pdf
  • OoPdfFormExample_WithStamp_SavedFromWindowsReader_StampRemoved.pdf
  • OoPdfFormExample_WithStamp_SavedFromAdobeReader.pdf
  • OoPdfFormExample_WithStamp_SavedFromAcrobat_StampRemoved.pdf

C# code that is used to remove the stamp(s):

/// <summary>
/// Removes stamps from PDF file.
/// </summary>
/// <param name="pdfFile"></param>
private static void RemoveStamps( string pdfFile )
{
    // Create PDF content editor.
    Aspose.Pdf.Facades.PdfContentEditor contentEditor = new Aspose.Pdf.Facades.PdfContentEditor();

    // Open the temp file.
    contentEditor.BindPdf( pdfFile );

    // Process all pages.
    foreach ( Page page in contentEditor.Document.Pages )
    {
        // Get the stamp infos.
        Aspose.Pdf.Facades.StampInfo[] stampInfos = contentEditor.GetStamps( page.Number );

        //Process all stamp infos
        foreach ( Aspose.Pdf.Facades.StampInfo stampInfo in stampInfos )
        {
            // Use try catch so we can output possible error w/out break point.
            try
            {
                contentEditor.DeleteStampById( stampInfo.StampId );
            }
            catch ( Exception e )
            {
                Console.WriteLine( e );
            }
        }
    }

    // Save changes to the temp file.
    contentEditor.Save( StampRemovedPdfFile );
}

Using Adobe: The process of removing stamp works fine, but trying to open the file will end up having an issue with the file.

"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."

EDIT: After testing more, and just opening file to Aspose, and saving it without modifications, that didn't break the file, only once the stamp was removed with Aspose method it was broken.

Using Foxit: Only difference in the process is that opening the file to Foxit Reader and save form there. The stamp is removed and file is fine, works with any PDF reader.

Using Windows (10) Reader: Only difference in the process is that opening the file to Windows Reader and save from there. The stamp is removed and file is fine, works with any PDF reader.


Solution

  • Ok - The thing you are referring to is not a stamp annotation. It's an XObject that gets drawn into the page content. Why Aspose refers to it as a Stamp is... well... a mystery. When you remove the "stamp" (not a stamp) Aspose seems to be removing the XObject but not the instructions to draw it from the page Contents stream... that's why you're getting the error in Acrobat. The other applications are more permissive with bad PDF and my guess is when they write out the file, they are removing references to non-existent objects. You can make Acrobat attempt to fix problems like this by selecting Save As Optimized PDF. However, you are far better off removing the drawing instruction in addition to the XObject.

    Because of the way you've created the file and added the "stamp", your page content stream is an array of streams. Remove the last item in the array, which is the instruction to draw the XObject, and you file will work without errors in all the viewers. Note: It won't always be the case that the last item in the content array will be your stamp. It's just that your stamp is the last thing to get drawn so it goes at the end.

    If your intention is to "replace" the "stamp", you'll want to do so by removing the XObject as you are doing now, then remove the instruction, then add the new "stamp".