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

Section Data Overwriting Header Data
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4330
Page 1 of 1

Author:  aaronveach [ Wed Mar 16, 2022 5:04 pm ]
Post subject:  Section Data Overwriting Header Data

Hi All,
Brand new to PDFSharp/Migradoc, so I presume I am missing something easy here, but I believe I am doing this the same way as the examples. I am having an issue where data is overwriting into my header. It appears the header is only increasing the y position by 3 rows regardless of how much data I put in it (in my application, I am building a table for the header, which I dont know the size of until its complete). So whether I add a single paragraph with 3 lines of text or 3 paragraphs with a single line to my header, when I later add the "body" paragraph to the section, it always starts 3 rows below the first row of the header (which becomes a problem when my header is 4 or more rows of data). So in the below example, "BODY DATA" overwrites "4444444"

header = section.Headers.Primary;
header.AddParagraph(" 1111111 ");
header.AddParagraph(" 2222222 ");
header.AddParagraph(" 3333333 ");
header.AddParagraph(" 4444444 ");

document.LastSection.AddParagraph("BODY DATA");

Author:  TH-Soft [ Thu Mar 17, 2022 7:16 am ]
Post subject:  Re: Section Data Overwriting Header Data

Hi!

You set the Body area with the margins at top, bottom, right, and left. If the header does not stay in the area reserved by the top margin, header and body will overlap.

Simple solution: increase the top margin.

Author:  aaronveach [ Thu Mar 17, 2022 6:52 pm ]
Post subject:  Re: Section Data Overwriting Header Data

I did try setting the TopMargin in the PageSetup for the body Section. That works if your header is a known static size. But mine is not. What I really need to know is what the size of the header Section object is, so that I can, apparently, reserve that amount of space at the top of my body section.

Is there not just a way to return the "size" of the header section?

Thanks

P.S. If you want a bit more detail, my header data is being placed into a table. So I know the number of rows in that table. My initial thought was just to multiply the rows, by some fixed number that represent a y pos for a single row of text. However, given the font size and the amount of text in each row can vary (so some rows can wrap), that doesnt prove to be a good solution. Then I was thinking I could just loop through the table and get the table.Rows.Height, but that appears to be a user defined property that I would set (and I dont want to statically define a row size for each row in the table, as it should depend on the font size, etc.).

Author:  TH-Soft [ Fri Mar 18, 2022 8:23 am ]
Post subject:  Re: Section Data Overwriting Header Data

Sizes and positions are calculated when a document is being "prepared".

So in your case, you can create a document that contains the header table only, prepare that document, and determine the height of the table to set the top margin for the "big document".

If the table does not mix different fonts or font attribute in any cell, then you can use PDFsharp functionality to calculate the space required for each cell. This will be quicker and probably simpler and will allow to estimate the number of rows required.

Author:  aaronveach [ Fri Mar 18, 2022 3:42 pm ]
Post subject:  Re: Section Data Overwriting Header Data

Thanks. I would not be mixing anything within a given CELL (besides bold and non-bolded text of the same font). However, different cells (in the same row) would have different sized fonts and wrap. I presume your suggesting below, means that I could presumably loop through each row in the table, get the largest CELL size and then keep a running total of that? Is that what you are suggesting? I was trying to do the same thing hoping there would be some size stored for the row, but there isnt.

Thanks

Author:  TH-Soft [ Mon Mar 21, 2022 8:51 am ]
Post subject:  Re: Section Data Overwriting Header Data

aaronveach wrote:
I was trying to do the same thing hoping there would be some size stored for the row, but there isnt.
Size does not exist while you define the contents of a MigraDoc document. Size does not exist when you render to RTF.
Size does exist when you prepare a document for PDF because MigraDoc must do the linebreaks, pagebreaks, and the layout of the PDF pages.

Bold text can be wider than regular text.
Bold text can be wider than regular text.

So to be on the safe side, calculated the width and height for all-bold cell texts or for the actual mix. If there is a gap because prediction failed, then there will be no overlapping as you fail to the safe side.

When you use MigraDoc to render just the header table, then you will have the exact space requirements. This could even be the simpler approach if the header table is somewhat complicated.

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