PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
Putting text over image https://forum.pdfsharp.net/viewtopic.php?f=2&t=3555 |
Page 1 of 1 |
Author: | VitalyP [ Mon Mar 20, 2017 10:04 am ] |
Post subject: | Putting text over image |
Hello, I need to add text over image in pdf. So I have some images (pages) and text (OCR results from tesseract). I tried use PDFSharp: And for each word in OCR-result: Code: Dim document As PdfDocument Dim page As PdfPage Dim gfx As XGraphics Dim ximg As XImage Document = New PdfDocument Page = document.AddPage Gfx = XGraphics.FromPdfPage (page) Ximg = XImage.FromFile ('path_to_img') Page.Height = ximg.Size.Height Page.Width = ximg.Size.Width Gfx.DrawImage (ximg, 0, 0) And for each word in OCR-result: Code: Gfx.DrawString (W_TEXT, New XFont ("Consolas", W_HEIGHT, XFontStyle.Regular, New XPdfFontOptions (PdfFontEncoding.Unicode, PdfFontEmbedding.Default)), XBrushes.Black, New XPoint (W_X, W_Y), XStringFormats.TopLeft) Where W_TEXT, W_HEIGHT, W_X, W_Y - word text, height, coordinates. Code: Document.Save ('output_path') And it was not bad, BUT I need invisible text (with nullable alpha channel) So I tried to use Migradoc: Code: Dim dcm As Document Dcm = New Document () Dim sct As Section = dcm.AddSection Sct.PageSetup.PageHeight = ImageHeight Sct.PageSetup.PageWidth = ImageWidth Sct.PageSetup.TopMargin = 0 Sct.PageSetup.LeftMargin = 0 Sct.PageSetup.BottomMargin = 0 Sct.PageSetup.RightMargin = 0 Sct.AddImage ('path_to_img') Then I tried to use some ways: add paragraphs on sections, use tables and TextFrames. Last way: For each word: Code: Dim txtfrm As MigraDoc.DocumentObjectModel.Shapes.TextFrame = sct.AddTextFrame () Txtfrm.Height = W_HEIGHT Txtfrm.Width = W_WIDTH Txtfrm.Left = W_X Txtfrm.Top = W_Y Dim pgph As Paragraph = New Paragraph () Pgph.Format.Font.Color = MigraDoc.DocumentObjectModel.Color.FromCmyk (0, 0, 0, 0, 0) 'Invisible Pgph.Format.Font.Size = W_HEIGHT Pgph.Format.Alignment = ParagraphAlignment.Center Pgph.AddText (W_TEXT) Txtfrm.Add (pgph) I expect something like this: Code: word1 word2 word3 word4 word5 but have: Code: word1 word2 word3 word4 word5 So every textframe is on new row. And more than that - this words starts on second page (after image). As I understand images and textframes can not overlap each other. Could you give me advice about it. P.S .: sorry for my English) |
Author: | Thomas Hoevel [ Mon Mar 20, 2017 12:56 pm ] |
Post subject: | Re: Putting text over image |
Hi! VitalyP wrote: As I understand images and textframes can not overlap each other. That's wrong.Images and TextFrames are both derived from class Shape. A Shape can be part of a paragraph (with respect to layouting) and elements of a paragraph will not overlap. A Shape can be excluded from the normal layout process. You can use this to assign the Shape an absolute position on the page. Shapes used this way can overlap with other Shapes or with text. Why not use gfx.DrawString with a text color that is 100% transparent? |
Author: | VitalyP [ Mon Mar 20, 2017 3:21 pm ] |
Post subject: | Re: Putting text over image |
Thank you, Thomas! I supposed that I can not use transparent text in DrawString... Maybe you can help me with next problem: As I say I have box (x,y,height,width) and text for all words. But I have not information about font. That is why I use box height as font size and box x and y for word coordinates. So often word on document goes beyond of box or its width less than box width. How to fit word in box? Can I use different intervals between characters in word? How to get factually value of width of drawn string (better know it before drawing for change font)? Or maybe I asked not right things... |
Author: | Thomas Hoevel [ Wed Mar 22, 2017 10:22 am ] |
Post subject: | Re: Putting text over image |
You can use gfx.MeasureString() to determine width and height. You can adjust the font size to match the height. And then maybe draw the word letter by letter, calculating the position of each letter to match the width of the word. Using Courier when the original was Arial will never be perfect. Same if it is the other way around. |
Author: | Gerben Vos [ Mon May 08, 2017 9:22 pm ] |
Post subject: | Re: Putting text over image |
For invisible text, you'd need text rendering mode 3, but from a quick look at the code, it doesn't look like PDFsharp allows you to set that when generating a PDF. |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |