Search code examples
c#pdfocrtransparencypdfsharp

C# PDFSharp transparent Text


I am making a searchable PDF using C# and PDFSharp. Basically - I run the PDF image through a separate OCR application to get the text and then I open the PDF (using PDFSharp) and write the text over the PDF image. All of this works. User then open the final PDF - search for text in the PDF and it highlights the spot in the PDF that has the text.

Problem is - writing text over the PDF - blocks the original PDF image. I want to write transparent text over the PDF. So the text is there - but the text is not visible to humans

This example shows how to do it - but it does not work http://www.pdfsharp.net/wiki/Graphics-sample.ashx#Show_how_to_get_text_metric_information_19

My code is:

            XColor transparentColor = XColors.White;
            transparentColor.A = 0;
            XSolidBrush transparentBrush = new XSolidBrush(transparentColor);

                    xTextFormatter.DrawString(block.Text, font, transparentBrush, xRect);

Some posts I found talked about how it had to be done in CMYK space. I found this example http://www.pdfsharp.net/wiki/ColorsCMYK-sample.ashx And tried this code

            XColor transparentColor = XColor.FromCmyk(0, 0, 0, 0);
            XSolidBrush transparentBrush = new XSolidBrush(transparentColor);

                newPdfDocument.Options.ColorMode = PdfColorMode.Cmyk;

                    xTextFormatter.DrawString(block.Text, font, transparentBrush, xRect);

Both tests produce white text over top of the PDF image. Any ideas? Thanks!


Solution

  • It appears that the opacity (alpha) flag works opposite of what it should in version 1.50.5147 of PDFSharp I got from NuGet.org. @RebootDeluxe should have been correct. But what works - is the opposite. XSolidBrush(XColor.FromArgb(1, 0, 0, 0));.