ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.08k stars 237 forks source link

Draw Invisible/Transparent Text #297

Open rog1039 opened 2 years ago

rog1039 commented 2 years ago

Hello,

I was looking at making an existing PDF searchable. I have OCR results and was hoping to use DrawString to draw the text in an invisible way so the user can select and copy the text from the page but so it is otherwise not visible.

I tried using new XSolidBrush(XColors.Transparent) to make the text transparent but that just makes the text appear white over the image I am drawing on top of.

Also I tried using DrawRectangle with a Transparent Pen and Transparent brush and that just drew a white rectangle on the page.

Not quite sure where I am going wrong.

This is the code I wrote:

var fileName = @"input.pdf";
var doc      = PdfReader.Open(fileName);

var page = doc.Pages[0];
var gfx  = XGraphics.FromPdfPage(page);

XFont font = new XFont("Times New Roman", 10, XFontStyle.Regular);
gfx.DrawString("some text", font, new XSolidBrush(XColors.Transparent), 50, 50);

doc.Save("output.pdf");

Thanks!

fishrdev commented 2 years ago

You could try drawing the string at the edge of the page, then the text might still be able to be searched. To my knowledge some readers have problem displaying transparent text.

rog1039 commented 2 years ago

Thanks for the input. That would work for that use case. In my case, I am trying to provide the option to highlight 'text' on the page itself. So if the image has 'some random text here' on the page, the user could use the text tool to highlight some or all of that text and copy it to the clipboard. For my use case this invisible/transparent text would have to be directly overtop the image of that text on the page. As far as I know, this is how most of the OCR programs that output searchable PDF documents work -- by applying an invisible/transparent layer of text overtop the original image.