ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.04k stars 231 forks source link

pdfsharpcore file size grows thrice after edit and save for header and footer #422

Open Roopeshpm opened 5 months ago

Roopeshpm commented 5 months ago

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.

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;

}