Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Dec 26, 2022 5:00 pm

How to extract background color of a text from pdf using spire pdf?
Like in the below picture I want to fill the white space with the gray color. I want to fill the color dynamically, need to find a text and the background color of the text and replace the text with the same background color. PLEASE HELP
colorReplace.jpg
[code]/\|<|-|||_[/code]

akhiljksuyati
 
Posts: 3
Joined: Tue Aug 30, 2022 5:56 am

Tue Dec 27, 2022 2:44 am

Hello,

Thanks for your inquiry.
Sorry that Spire.Pdf doesn’t support getting the backcolor of text, however, I have logged it as a new function into our product updating system with the ticket number SPIREPDF-3193. Our development team will achieve it. Once it is achieved, I will inform you in time. Sorry for the inconvenience caused.
In addition, Spire.Pdf support setting the backcolor for the text you find, I put the code below for your reference.
If you have any issue, just feel free to contact us.
Code: Select all
//Load the Pdf file
            PdfDocument pdfDocument = new PdfDocument();
            pdfDocument.LoadFromFile(@"../../data/test.pdf");

            PdfPageBase page = pdfDocument.Pages[0];

            //Finding the "70%" in pdf file
            PdfTextFinder finder = new PdfTextFinder(page);
            finder.Options.Parameter = TextFindParameter.IgnoreCase;
            List<PdfTextFragment> fragmentsCollection = finder.Find("70%");

            //Traveing the result of finding text
            foreach (PdfTextFragment fragment in fragmentsCollection)
            {
                RectangleF rectangleF = fragment.Bounds[0];
               
                //Setting the backcolor
                page.Canvas.SetTransparency(1, 1, PdfBlendMode.Multiply);
                page.Canvas.DrawRectangle(new PdfSolidBrush(new PdfRGBColor(191, 191, 191)), rectangleF);           
            }

            //Save the result pdf file
            pdfDocument.SaveToFile(@"../../output/result.pdf", FileFormat.PDF);



Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Fri Jun 13, 2025 9:12 am

Dear E-ICEBLUE Team,

I came across a support response from your team (Ticket No. SPIREPDF-3193), where a user requested the ability to dynamically extract the background color behind a text element in a PDF using Spire.PDF.

I would like to follow up and ask:

1. Has this feature been implemented since the original request?

2. If yes, could you please provide an example or documentation showing how to detect or extract the background color behind a specific text object dynamically?

3. If not yet available, is there any timeline or workaround recommended to approximate background color detection in the meantime?

This feature is quite crucial for my current project, which involves replacing placeholders in PDFs while preserving or matching the background visuals seamlessly.

Looking forward to your response.

snehal.shahdev
 
Posts: 1
Joined: Fri Jun 13, 2025 9:04 am

Fri Jun 13, 2025 10:13 am

Hello,

Thanks for your iqnuiry.
At this time, due to the complexity of this case, our development team is still working to identify a viable solution. Unfortunately, this means we’re unable to provide a concrete timeline for resolution just yet. However, please rest assured that we’re actively prioritizing this matter and will notify you immediately once there are any updates or progress to share.

Sincerely
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF