2

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.

  • In the file "OoPdfFormExample_WithStamp.pdf", I'm not seeing a stamp at all... assuming you are referring to a "stamp" annotation. – joelgeraci Jul 06 '17 at 21:29
  • Thanks @joelgeraci - it is nor obvious where the stamp is. I added an image [Stamp_Location.png](https://1drv.ms/i/s!Auvpijam7a73iD2U4rlaIAFBuQhi) to help to see where the stamp is. – Matti Ketonen Jul 06 '17 at 21:39
  • @MattiKetonen, I work as developer evangelist at Aspose. I have recorded all your concerns about the logged issue along with further details which has been shared here. As soon as product team completes their investigation, we will notify you within your original thread at Aspose forums. Please spare us little time. – Asad Ali Jul 07 '17 at 10:10
  • @AsadAli, thanks for taking interest to this in here as well. I am looking for additional options and information what to do as I do have my deadlines as well. Your (Aspose) support has been very good to my experience, and getting help from your forum has been instrumental to get most of issues fixed of the solution that this PDF stamping is part of. – Matti Ketonen Jul 07 '17 at 13:43
  • Matti, does @joelgeraci's answer help you? Or do you still have questions? – mkl Jul 10 '17 at 08:43
  • @mkl I haven't yet had time to test, I will do that today and try to see what I learn with my test application. – Matti Ketonen Jul 10 '17 at 12:57

1 Answers1

1

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

joelgeraci
  • 4,606
  • 1
  • 12
  • 19
  • *"Why Aspose refers to it as a Stamp is... well... a mystery."* - well, every PDF library or editor seems to have its misnamed items. But using "stamp" for generic xobjects even though the PDF specification uses "stamp" for a specific annotation type, is very weird indeed. – mkl Jul 07 '17 at 04:21
  • Thanks @joelgeraci, this indeed helps to understand what happens under the hood. I will add an edit to the original question as I figured out one detail more, however it just proves exactly what you have explained that the stamp removal part is where it goes wrong. – Matti Ketonen Jul 10 '17 at 14:07