Deleting Text Stamp by ID not working

Hi, I'm a newbie to Aspose and I hope this is an easy issue to fix. My .NET solution will generate a text stamp on a pdf document for approvals. If the document is ever approved a second time, my text stamp needs removed and a new one generated in it's place. I don't have errors in my code or at runtime and the contentEditor.DeleteStampById(int) appears to run fine, but no results. Here is my code.

Aspose.Pdf.Facades.PdfContentEditor contentEditor = new Aspose.Pdf.Facades.PdfContentEditor();

contentEditor.BindPdf(li.File.OpenBinaryStream());

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(li.File.OpenBinaryStream());

//Count of pages on document

int iPageCount = pdfDocument.Pages.Count;

//Get all stamps in the document

Aspose.Pdf.Facades.StampInfo[] stampInfo = contentEditor.GetStamps(iPageCount);

//Loop through each stamp

for (int i = 0; i < stampInfo.Length; i++)

{

//Check to see if the ID = 999, if so delete the stamp.

if (stampInfo[i].StampId == 999)

{

contentEditor.DeleteStampById(stampInfo[i].StampId);

}

}

//Adjust for Easter Standard Time

DateTime dtServerTime = DateTime.Now;

DateTime dtEST = dtServerTime.Add(new TimeSpan(GetTimeZoneOffset(), 0, 0));

Aspose.Pdf.Facades.FormattedText txtFormatted = new Aspose.Pdf.Facades.FormattedText();

txtFormatted.AddNewLineText("Processed/Reviewed By:");

txtFormatted.AddNewLineText(web.CurrentUser + " " + dtEST);

TextStamp textStamp = new TextStamp(txtFormatted);

textStamp.Background = true;

textStamp.BottomMargin = 50;

textStamp.HorizontalAlignment = HorizontalAlignment.Right;

textStamp.VerticalAlignment = VerticalAlignment.Bottom;

textStamp.XIndent = 10;

textStamp.YIndent = 10;

textStamp.TextState.FontSize = 10.0F;

textStamp.Opacity = 100;

//Set ID of stamp

textStamp.setStampId(999);

pdfDocument.Pages[iPageCount].AddStamp(textStamp);

//Hold the bits of the PDF

MemoryStream pdfNewDocument = new MemoryStream();

//save output document

pdfDocument.Save(pdfNewDocument);

Hi,

Thank you for considering Aspose.Pdf.

I am able to reproduce your mentioned issue after an initial test. Your issue has been registered in our issue tracking system with issue id: PDFNEWNET-34448. You will be notified via this forum thread regarding any updates against your issue.

Sorry for the inconvenience.

Thank you for your time and response.

Hi,

Thanks for your patience.

We have further investigated this issue in detail and have found that the problem is occurring because of the approach you are following in your code.

  1. Opening source document (“Test_Output.pdf”) via PdfContentEditor object:

    Aspose.Pdf.Facades.PdfContentEditor contentEditor
        = new Aspose.Pdf.Facades.PdfContentEditor();
    contentEditor.BindPdf("D:\\Test_output.pdf");
    
  2. Opening the same source document ('Test_Output.pdf") via Document object:

    Aspose.Pdf.Document pdfDocument
        = new Aspose.Pdf.Document("D:\\Test_output.pdf");
    
  3. Deleting stamps with PdfContentEditor object (please note that your code is redundant; it is not necessary to iterate through all stamps on the page. It is enough to just call contentEditor.DeleteStampById(999) instead.)

    Aspose.Pdf.Facades.StampInfo[] stampInfo = contentEditor.GetStamps(1);
    // Loop through each stamp
    for (int i = 0; i < stampInfo.Length; i++)
    {
        // Check to see if the ID = 999, if so delete the stamp.
        if (stampInfo[i].StampId == 999)
        {
            contentEditor.DeleteStampById(stampInfo[i].StampId);
        }
    }
    
  4. WITHOUT SAVING RESULTS OF STAMP DELETING, you add a new stamp on the 1st page of the SOURCE document via previously created Document object:

    DateTime dtServerTime = DateTime.Now;
    DateTime dtEST = DateTime.Now;
    Aspose.Pdf.Facades.FormattedText txtFormatted = new Aspose.Pdf.Facades.FormattedText();
    txtFormatted.AddNewLineText("Processed/Reviewed By:");
    txtFormatted.AddNewLineText(dtEST.ToString());
    TextStamp textStamp = new TextStamp(txtFormatted);
    textStamp.Background = true;
    textStamp.BottomMargin = 50;
    textStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
    textStamp.VerticalAlignment = VerticalAlignment.Bottom;
    textStamp.XIndent = 10;
    textStamp.YIndent = 10;
    textStamp.TextState.FontSize = 10.0F;
    textStamp.Opacity = 100;
    // Set ID of stamp
    textStamp.setStampId(999);
    pdfDocument.Pages[1].AddStamp(textStamp);
    pdfDocument.Save("D:\\Test_output2.pdf");
    

As a result, stamp deleting is lost (because PdfContentEditor result was not even saved) and a new stamp was added to the original document (where the stamp was not deleted). The resultant document has TWO stamps (old and new).

In order to perform the desired behavior, please update your code as specified below.

  1. Save the result of deleting stamps in PdfContentEditor
  2. Load this file into Document.

(You should delete the old stamp with PdfContentEditor, save the result in a temporary file (or memory stream if the file size allows it), load the result with Document and add a new stamp to it and save the resultant file.)

// load source document
Aspose.Pdf.Facades.PdfContentEditor contentEditor
    = new Aspose.Pdf.Facades.PdfContentEditor();
contentEditor.BindPdf("d:/pdftest/34448.pdf");

// remove stamp by Id (without stamps iterating)
contentEditor.DeleteStampByIds(new int[] { 999 });

// save result into intermediate file (may be stored into memory stream to not deal with temporary files)
contentEditor.Save("d:/pdftest/34448-2.pdf");
Aspose.Pdf.Document pdfDocument
    = new Aspose.Pdf.Document("d:/pdftest/34448-2.pdf");

// add stamp as was made by customer (as in original code).
// Adjust for Easter Standard Time
DateTime dtServerTime = DateTime.Now;
DateTime dtEST = DateTime.Now;
Aspose.Pdf.Facades.FormattedText txtFormatted = new Aspose.Pdf.Facades.FormattedText();
txtFormatted.AddNewLineText("Processed/Reviewed By:");
txtFormatted.AddNewLineText(dtEST.ToString());
TextStamp textStamp = new TextStamp(txtFormatted);
textStamp.Background = true;
textStamp.BottomMargin = 50;
textStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
textStamp.VerticalAlignment = VerticalAlignment.Bottom;
textStamp.YIndent = 10;
textStamp.TextState.FontSize = 10.0F;
textStamp.Opacity = 100;
// Set ID of stamp
textStamp.setStampId(999);
pdfDocument.Pages[1].AddStamp(textStamp);

// save output document
pdfDocument.Save("d:/pdftest/34448_Updated.pdf");

The issues you have found earlier (filed as PDFNEWNET-34448) have been fixed in Aspose.Pdf for .NET 7.5.0.


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