PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed May 19, 2021 2:20 pm 
Offline

Joined: Wed May 19, 2021 1:57 pm
Posts: 1
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);
        }


Top
 Profile  
Reply with quote  
PostPosted: Wed May 19, 2021 7:21 pm 
Offline
PDFsharp Expert
User avatar

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

_________________
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  [ 2 posts ] 

All times are UTC


Who is online

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