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

Why is text overlapping when I generate PDF?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4552
Page 1 of 1

Author:  anxytee [ Thu Feb 22, 2024 1:42 pm ]
Post subject:  Why is text overlapping when I generate PDF?

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.

Author:  TH-Soft [ Thu Feb 22, 2024 1:45 pm ]
Post subject:  Re: Why is text overlapping when I generate PDF?

Make sure top margin and bottom margin are large enough for header and footer.

Author:  anxytee [ Thu Feb 22, 2024 1:51 pm ]
Post subject:  Re: Why is text overlapping when I generate PDF?

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.

Author:  TH-Soft [ Mon Feb 26, 2024 8:32 am ]
Post subject:  Re: Why is text overlapping when I generate PDF?

The Section object has a PageSetup property where you can set TopMargin and BottomMargin and other parameters.

Author:  anxytee [ Mon Feb 26, 2024 10:36 am ]
Post subject:  Re: Why is text overlapping when I generate PDF?

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.

Author:  TH-Soft [ Mon Feb 26, 2024 10:42 am ]
Post subject:  Re: Why is text overlapping when I generate PDF?

Try setting "se.PageSetup". Are 3.5 cm enough?

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

Author:  anxytee [ Mon Feb 26, 2024 4:08 pm ]
Post subject:  Re: Why is text overlapping when I generate PDF?

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?

Author:  TH-Soft [ Mon Feb 26, 2024 11:31 pm ]
Post subject:  Re: Why is text overlapping when I generate PDF?

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.

Author:  anxytee [ Wed Feb 28, 2024 2:01 am ]
Post subject:  Re: Why is text overlapping when I generate PDF?

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?

Author:  TH-Soft [ Wed Feb 28, 2024 7:58 am ]
Post subject:  Re: Why is text overlapping when I generate PDF?

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.

Author:  anxytee [ Fri Mar 01, 2024 1:23 pm ]
Post subject:  Re: Why is text overlapping when I generate PDF?

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.

Author:  TH-Soft [ Fri Mar 01, 2024 1:38 pm ]
Post subject:  Re: Why is text overlapping when I generate PDF?

OK, I don't understand the problem.

Author:  anxytee [ Sun Mar 03, 2024 8:33 am ]
Post subject:  Re: Why is text overlapping when I generate PDF?

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?

Author:  () => true [ Sun Mar 03, 2024 6:48 pm ]
Post subject:  Re: Why is text overlapping when I generate PDF?

You call this for the first page before the date:
Code:
AddSpacer(se, 10); // Adjust the size as needed

Author:  anxytee [ Mon Mar 04, 2024 9:10 am ]
Post subject:  Re: Why is text overlapping when I generate PDF?

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

Author:  Thomas Hoevel [ Mon Mar 04, 2024 12:41 pm ]
Post subject:  Re: Why is text overlapping when I generate PDF?

anxytee wrote:
Removed that but still same issue.
Link to IssueSubmissionTemplate:
https://docs.pdfsharp.net/General/IssueReporting.html

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