PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Mar 16, 2022 5:04 pm 
Offline

Joined: Wed Mar 16, 2022 4:40 pm
Posts: 5
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");


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 17, 2022 7:16 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

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


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 17, 2022 6:52 pm 
Offline

Joined: Wed Mar 16, 2022 4:40 pm
Posts: 5
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.).


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 18, 2022 8:23 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

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


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 18, 2022 3:42 pm 
Offline

Joined: Wed Mar 16, 2022 4:40 pm
Posts: 5
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 21, 2022 8:51 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

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


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 138 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