PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Nov 09, 2024 12:18 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 16 posts ] 
Author Message
PostPosted: Thu Feb 22, 2024 1:42 pm 
Offline

Joined: Thu Feb 22, 2024 1:31 pm
Posts: 10
Hi I'm trying to generate a pdf with some data (header, paragraph, table, footer etc.) where the table data is taken from a WPF datagrid. But whenever there is many rows in the table the footer text and if the next page header text is overlapping with table data (sample pdf: https://filebin.net/u78dhz0ngwhh1pdh).

Here is the full code:

Code:
      //PDF maker helper Methods
      void AddHeader(MigraDoc.DocumentObjectModel.Section se, string text, int size, bool addBorder = false)
      {
         MigraDoc.DocumentObjectModel.Paragraph pa = se.Headers.Primary.AddParagraph();
         pa.AddFormattedText(text, TextFormat.Bold);
         pa.Format.Alignment = ParagraphAlignment.Center;
         pa.Format.Font.Name = "Times New Roman";
         pa.Format.Font.Size = size;
         pa.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.Blue;
         
         if (addBorder)
         {
            pa.Format.Borders.Bottom = new MigraDoc.DocumentObjectModel.Border() { Width = "0.5pt", Color = MigraDoc.DocumentObjectModel.Colors.DarkGray };
         }

      }


      void AddParagraph(MigraDoc.DocumentObjectModel.Section se, string text, ParagraphAlignment alignment, int size)
      {
         MigraDoc.DocumentObjectModel.Paragraph p = se.AddParagraph();
         p.AddFormattedText(text);
         p.Format.Alignment = alignment;
         p.Format.Font.Name = "Times New Roman";
         p.Format.Font.Size = size;
      }

      void AddParagraph(MigraDoc.DocumentObjectModel.HeaderFooter hf, string text, ParagraphAlignment alignment, int size)
      {
         MigraDoc.DocumentObjectModel.Paragraph p = hf.AddParagraph();
         p.AddFormattedText(text);
         p.Format.Alignment = alignment;
         p.Format.Font.Name = "Times New Roman";
         p.Format.Font.Size = size;
      }



      MigraDoc.DocumentObjectModel.Tables.Table CreateTable(double[] columnWidths)
      {
         MigraDoc.DocumentObjectModel.Tables.Table tab = new MigraDoc.DocumentObjectModel.Tables.Table();
         CreateTableAndAddColumns(tab, columnWidths);
         return tab;
      }

      void AddRowWithHeaders(MigraDoc.DocumentObjectModel.Tables.Table tab, string[] headers)
      {
         MigraDoc.DocumentObjectModel.Tables.Row rw = tab.AddRow();
         rw.Shading.Color = MigraDoc.DocumentObjectModel.Colors.PaleGoldenrod;
         for (int i = 0; i < headers.Length; i++)
         {
            MigraDoc.DocumentObjectModel.Tables.Cell cl = rw.Cells[i];
            cl.AddParagraph(headers[i]);
         }
      }
      
      void AddRowWithData(MigraDoc.DocumentObjectModel.Tables.Table tab, Entry entry)
      {
         MigraDoc.DocumentObjectModel.Tables.Row rw = tab.AddRow();
         rw.Format.Alignment = ParagraphAlignment.Left;
         rw.Format.Font.Size = 9;
         MigraDoc.DocumentObjectModel.Tables.Cell cl;

         cl = rw.Cells[0];
         cl.AddParagraph(entry.SlNo.ToString());

         cl = rw.Cells[1];
         cl.AddParagraph(entry.Description);

         cl = rw.Cells[2];
         cl.AddParagraph(entry.UnitString);

         cl = rw.Cells[3];
         cl.AddParagraph(entry.UOM);

         cl = rw.Cells[4];
         cl.AddParagraph(entry.RateString);

         cl = rw.Cells[5];
         cl.AddParagraph(entry.Amount.ToString("F2", CultureInfo.InvariantCulture));
      }


      void AddTotalTable(MigraDoc.DocumentObjectModel.Document doc, double[] columnWidths, string total)
      {
         MigraDoc.DocumentObjectModel.Tables.Table tTab = CreateTable(columnWidths);
         MigraDoc.DocumentObjectModel.Tables.Row _row = tTab.AddRow();
         _row.Format.Font.Bold = true;
         _row.Cells[0].MergeRight = 3;
         MigraDoc.DocumentObjectModel.Tables.Cell _cell = _row.Cells[0];
         _cell.Format.Alignment = ParagraphAlignment.Right;
         _cell.AddParagraph("Total :");
         _cell = _row.Cells[4];
         _cell.AddParagraph(total);
         doc.LastSection.Add(tTab);
      }
      
      void AddSpacer(MigraDoc.DocumentObjectModel.Section se, int size)
      {
         MigraDoc.DocumentObjectModel.Paragraph p = se.AddParagraph();
         p.Format.SpaceAfter = size;
      }

      
      void CreateTableAndAddColumns(MigraDoc.DocumentObjectModel.Tables.Table table, double[] columnWidths)
      {
         table.Borders.Width = 0.75;
         table.Rows.Alignment = RowAlignment.Center;

         foreach (double width in columnWidths)
         {
            table.AddColumn(Unit.FromCentimeter(width));
         }
      }


PDF generating code
Code:
private void GenPreview(object parameter)
      {
         MigraDoc.DocumentObjectModel.Document doc = new MigraDoc.DocumentObjectModel.Document();
         MigraDoc.DocumentObjectModel.Section se = doc.AddSection();

         AddHeader(se, "Amceable Ork, Inc.", 17);
         AddHeader(se, "A UNIT OF UNITED ASPOSE LLC", 15);
         AddHeader(se, MySelectedItem, 12);
         
         AddHeader(se, "A17-2/NEW SIREN ROAD (WEST), MUMABI-11", 12, true);
         
         AddSpacer(se, 10); // Adjust the size as needed
         
         se.AddParagraph();
         AddParagraph(se, "\nDate: "+ DateTime.Now.ToString("dd-MMM-yyyy")+"                        \n\n", ParagraphAlignment.Right, 10);
         AddParagraph(se, "To\nM/s T@@@@@@@@@@@\nGSTIN: 19#########\nKOLKATA\n\n\n\nDear Sir,\n\nPlease find enclosed herewith the payment for Rs. 999999999999999999999 through IMPS of your following bills\n\n", ParagraphAlignment.Left, 12);

         MigraDoc.DocumentObjectModel.Tables.Table tab = CreateTable(new double[] {1.5, 7.5, 1.5, 1.5, 2, 2.5});
         AddRowWithHeaders(tab, new string[] {"SL No.", "Description", "Unit", "UOM", "Rate", "Amount"});

         
         foreach (Entry entry in Entries)
         {
            AddRowWithData(tab, entry);
         }

         doc.LastSection.Add(tab);
         AddTotalTable(doc, new double[] {1.5, 7.5, 1.5, 1.5, 2, 2.5}, "55555555.00");

         se.Footers.Primary.AddParagraph();
         se.Footers.Primary.AddParagraph();
         se.Footers.Primary.AddParagraph();
         se.Footers.Primary.AddParagraph();
         AddParagraph(se.Footers.Primary, "Kindly acknowledge receipt of the same.\n\nThanking you,\n\nYours faithfully,\nfor AMCEABLE ORK, INC.\n\n\n(M. K. LODHA)", ParagraphAlignment.Left, 12);


         PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false);
         pdfRenderer.Document = doc;
         pdfRenderer.RenderDocument();
         
         
         pdfRenderer.PdfDocument.Save("file.pdf");
         Process.Start("file.pdf");
      }


Please help me fix this.


Last edited by anxytee on Thu Feb 22, 2024 1:49 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 22, 2024 1:45 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1007
Location: CCAA
Make sure top margin and bottom margin are large enough for header and footer.

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


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 22, 2024 1:51 pm 
Offline

Joined: Thu Feb 22, 2024 1:31 pm
Posts: 10
TH-Soft wrote:
Make sure top margin and bottom margin are large enough for header and footer.

Can you please show me how? I've added the link, you can check the generated PDF and what sort of overlapping I'm talking about.


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 26, 2024 8:32 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1007
Location: CCAA
The Section object has a PageSetup property where you can set TopMargin and BottomMargin and other parameters.

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 26, 2024 10:36 am 
Offline

Joined: Thu Feb 22, 2024 1:31 pm
Posts: 10
I've already tried
Code:
se.Document.DefaultPageSetup.TopMargin=Unit.FromCentimeter(3.5);
in my AddHeader method but for some reason when the second page starts the first paragraph there starts quite above the first paragraph of first page. Regarding the footer I'm thinking of using normal paragraphs as it should only appear once in the document.


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 26, 2024 10:42 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1007
Location: CCAA
Try setting "se.PageSetup". Are 3.5 cm enough?

Never change the global DefaultPageSetup. This will throw errors with the current version anyway.

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 26, 2024 4:08 pm 
Offline

Joined: Thu Feb 22, 2024 1:31 pm
Posts: 10
Quote:
Try setting "se.PageSetup".


Tried that, couldn't notice any change though. Still when the second page starts the first paragraph there starts quite above the first paragraph of first page.

Quote:
Are 3.5 cm enough?


Seems like it.

Also, is there a way to check if there is enough space for a paragraph to completely fit in the same page and if the entire para (which might be multiline) doesn't fit in the page then just move that paragraph to next page?


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 26, 2024 11:31 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1007
Location: CCAA
anxytee wrote:
Also, is there a way to check if there is enough space for a paragraph to completely fit in the same page and if the entire para (which might be multiline) doesn't fit in the page then just move that paragraph to next page?
Just set "KeepTogether" for that paragraph to "true" and MigraDoc will keep it on one page.

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


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 28, 2024 2:01 am 
Offline

Joined: Thu Feb 22, 2024 1:31 pm
Posts: 10
Thanks for that information.
But what about the 1st paragraph after header in 1st page appearing in one position and 1st paragraph after header on 2nd page appearing in a higher position compared to 1st page. How to fix it?


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 28, 2024 7:58 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1007
Location: CCAA
anxytee wrote:
But what about the 1st paragraph after header in 1st page appearing in one position and 1st paragraph after header on 2nd page appearing in a higher position compared to 1st page. How to fix it?
Add an empty dummy paragraph before the first visible paragraph on the first page to move it down.

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


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 01, 2024 1:23 pm 
Offline

Joined: Thu Feb 22, 2024 1:31 pm
Posts: 10
Quote:
Add an empty dummy paragraph before the first visible paragraph on the first page to move it down.

I think you misunderstood, the first visible paragraph on the first page is already located down compared to the first visible paragraph on the second page. I want to keep it in the same position throughout the pages. I know in my original code I've added blank paragraph `se.AddParagraph();` after the header but even if I remove it, doesn't change much.


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 01, 2024 1:38 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1007
Location: CCAA
OK, I don't understand the problem.

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


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 03, 2024 8:33 am 
Offline

Joined: Thu Feb 22, 2024 1:31 pm
Posts: 10
Here is the 1st page and 2nd page image for your better understanding https://filebin.net/6ifk50tlnhme8gl5

You could see the 1st para starts immediately after the header separator in 2nd page but in 1st page the 1st para starts much lower after the header separator. Why is that happening?


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 03, 2024 6:48 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 346
You call this for the first page before the date:
Code:
AddSpacer(se, 10); // Adjust the size as needed

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 04, 2024 9:10 am 
Offline

Joined: Thu Feb 22, 2024 1:31 pm
Posts: 10
() => true wrote:
You call this for the first page before the date:
Code:
AddSpacer(se, 10); // Adjust the size as needed

Removed that but still same issue.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 04, 2024 12:41 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
anxytee wrote:
Removed that but still same issue.
Link to IssueSubmissionTemplate:
https://docs.pdfsharp.net/General/IssueReporting.html

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 11 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group