TextFragment over filled rectangle not visible

I’ve got PDFs and want to add text add a specified position. For this I draw first a white filled rectangle to hide already existing content. Secondly I add text fragments.

Problem is, that the filled rectangles hide all my text fragments:

  • if I do draw the rectangle, then my text fragments are not visible
  • if I do not draw the rectangle, then my text fragements are visible but over the original content

Is this solvable?

@HauptlorenzAG

To solve the issue of text fragments being hidden by filled rectangles in your PDF, you can adjust the drawing order of the elements. The key is to ensure that the text fragments are drawn after the filled rectangles. Here’s how you can achieve this:

  1. Draw the filled rectangle first: This will cover any existing content in the specified area.
  2. Add the text fragments after the rectangle: This ensures that the text is rendered on top of the rectangle, making it visible.

Here’s a sample code snippet demonstrating this approach:

using Aspose.Pdf;
using Aspose.Pdf.Text;
using System.Drawing;

public class AddTextWithRectangle
{
    public static void Run()
    {
        // Create a new document
        Document pdfDocument = new Document();
        Page pdfPage = pdfDocument.Pages.Add();

        // Draw a filled rectangle
        var rect = new Aspose.Pdf.Rectangle(100, 600, 300, 650);
        pdfPage.Paragraphs.Add(new Aspose.Pdf.GraphicState());
        pdfPage.Paragraphs[0].Graphics.DrawRectangle(rect, Color.White);

        // Create a text fragment
        TextFragment textFragment = new TextFragment("Your text here");
        textFragment.Position = new Position(110, 620); // Adjust position as needed
        textFragment.TextState.FontSize = 12;
        textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");
        textFragment.TextState.ForegroundColor = Color.Red;

        // Create a TextBuilder object and append the text fragment
        TextBuilder textBuilder = new TextBuilder(pdfPage);
        textBuilder.AppendText(textFragment);

        // Save the document
        pdfDocument.Save("Output.pdf");
    }
}

In this example, the filled rectangle is drawn first, and then the text fragment is added on top of it. Adjust the position of the text fragment as necessary to ensure it fits well within the rectangle.

By following this order, you should be able to see your text fragments clearly without them being hidden by the filled rectangles.

Thanks for your fast answer. I draw the rectangle first, but within an Graph object, as this is documented under Add Rectangle Object to PDF file|Aspose.PDF for .NET.

Is this the problem? I will try your example.

@HauptlorenzAG
I would like to clarify your request.

Do you want to add text in a position you specify so that it overlaps the existing content, but at the same time the content that is not overlapped is visible?
Please attach the document for which you want to do this, indicating the location of the text to be added.

I’ve tested your example: it does not work. Because Aspose.Pdf.GraphicState() is not existing.

Thanks sergei. I’ve attached you a ZIP-File with all necessary contents:

  • The original File
  • A file which shows the current output of Aspose
  • A manipulated PDF which shows how it should look
  • Also my test solution for VS, without our license

If I have a look at the current output file, I do see that my texts are added but they are behind the white rectangle.

Thanks!
20250307_Aspose_Ticket.zip (484.0 KB)

@HauptlorenzAG
I suggest the following approach.

  1. Removing content in the desired area
  2. Adding text.

The easiest and most convenient way to remove content is to use for this RedactionAnnotation.
Here is the code, how it will be using it

var doc = new Document(dataDir + "InputDocument.pdf");
Page page = doc.Pages[1];
var redactAnn = new RedactionAnnotation(page, new Aspose.Pdf.Rectangle(0, 550, page.Rect.Width, page.Rect.Height));
redactAnn.FillColor = Aspose.Pdf.Color.White;
redactAnn.Color = Aspose.Pdf.Color.White;
page.Annotations.Add(redactAnn);            
redactAnn.Redact();

Since, as you wrote, the text is added, I did not check this part.
If I misunderstood you or something turns out properly, please write here.

That’s it! Thank you very much.

@HauptlorenzAG
Thank you for your feedback.