PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 1:36 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jan 28, 2016 2:39 pm 
Offline

Joined: Thu Jan 28, 2016 2:15 pm
Posts: 2
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 8756 times ]
Capture.PNG
Capture.PNG [ 3.11 KiB | Viewed 8756 times ]
Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 28, 2016 3:38 pm 
Offline
PDFsharp Guru
User avatar

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 29, 2016 2:03 pm 
Offline

Joined: Thu Jan 28, 2016 2:15 pm
Posts: 2
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!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 134 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group