PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Position ond height of a paragraph
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3895
Page 1 of 1

Author:  Thariama [ Mon Dec 10, 2018 2:25 pm ]
Post subject:  Position ond height of a paragraph

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

Author:  Thomas Hoevel [ Mon Dec 10, 2018 2:41 pm ]
Post subject:  Re: Position ond height of a paragraph

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.

Author:  Thariama [ Mon Dec 10, 2018 3:35 pm ]
Post subject:  Re: Position ond height of a paragraph

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?

Author:  Thomas Hoevel [ Mon Dec 10, 2018 3:50 pm ]
Post subject:  Re: Position ond height of a paragraph

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.

Author:  Thariama [ Mon Dec 10, 2018 3:53 pm ]
Post subject:  Re: Position ond height of a paragraph

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.

Author:  Thariama [ Mon Dec 10, 2018 4:08 pm ]
Post subject:  Re: Position ond height of a paragraph

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).

Author:  Thariama [ Mon Dec 10, 2018 4:55 pm ]
Post subject:  Re: Position ond height of a paragraph

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?

Author:  Thomas Hoevel [ Mon Dec 10, 2018 5:53 pm ]
Post subject:  Re: Position ond height of a paragraph

"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.

Author:  Thariama [ Tue Dec 11, 2018 9:58 am ]
Post subject:  Re: Position ond height of a paragraph

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

Author:  Thomas Hoevel [ Tue Dec 11, 2018 10:30 am ]
Post subject:  Re: Position ond height of a paragraph

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.

Author:  Thariama [ Tue Dec 11, 2018 10:40 am ]
Post subject:  Re: Position ond height of a paragraph

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?

Author:  Thomas Hoevel [ Tue Dec 11, 2018 12:10 pm ]
Post subject:  Re: Position ond height of a paragraph

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.

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