I am trying to clear areas on a .pdf document using Aspose.Pdf (using version 19.4; also tried with v17.4) and its “RedactArea” method.
It is clearing the area that is specified correctly, but only when underneath that area there is only text. If there happens to be an image in that area, full or partial, that area will be cleared (say, on the 1st page) but there will be black boxes over images that appear in that area on other pages.
I’ve included the .pdf that I’m clearing the areas on (“RevSpringTestPdf.pdf”) and that .pdf after it has been cleared (“RevSpringTestPdfBlackBoxes.pdf”).
RevSpringTestPdf.pdf (267.7 KB)
RevSpringTestPdfBlackBoxes.pdf (183.7 KB)
Do you know what is causing this behavior, and if it can be resolved? Thank you very much for any assistance.
Here is the code that I am using to clear areas:
public class Coordinate
{
public double X;
public double Y;
public Coordinate()
{
this.X = 0;
this.Y = 0;
}
public Coordinate(double x, double y)
{
this.X = x;
this.Y = y;
}
}
static void Main()
{
FileInfo pdfFile = new FileInfo(@"c:\temp\RevSpringTestPdf.pdf");
RemoveContent(pdfFile, new List<int>() { 2 }, new Coordinate(1.0, 4.0), new Coordinate(6.5, 7.0));
RemoveContent(pdfFile, new List<int>() { 1 }, new Coordinate(0.25, 0.25), new Coordinate(7.5, 2.0));
}
public static void RemoveContent(FileInfo pdfFileInfo, List<int> pageNumbers, Coordinate bottomLeftInInches, Coordinate topRightInInches)
{
Document pdfDocument = new Document(pdfFileInfo.FullName);
PdfAnnotationEditor annotator = new PdfAnnotationEditor(pdfDocument);
foreach (int pageNumber in pageNumbers)
{
Page page = pdfDocument.Pages[pageNumber];
Rectangle rectangle = new Rectangle(bottomLeftInInches.X * 72, page.MediaBox.Height - bottomLeftInInches.Y * 72,
topRightInInches.X * 72, page.MediaBox.Height - topRightInInches.Y * 72);
annotator.RedactArea(pageNumber, rectangle, page.Background.ToRgb());
}
string tempPath = pdfFileInfo.FullNameWithDifferentExtension(".temppdffile");
annotator.Save(tempPath);
annotator = null;
pdfDocument = null;
pdfFileInfo.Delete();
File.Move(tempPath, pdfFileInfo.FullName);
pdfFileInfo.Refresh();
}