Env
- Windows 11
- Aspose.PDF for .NET 25.2
Code
var doc = new Aspose.Pdf.Document(@"C:\Users\hello\Desktop\test\1.pdf");
var annotationColor = Aspose.Pdf.Color.White;
foreach (var page in doc.Pages)
{
var annot = new RedactionAnnotation(page, new Aspose.Pdf.Rectangle(0, 0, 300, 100));//throw error when call Redact()
//var annot = new RedactionAnnotation(page, new Aspose.Pdf.Rectangle(0, 0, 50, 50)); //is ok
page.Annotations.Add(annot);
annot.Redact();
}
doc.Save(@"C:\Users\hello\Desktop\test\2.pdf");
Test File
test.zip (365.1 KB)
@xxtears
It seems that you are encountering an exception when calling the Redact()
method on a RedactionAnnotation
object in your code. This issue may arise due to the dimensions of the rectangle you are using for the annotation.
In your code, you are creating a RedactionAnnotation
with a rectangle defined as new Aspose.Pdf.Rectangle(0, 0, 300, 100)
. If this rectangle exceeds the bounds of the page or overlaps with existing content in a way that is not supported, it can lead to exceptions when you attempt to call Redact()
.
Here are a few suggestions to troubleshoot and resolve the issue:
-
Check Rectangle Dimensions: Ensure that the rectangle you are defining for the redaction is within the bounds of the page. You can try using smaller dimensions or adjusting the position to see if that resolves the issue.
-
Test with Different Sizes: As you noted, using a smaller rectangle like new Aspose.Pdf.Rectangle(0, 0, 50, 50)
works without throwing an error. This indicates that the issue may be related to the size or position of the larger rectangle.
-
Overlay Text: If you are using overlay text with the redaction, ensure that it is properly set up. In the example from the documentation, the overlay text is specified, which may help in visualizing the redaction area.
-
Review Documentation: You can refer to the official examples provided in the Aspose documentation for redaction annotations. They demonstrate how to create and apply redactions correctly, which might provide insights into your implementation.
Here is a simplified example based on the documentation that you can use as a reference:
var doc = new Aspose.Pdf.Document(@"C:\Users\hello\Desktop\test\1.pdf");
var annot = new RedactionAnnotation(doc.Pages[1], new Aspose.Pdf.Rectangle(200, 500, 300, 600));
annot.FillColor = Aspose.Pdf.Color.Green;
annot.OverlayText = "REDACTED";
doc.Pages[1].Annotations.Add(annot);
annot.Redact();
doc.Save(@"C:\Users\hello\Desktop\test\2.pdf");
Make sure to adjust the rectangle coordinates to fit within the page dimensions. If the problem persists, consider checking the specific content of the PDF that might be causing the issue.
If you continue to experience difficulties, please provide more details about the exception message you are receiving, as that could help in diagnosing the problem further.
Sources:
[1]: RedactPage.cs
@xxtears
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFNET-59504
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Thank you for your inquiry.
I reproduced the issue and added a task for the development team.
You’ll be notified when the issue has been resolved.