PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Feb 14, 2025 8:57 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Generated PDF size
PostPosted: Wed Jan 29, 2025 12:11 pm 
Offline

Joined: Wed Jan 29, 2025 11:55 am
Posts: 2
Hello. I am evaluating MigraDoc to replace our current PDF library. The following code creates a PDF that is 25KB whereas our current library creates a PDF that is only 1KB.

Clearly, there is something wrong with my code. Can anyone point out where I am going wrong?

Thanks!

Code:
// PDFsharp-MigraDoc 6.1.1 from NuGet
// Legacy .NET Framework 4.8 application

using System.IO;
using PdfSharp.Pdf;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;

namespace IDS.API.PdfTemplates
{
    public class HelloMigraDoc
    {
        public static byte[] CreateDocument()
        {
            var document = new Document();
            var section = document.AddSection();
            var paragraph = section.AddParagraph();
            paragraph.AddText("Hello World!");

            var pdfRenderer = new PdfDocumentRenderer
            {
                Document = document,
                PdfDocument =
                {
                    Options =
                    {
                        FlateEncodeMode = PdfFlateEncodeMode.BestCompression,
                        NoCompression = false,
                        CompressContentStreams = true
                    }
                }
            };

            pdfRenderer.RenderDocument();

            using (var stream = new MemoryStream())
            {
                pdfRenderer.PdfDocument.Save(stream, false);
                return stream.ToArray(); // results in a 25KB filesize
            }
        }
    }
}


Top
 Profile  
Reply with quote  
 Post subject: Re: Generated PDF size
PostPosted: Fri Jan 31, 2025 8:21 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1030
Location: CCAA
PDFsharp always embeds a subset of the font in the PDF file to make sure the font data is always present when the PDF is rendered. This way you have to expect 15 kiB to 30 kiB for just a Hello World sample, depending on the font.

A file size of 1 kiB is only possible if the font is not embedded. Thus there may be layout problems if the font used to display the PDF does not exactly match the font used to measure the text while creating the PDF.

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


Top
 Profile  
Reply with quote  
 Post subject: Re: Generated PDF size
PostPosted: Tue Feb 04, 2025 9:14 pm 
Offline

Joined: Wed Jan 29, 2025 11:55 am
Posts: 2
TH-Soft wrote:
PDFsharp always embeds a subset of the font in the PDF file to make sure the font data is always present when the PDF is rendered. This way you have to expect 15 kiB to 30 kiB for just a Hello World sample, depending on the font.

A file size of 1 kiB is only possible if the font is not embedded. Thus there may be layout problems if the font used to display the PDF does not exactly match the font used to measure the text while creating the PDF.


Thanks TH-Soft. That makes perfect sense. The subset tip is also appreciated as (for example) avoiding the use of italic fonts unless absolutely necessary can also lead to smaller file sizes.


Top
 Profile  
Reply with quote  
 Post subject: Re: Generated PDF size
PostPosted: Tue Feb 11, 2025 1:20 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1030
Location: CCAA
zooberoo wrote:
The subset tip is also appreciated as (for example) avoiding the use of italic fonts unless absolutely necessary can also lead to smaller file sizes.
Yes, most fonts come with 4 TTF files: regular, bold, italic, bold italic.
If size matters, then going for regular only, maybe using colour or underlining, can lead to smaller PDF files.
Going for simple fonts with small TTF files may also help.

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

All times are UTC


Who is online

Users browsing this forum: No registered users and 44 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