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

Change code to make PDF footer
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4254
Page 1 of 1

Author:  wardh [ Wed May 19, 2021 2:20 pm ]
Post subject:  Change code to make PDF footer

Hi,

How can I change my code to get a footer that says Page n of total. e.g. Page 1 of 3.

I can't seem to move my last paragraph to the bottom. Unless there is a better way.

Thanks,

Ward

Code:
private void Generate_PDF_Files(int number_of_pages, string output_folder, int sequence_number)
        {
            string pdf_file_name = string.Format(@"{0}\PDFTest_{1}.pdf", output_folder, sequence_number.ToString("0000"));

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            var enc1252 = Encoding.GetEncoding(1252);

            // Create a MigraDoc document
            Document document = new Document();

            // Add a section to the document
            Section section = document.AddSection();

            for (int page_number=1;page_number<=number_of_pages;page_number++)
            {
                section.AddPageBreak();
                // Add a paragraph to the section
                MigraDoc.DocumentObjectModel.Paragraph paragraph = section.AddParagraph();
               
                string message = string.Format("Filename: {0}", System.IO.Path.GetFileName(pdf_file_name));

                paragraph.AddFormattedText(message, TextFormat.Bold);

                section.AddParagraph();
                //MigraDoc.DocumentObjectModel.Shapes.Image img1 = section.AddImage(image_file_path);

                //img1.LockAspectRatio = true;
                //img1.Width = Unit.FromPoint(500);
                //img1.Height = Unit.FromPoint(300);

                section.AddParagraph();
                //MigraDoc.DocumentObjectModel.Shapes.Image img2 = section.AddImage(image_file_path);

                //img2.LockAspectRatio = true;
                //img2.Width = Unit.FromPoint(500);
                //img2.Height = Unit.FromPoint(300);

                paragraph = section.AddParagraph();
                paragraph.Format.Alignment = ParagraphAlignment.Center;

                message = string.Format("Page {0} of {1}", page_number, number_of_pages);
                paragraph.AddFormattedText(message, TextFormat.Bold);
            }

            const bool unicode = false;
            const PdfFontEmbedding embedding = PdfFontEmbedding.Always;
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);
            pdfRenderer.Document = document;
            pdfRenderer.RenderDocument();

            pdfRenderer.PdfDocument.Save(pdf_file_name);
        }

Author:  TH-Soft [ Wed May 19, 2021 7:21 pm ]
Post subject:  Re: Change code to make PDF footer

Why not use the MigraDoc footer?

See "DefineContentSection" here:
http://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

For simple page numbers you do not need MigraDoc and can use PDFsharp to draw on existing PDF pages.

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