PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Jun 27, 2022 4:48 am

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: Tue Dec 28, 2021 1:08 pm 

Joined: Tue Dec 14, 2021 7:21 pm
Posts: 4
Following on from my difficulties with Concatenation of documents that included Formfields, I have been looking at creating each document without form fields and using drawstring for each item.

Could anyone assist with suggestions about text rotation where there are multiple items of text.

I have experimented using the Hello World example but placing 2 text strings on the document and rotating the string when drawing using RotateAtTransform. I can prevent the rotation for each string by commenting out the RotateAtTransform command. This way I can sample all 4 possible cases for the two strings.

Unfortunately I find that I am not getting each string rotated with the following results:

Instruction // Result
Test Text A / Text B / Text A / Text B
1 Horizontal (No rot) / Horizontal / A Horizontal / B Horizontal
2 Rotate / Horizontal / A Rotated / B NOT shown
3 Horizontal (No Rot) / Rotate / A Horizontal / B Rotated
4 Rotate / Rotate / A Rotated / B NOT shown ----- Same RESULT as Test 2

From the tests, I can conclude that the string is not drawn IF I have already done a RotateAtTransform for a previous string.

Could anyone advise what I am doing wrong or what I need to add after each transformation so that any further strings get then get drawn and rotated as needed?

I would be pleased for any comments as having looked at many examples, I cannot see what I need to do differently.

Public Sub testpdf()
Dim document As PdfDocument = New PdfDocument()
document.Info.Title = "Created with PDF sharp"

'CreateObject empty page
Dim page As PdfPage = document.AddPage()

'create an xgraphics object for drawing
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)

'draw crossing lines
Dim pen As XPen = New XPen(XColor.FromArgb(255, 0, 0))
gfx.DrawLine(pen, New XPoint(0, 0), New XPoint(page.Width.Point, page.Height.Point))
gfx.DrawLine(pen, New XPoint(page.Width.Point, 0), New XPoint(0, page.Height.Point))

'draw ellipse
gfx.DrawEllipse(pen, 3 * page.Width.Point / 10, 3 * page.Height.Point / 10, 2 * page.Width.Point / 5, 2 * page.Height.Point / 5)

'create font
Dim font As XFont = New XFont("Arial", 72, XFontStyle.Regular)
Dim OPfilename As String = "HelloworldTest1.pdf"

'draw Title
gfx.DrawString("1", font, XBrushes.Black, New XRect(page.Width.Point / 2, page.Height.Point / 2, 50, 72), XStringFormats.CenterLeft)

font = New XFont("Arial", 12, XFontStyle.Regular)

'draw text A
'.RotateAtTransform(90, New XPoint(22, 300))
gfx.DrawString("AAAAAAAA", font, XBrushes.Black, New XRect(22, 300, 50, 20), XStringFormats.CenterLeft)

'draw text B
' gfx.RotateAtTransform(90, New XPoint(425, 560))
gfx.DrawString("BBBBBBBB", font, XBrushes.Black, New XRect(425, 560, 50, 20), XStringFormats.CenterRight)

' SAVE the document
document.Save(strNamePath & "\PDTIX\Created" & OPfilename)

' start a viewer
Process.Start(strNamePath & "\PDTIX\Created" & OPfilename)

End Sub

File comment: Test3 result
HWTest3.jpg [ 240.13 KiB | Viewed 739 times ]
File comment: Test2 result
HWTest2.jpg [ 238.58 KiB | Viewed 739 times ]
File comment: Test1 result
HWTest1.jpg [ 238.94 KiB | Viewed 739 times ]
Reply with quote  
PostPosted: Wed Dec 29, 2021 10:33 am 
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 726
Location: CCAA
You apply transformations to the XGraphics object - and all transformations are accumulated.

What you are missing is "this.state = gfx.Save();" before applying the transformation and "gfx.Restore(this.state);" when the transformations are no longer needed.
The name of the field (here "this.state") does not matter, but make sure to store the state.
See also:
http://pdfsharp.net/wiki/Graphics-sampl ... sformed_22

Best regards
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)

Reply with quote  
PostPosted: Thu Jan 13, 2022 9:26 am 

Joined: Tue Dec 14, 2021 7:21 pm
Posts: 4
Thanks for your advice - that has worked perfectly.


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 18 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:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group