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

Generated PDF size
http://forum.pdfsharp.de/viewtopic.php?f=2&t=4776
Page 1 of 1

Author:  zooberoo [ Wed Jan 29, 2025 12:11 pm ]
Post subject:  Generated PDF size

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
            }
        }
    }
}

Author:  TH-Soft [ Fri Jan 31, 2025 8:21 am ]
Post subject:  Re: Generated PDF size

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.

Author:  zooberoo [ Tue Feb 04, 2025 9:14 pm ]
Post subject:  Re: Generated PDF size

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.

Author:  TH-Soft [ Tue Feb 11, 2025 1:20 pm ]
Post subject:  Re: Generated PDF size

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.

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