PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Limit File Size
http://forum.pdfsharp.de/viewtopic.php?f=2&t=4563
Page 1 of 1

Author:  smuldr [ Mon Mar 18, 2024 5:20 pm ]
Post subject:  Limit File Size

I am working on trying to limit the size of the file generated in my code:

I believe one of my issues is that the fonts are being included on each page of the document and not the overall document itself.

Any suggestions would be appreciated. I am not using images on this form. Just tables and textframes.

Here is the code I am using:
Code:
private byte[] Combine(List<Student> students)
        {
            if (students.Count != 0)
            {
                using var ms = new MemoryStream();
                using (var doc = new PdfDocument())
                {
                    foreach (var student in students)
                    {
                        using var pdfStream = new MemoryStream(Generate(student));
                        using var pdf = PdfReader.Open(pdfStream, PdfDocumentOpenMode.Import);
                        foreach (var page in pdf.Pages)
                        {
                            doc.AddPage(page);
                        }
                    }

                    doc.Save(ms);
                }

                return ms.ToArray();
            }

            throw new Exception("No students were provided.");
        }

        private byte[] Generate(Student student)
        {
            var document = new Document();

            var styleHeader = document.Styles.AddStyle("fntTableFontHdr", "Normal");
            styleHeader.Font.Name = "Tinos";
            styleHeader.Font.Size = 14;
            styleHeader.Font.Bold = true;

            var styleRegular = document.Styles.AddStyle("fntTableFont", "Normal");
            styleRegular.Font.Name = "Tinos";
            styleRegular.Font.Size = 12;
            styleRegular.Font.Bold = false;

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

            // Set the section to portrait orientation explicitly
            section.PageSetup.Orientation = Orientation.Portrait;

            // Set the page size to 8.5 x 11 inches
            section.PageSetup.PageWidth = Unit.FromInch(8.5);
            section.PageSetup.PageHeight = Unit.FromInch(11);

            // Set custom margins (units are points, 72 points = 1 inch)
            section.PageSetup.TopMargin = Unit.FromMillimeter(16);
            section.PageSetup.LeftMargin = Unit.FromMillimeter(7);
            section.PageSetup.RightMargin = Unit.FromMillimeter(0);
            section.PageSetup.BottomMargin = Unit.FromMillimeter(0);

            //Add page content below

            GeneratePage1Header(section, student);

            GenerateBlockCodeInfo(section, student);

            GenerateFormTitleAndYear(section);

            GenerateSessionInfo(section, student);

            GenerateStudentInfo(section, student);

            GenerateCourseSelections(section, student);

            GeneratePage1Footer(section);

            section.AddPageBreak();//methods below are for the second page

            GeneratePage2Header(section, student);

            GenerateHighSchoolCoursesInfo(section, student);

            GenerateStemInterest(section, student);

            GenerateInterestSurvey(section, student);

            GenerateApCourseInfo(section, student);

            GenerateCollegeCoursesTakenInfo(section, student);

            //End of adding Data

            var pdfRenderer = new PdfDocumentRenderer()
            {
                Document = document
            };
            pdfRenderer.RenderDocument();

            // Before saving the document, modify the viewer preferences
            // Check if ViewerPreferences exists, if not, create it and turn off print scaling.
            pdfRenderer.PdfDocument.Internals.Catalog.Elements["/ViewerPreferences"] ??= new PdfDictionary(pdfRenderer.PdfDocument);
            var viewerPreferences = (PdfDictionary)pdfRenderer.PdfDocument.Internals.Catalog.Elements["/ViewerPreferences"]!;
            viewerPreferences.Elements["/PrintScaling"] = new PdfName("/None");

            using MemoryStream ms = new();
            pdfRenderer.PdfDocument.Save(ms, false);

            return ms.ToArray();
        }

Author:  TH-Soft [ Mon Mar 18, 2024 5:30 pm ]
Post subject:  Re: Limit File Size

Create all pages in a single run and the font will be included only once.

If you create single-page PDF files and finally combine them, then the fonts come with each page.

Author:  smuldr [ Mon Mar 18, 2024 6:22 pm ]
Post subject:  Re: Limit File Size

Thank you so much for this. Sometimes all it takes is another set of eyes.

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