PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 7:40 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Limit File Size
PostPosted: Mon Mar 18, 2024 5:20 pm 
Offline

Joined: Fri Mar 01, 2024 5:57 pm
Posts: 4
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();
        }


Top
 Profile  
Reply with quote  
 Post subject: Re: Limit File Size
PostPosted: Mon Mar 18, 2024 5:30 pm 
Offline
PDFsharp Expert
User avatar

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

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


Top
 Profile  
Reply with quote  
 Post subject: Re: Limit File Size
PostPosted: Mon Mar 18, 2024 6:22 pm 
Offline

Joined: Fri Mar 01, 2024 5:57 pm
Posts: 4
Thank you so much for this. Sometimes all it takes is another set of eyes.


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

All times are UTC


Who is online

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