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

pdfsharpcore file size grows after edit and save
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4546
Page 1 of 1

Author:  roopesh [ Tue Feb 06, 2024 7:23 am ]
Post subject:  pdfsharpcore file size grows after edit and save

I was working on a project where user will upload a PDF document which will be around 35mb and it will be stored in ECS bucket.
While user tries to download the same file from back end we will open the document programmatically and update each page header and footer with some predefined text For ex:User name and document valid date.
After that we will save the file and send back to user.
But once we update the file and save, size grows near to three times. For a 35mb file it will become around 99mb.
Can you please help me to solve this. for a simple header and footer change if file grows to three times then my client will raise concerns.

Code:
public MemoryStream UpdatePDFFromStream(MemoryStream ms)
{
    var exportMarkingText = "Company Proprietary. Copyright © The  Company";
    XFont font = new XFont("Calibri", 14, XFontStyle.Bold);
    PdfDocument PDFDoc = PdfReader.Open(ms, PdfDocumentOpenMode.Modify);
    for (int Pg = 0; Pg < PDFDoc.Pages.Count; Pg++)
    {
        PdfPage page = PDFDoc.Pages[Pg];
        XGraphics gfx = XGraphics.FromPdfPage(page);
        //check pdf's rotation angle
        var fileRotationAngle = PDFDoc.Pages[0].Rotate;
        XPoint point = new XPoint(0, 0);

        if (fileRotationAngle == 90)
        {
            point = new XPoint(60 - PDFDoc.Pages[0].Width, 50);
            gfx.RotateTransform(-fileRotationAngle);
        }
        else if ((fileRotationAngle == 0))
        {
            //point = new XPoint(60, 12);
            point = new XPoint(80, 12);

        }
        else
        {

        }
        XPoint pointFooter = new XPoint(80, 780);
        gfx.DrawString(exportMarkingText, font, XBrushes.Black, point);
        //sample test. will change as per user requirements
        gfx.DrawString("Not Valid after Sept-30-2024", font, XBrushes.Black, pointFooter);

    }
               
    PDFDoc.Save(ms);
    return ms;


}

Author:  TH-Soft [ Tue Feb 06, 2024 8:29 am ]
Post subject:  Re: pdfsharpcore file size grows after edit and save

roopesh wrote:
For a 35mb file it will become around 99mb.
For some files, the change in size will be small. For other files there will be a significant increase.
At the moment, this is not a bug, but a feature.

Author:  roopesh [ Tue Feb 06, 2024 8:45 am ]
Post subject:  Re: pdfsharpcore file size grows after edit and save

But How come file size grows this way. I have tested with file 150kb and this file will grow up to 290kb.
For each page I am adding header and footer which is very small for ex: around 200characters.
Because of this issue my client is not ready to accept the solution and they complain on file size.
Do you have any suggestions?

Author:  TH-Soft [ Tue Feb 06, 2024 9:29 am ]
Post subject:  Re: pdfsharpcore file size grows after edit and save

roopesh wrote:
For each page I am adding header and footer which is very small for ex: around 200characters.
When adding text to a document, PDFsharp will add the text plus a font subset. This cannot be avoided at the moment when using PDFsharp. The font subset is added once per document and per edit cycle, not once per page.
PDFsharp does not yet write object streams. PDF files that contain object streams may grow significantly in size when processed with PDFsharp. This is by design.

You can try other tools to modify the PDF files.
You can try and run PDF compression tools after modifying the PDF files with PDFsharp.

BTW: Are you using PDFsharp or are you using PdfSharpCore? This site is about PDFsharp and information given here may not apply to PdfSharpCore.

Author:  roopesh [ Tue Feb 06, 2024 3:33 pm ]
Post subject:  Re: pdfsharpcore file size grows after edit and save

I am using PDFSharpCore. I thought it is a subset of PDFsharp.
If not can you guide me which is the support site for PDFSharpCore.

Author:  TH-Soft [ Tue Feb 06, 2024 3:42 pm ]
Post subject:  Re: pdfsharpcore file size grows after edit and save

roopesh wrote:
If not can you guide me which is the support site for PDFSharpCore.
You can try this:
https://github.com/ststeiger/PdfSharpCore

You can also try PDFsharp to see if the results are the same.

Author:  TH-Soft [ Fri Feb 09, 2024 6:20 am ]
Post subject:  Re: pdfsharpcore file size grows after edit and save

GhostScript may work to reduce filesize.
See here:
https://stackoverflow.com/a/51617404/162529

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