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)