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,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

<o:p> </o:p>

Thank
you for considering Aspose.Pdf.<o:p></o:p>

<o:p> </o:p>

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.<o:p></o:p>

<o:p> </o:p>

Sorry
for the inconvenience,<o:p></o:p>

Thank you for your time and response.

Hi,


Thanks for your patience.

We have further investigated this issue in details and have found that the problem is occurring because of the approach which 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();<o:p></o:p>

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 RESULT OF STAMP DELETING, you add a new stamp on the 1st page of 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 PdfContentEdito result was not even saved) and new stamp was added to original document (where stamp was not deleted). Resultant document has TWO stamps (old and new).

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

  1. Save result of deleting stamps in PdfcontentEditor
  2. Load this file into Document.
(You should delete old stamp with PdfcontentEditor, save result in temporary file (or memory stream is file size allows it), load result with Document and new stamp to it and save resultant file).

[C#]

//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 meomry stream to dont deal with temporary flles)

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.