PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue May 23, 2017 3:14 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Putting text over image
PostPosted: Mon Mar 20, 2017 10:04 am 
Offline

Joined: Mon Mar 20, 2017 9:45 am
Posts: 2
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)


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 20, 2017 12:56 pm 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2640
Location: Cologne, Germany
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?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 20, 2017 3:21 pm 
Offline

Joined: Mon Mar 20, 2017 9:45 am
Posts: 2
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...


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 22, 2017 10:22 am 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2640
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon May 08, 2017 9:22 pm 
Offline

Joined: Tue Aug 02, 2016 9:56 am
Posts: 35
Location: Amsterdam, The Netherlands
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.

_________________
Gerben Vos
Developer, ZyLAB Technologies B.V.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group