PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Mar 23, 2019 12:24 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Mon Dec 10, 2018 2:25 pm 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
I have used the GetRenderInfoFromPage function and a previously set Tag to get a the rendering info of a paragrpah.
Then i accessed the LayoutInfo to get the desired values:

Code:
Console.WriteLine("Heigth: {0}", renderInfos[g].LayoutInfo.ContentArea.Height);
Console.WriteLine("Width: {0}", renderInfos[g].LayoutInfo.ContentArea.Width);
Console.WriteLine("StartX: {0}", renderInfos[g].LayoutInfo.ContentArea.X);
Console.WriteLine("StartY: {0}", renderInfos[g].LayoutInfo.ContentArea.Y);


Code:
Console.WriteLine("Heigth: {0}", renderInfos[g].LayoutInfo.ContentArea.Height);
Console.WriteLine("Width: {0}", renderInfos[g].LayoutInfo.ContentArea.Width);
Console.WriteLine("StartX: {0}", renderInfos[g].LayoutInfo.ContentArea.X);
Console.WriteLine("StartY: {0}", renderInfos[g].LayoutInfo.ContentArea.Y);


The disturbing thing i encountered is that the LayoutInfo i receive is erroneous.
The X offset and Y offset and even the width and height are wrong!
I do not know why.
Can it be that line breaking in the paragraph does not get accounted for?
This does not explain why the start X and Y position are wrong.

Even when i tag a simple small paragraph containing one single word and place it at x = 1.3 cm i get
70 pt as renderInfos[g].LayoutInfo.ContentArea.X!!!! WTF?


Does anyone have any insights to this?

Stefan


Last edited by Thariama on Mon Dec 10, 2018 2:55 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 10, 2018 2:41 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2918
Location: Cologne, Germany
Hi!
Thariama wrote:
Does anyone have any insights to this?
This is the information that is used to draw the paragraph.

What do you mean with "The X offset and Y offset and even the width and height are wrong!"?
Which values do you get? Which values would be correct?
The unit for offsets and dimensions is Points.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 10, 2018 3:35 pm 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
I used this code to create my simple heading paragraph:

Code:
            Paragraph heading = currentSection.AddParagraph();
            heading.Tag = "Heading";
            heading.AddText("Data Sheet"));
            heading.Format.Font.Size = 20;
            heading.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.Black;
            heading.Format.Font.Name = fontName;
            heading.Format.Alignment = ParagraphAlignment.Left;
            heading.Format.FirstLineIndent = "0mm";

            // render the heading paragraph
docRenderer.RenderObject(currentGfx, XUnit.FromCentimeter(1.3), XUnit.FromCentimeter(1.1), "8cm", heading);


I used the code bellow to paint the dimensions of my simple paragraph:

Code:
 
                                Console.WriteLine("Heading Info:");

                                XPen pena = new XPen(XColor.FromKnownColor(XKnownColor.YellowGreen), 1);
                                currentGfx.DrawLine(pena, new System.Windows.Point(renderInfos[g].LayoutInfo.ContentArea.X, renderInfos[g].LayoutInfo.ContentArea.Y), new System.Windows.Point(renderInfos[g].LayoutInfo.ContentArea.X + renderInfos[g].LayoutInfo.ContentArea.Width, renderInfos[g].LayoutInfo.ContentArea.Y));
                                Console.WriteLine("x:" + (double) renderInfos[g].LayoutInfo.ContentArea.X);
                                Console.WriteLine("y:" + (double)renderInfos[g].LayoutInfo.ContentArea.Y);
                                Console.WriteLine("width:" + (double)renderInfos[g].LayoutInfo.ContentArea.Width);
                                Console.WriteLine("height:" + (double)renderInfos[g].LayoutInfo.ContentArea.Height);
                                currentGfx.DrawLine(pena, new System.Windows.Point(renderInfos[g].LayoutInfo.ContentArea.X, renderInfos[g].LayoutInfo.ContentArea.Y), new System.Windows.Point(renderInfos[g].LayoutInfo.ContentArea.X, renderInfos[g].LayoutInfo.ContentArea.Y + renderInfos[g].LayoutInfo.ContentArea.Height));



Here is the text output for the leading simple paragraph:
x:70,8661417322835
y:70,8661417322835
width:453,543307086614
height:24,72

Since approx. 28 pt = 1 cm i would expect to have X and Y value at slightly more than 30 pt but less than 40 pt (but i got 70 pt).
Maybe there is a misinterpretation of points to centimeters here.

The lines drawn on my pdf do not match my paragraph. Why?
Plus, i wonder why X and Y are the same when i gave it X = 1.3 cm and Y = 1.1 cm?


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 10, 2018 3:50 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2918
Location: Cologne, Germany
I never use RenderObject, so I don't have much experience.

It looks as if X and Y are 2.5 cm - which IIRC are the default margins of MigraDoc.

So maybe your offsets are added to the page margins?
I'd try setting page margins to 0 and run the code again.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 10, 2018 3:53 pm 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
The funny thing is, when i use 70 points to create the paragraph the drawn lines on the pdf match:
Code:
docRenderer.RenderObject(currentGfx, XUnit.FromPoint(70), XUnit.FromPoint(70), XUnit.FromPoint(300), heading);


Thx, for your answer.
I will give it a try and report what happened.


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 10, 2018 4:08 pm 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
I tried:

Code:
currentPage.TrimMargins.All = 0;


but i get the same result.
I mean, the rendering works fine and the pdf looks like it should (at least for the part that is not dependent on the measurement of the paragraph).


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 10, 2018 4:55 pm 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
I set:
Code:
                currentSection.PageSetup.TopMargin = 0;
                currentSection.PageSetup.LeftMargin = 0;


And renderInfos[g].LayoutInfo.ContentArea.X and Y are both 0 for the simple one line paragraph.
So a margin gets applied somehow.

But the width and height of the more complex paragraph still do not match and there should not be any margin *scream*.

It seems like this way to measure rendered paragraphs is somehow bogus.

Do you have any other working approach?


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 10, 2018 5:53 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2918
Location: Cologne, Germany
"RenderPage()" is what we use and it works as expected.

The comment for "RenderObject()" shows "This function is still in an experimental state."
RenderObject creates a Renderer internally. Maybe take the source code and modify RenderObject to return the RenderInfo that is actually used.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 11, 2018 9:58 am 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
The examples i found only show how to draw images, lines etc. using RenderPage.

Could you please show me a piece of example code of how to use RenderPage to place a paragraph on a page?
Thanks in adavance.
Stefan


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 11, 2018 10:30 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2918
Location: Cologne, Germany
Hi!
Thariama wrote:
Could you please show me a piece of example code of how to use RenderPage to place a paragraph on a page?
RenderPage draws the page.
Use TopMargin/LeftMargin or a Table or a TextFrame to position the text on the page.

You can call RenderPage a few times to stack several items on a single PDF page.

RenderObject does not handle page breaks. With RenderPage you can take advantage of automatic page breaks.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 11, 2018 10:40 am 
Offline

Joined: Mon Jul 30, 2018 1:40 pm
Posts: 15
Quote:
RenderObject does not handle page breaks.


That would not be a problem for me if automatic line breaks would be handled correctly.

Quote:
Use TopMargin/LeftMargin or a Table or a TextFrame to position the text on the page.

So, i am not able to render a paragraph? If no i will have to use a TextFrame.
Can i set a maximum width and line height for that and use formatted text?


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 11, 2018 12:10 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2918
Location: Cologne, Germany
Thariama wrote:
So, i am not able to render a paragraph?
The paragraph will be rendered as a part of the page. Same goes for TextFrame.
A TextFrame is only a container for Paragraphs and other document elements.

You can set left margin to 1.3cm, top margin to 1.1cm. You can set right margin to get a paragraph width of 8 cm.
No need for a TextFrame IMHO.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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