PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

XForm rendered position changes when text gets too small
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3279
Page 1 of 1

Author:  Sjeijoet [ Thu Jan 28, 2016 2:39 pm ]
Post subject:  XForm rendered position changes when text gets too small

Hello!

I'm using PdfSharp 1.32.3057.0 (GDI build I believe) to generate referencefiles for my company's scan process.
The PDFs we generate contain the object id and file type of the file we're scanning.
We use the following VB.Net code to apply both parameters on the PDF:

Code:
Private Sub DrawObjectIdAndFileType(ByVal document As PdfDocument, ByVal graphics As XGraphics, ByVal page As PdfPage, ByVal objectId As String, ByVal fileType As String)
    Dim objSize As XSize = graphics.MeasureString(objectId, New XFont("Calibri", 12, XFontStyle.Regular))
    Dim fileTypeSize = graphics.MeasureString("(" & fileType & ")", New XFont("Calibri", 12, XFontStyle.Regular))

    Dim totalHeight = objSize.Height + fileTypeSize.Width + 4

    Dim largestWidth = fileTypeSize.Width + 20
    If (objSize.Width > fileTypeSize.Width) Then
        largestWidth = objSize.Width + 20
    End If

    Dim objForm As XForm = New XForm(document, totalHeight, largestWidth)
    Dim objGraphs As XGraphics = XGraphics.FromForm(objForm)

    Dim objObjectRect = New XRect((largestWidth - objSize.Width) / 2, 0, objSize.Width, objSize.Height)
    Dim objFileTypeRect = New XRect((largestWidth - fileTypeSize.Width) / 2, objSize.Height + 4, fileTypeSize.Width, fileTypeSize.Height)

    Dim objFormatter As XTextFormatter = New XTextFormatter(objGraphs)

    objGraphs.RotateAtTransform(270, New XPoint(totalHeight / 2, largestWidth / 2))
    objFormatter.DrawString(objectId, New XFont("Calibri", 12, XFontStyle.Regular), XBrushes.Black, objObjectRect)
    objFormatter.DrawString("(" & fileType & ")", New XFont("Calibri", 12, XFontStyle.Regular), XBrushes.Black, objFileTypeRect)

    graphics.DrawImage(objForm, New System.Drawing.Point(100, CType(page.Height, Integer) * 0.8 - largestWidth / 2))
End Sub


However, when the file type gets too small, the horizontal offset is no longer correct:

Image

Quote:
13 0 obj
<<
/Type/XObject
/Subtype/Form
/BBox[0 0 37.328 56.492]
/Resources
<<
/ProcSet [/PDF/Text/ImageB/ImageC/ImageI]
/ExtGState
<<
/GS0 8 0 R
>>
/Font
<<
/F0 15 0 R
>>
>>
/Length 150
>>
stream
q
1 0 0 -1 0 56.492 cm -100 Tz
q
0 -1 1 0 -9.582031 46.910156 cm
BT
/GS0 gs
/F0 -12 Tf
10 11.4258 Td (120148) Tj
8.9063 18.6484 Td (\(ye\)) Tj
ET
Q
Q
endstream
endobj


As shown below, when the file type is a longer string, it does show correctly:

Image

Quote:
13 0 obj
<<
/Type/XObject
/Subtype/Form
/BBox[0 0 75.074 76.426]
/Resources
<<
/ProcSet [/PDF/Text/ImageB/ImageC/ImageI]
/ExtGState
<<
/GS0 8 0 R
>>
/Font
<<
/F0 15 0 R
>>
>>
/Length 158
>>
stream
q
1 0 0 -1 0 76.426 cm -100 Tz
q
0 -1 1 0 -0.675781 75.75 cm
BT
/GS0 gs
/F0 -12 Tf
19.9668 11.4258 Td (120148) Tj
-9.9668 18.6484 Td (\(Algemeen\)) Tj
ET
Q
Q
endstream
endobj


All PDF files that are generated with a larger file type have the same (offset?) value, marked in green.
When I change the value marked with red (first PDF) to the value marked in green (second PDF), the parameters from the first PDF are positioned correctly.

Any idea what is causing this issue?

Kind regards,
Anton

Attachments:
Capture2.PNG
Capture2.PNG [ 5.7 KiB | Viewed 8882 times ]
Capture.PNG
Capture.PNG [ 3.11 KiB | Viewed 8882 times ]

Author:  Thomas Hoevel [ Thu Jan 28, 2016 3:38 pm ]
Post subject:  Re: XForm rendered position changes when text gets too small

Hi, Anton,

This seems to happen when "If (objSize.Width > fileTypeSize.Width) Then" is true.

You specify " As XSize " for objSize, but not for fileTypeSize. There is no "As" for largestWidth either.
Maybe this makes a difference, maybe it doesn't. Check values and types in a debugger.

BTW: The code snippet invokes "New XFont("Calibri", 12, XFontStyle.Regular)" four times. Assigning this to a variable will make the code faster and IMHO easier to read and maintain.

Author:  Sjeijoet [ Fri Jan 29, 2016 2:03 pm ]
Post subject:  Re: XForm rendered position changes when text gets too small

Thanks for the suggestions, its better practice indeed.

My issue was my understanding of the behaviour when rotating the XForm graphics. I had chosen a wrong rotation point and did not transform the graphics into the right position to fit the XForm object:

Quote:
Private Sub DrawObjectIdAndFileType(ByVal document As PdfDocument, ByVal graphics As XGraphics, ByVal page As PdfPage, ByVal objectId As String, ByVal fileType As String)
Dim font = New XFont("Calibri", 12, XFontStyle.Regular)

Dim objSize = graphics.MeasureString(objectId, font)
Dim fileTypeSize = graphics.MeasureString("(" & fileType & ")", font)

Dim totalHeight = objSize.Height + fileTypeSize.Height + 4

Dim largestWidth = fileTypeSize.Width + 20
If (objSize.Width > fileTypeSize.Width) Then
largestWidth = objSize.Width + 20
End If

Dim objForm As XForm = New XForm(document, totalHeight, largestWidth)
Dim objGraphs As XGraphics = XGraphics.FromForm(objForm)

Dim objObjectRect = New XRect((largestWidth - objSize.Width) / 2, 0, objSize.Width, objSize.Height)
Dim objFileTypeRect = New XRect((largestWidth - fileTypeSize.Width) / 2, objSize.Height + 4, fileTypeSize.Width, fileTypeSize.Height)

Dim objFormatter As XTextFormatter = New XTextFormatter(objGraphs)

objGraphs.RotateAtTransform(270, New XPoint(totalHeight, largestWidth))
objGraphs.TranslateTransform(totalHeight, largestWidth - totalHeight)

objFormatter.DrawString(objectId, font, XBrushes.Black, objObjectRect)
objFormatter.DrawString("(" & fileType & ")", font, XBrushes.Black, objFileTypeRect)

graphics.DrawImage(objForm, New System.Drawing.Point(100, CType(page.Height, Integer) * 0.8 - largestWidth / 2))
End Sub


Thanks again!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/