PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Nov 09, 2024 12:11 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Tue Feb 06, 2024 7:23 am 
Offline

Joined: Tue Feb 06, 2024 6:56 am
Posts: 3
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;


}


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 06, 2024 8:29 am 
Offline
PDFsharp Guru
User avatar

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

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


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 06, 2024 8:45 am 
Offline

Joined: Tue Feb 06, 2024 6:56 am
Posts: 3
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 06, 2024 9:29 am 
Offline
PDFsharp Guru
User avatar

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

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


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 06, 2024 3:33 pm 
Offline

Joined: Tue Feb 06, 2024 6:56 am
Posts: 3
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 06, 2024 3:42 pm 
Offline
PDFsharp Guru
User avatar

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

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


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 09, 2024 6:20 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1007
Location: CCAA
GhostScript may work to reduce filesize.
See here:
https://stackoverflow.com/a/51617404/162529

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

All times are UTC


Who is online

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