PDFsharp & MigraDoc Forum
https://forum.pdfsharp.net/

Saving PDF shrinks the page size
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3404
Page 1 of 1

Author:  slothario [ Sat Jul 30, 2016 1:44 am ]
Post subject:  Saving PDF shrinks the page size

I save PDFs as follows:

Code:
            var InFileName = HostingEnvironment.MapPath("~/App_Data/Forms/" + Filename);
           
            PdfDocument doc = null;
            try
            {
                doc = PdfReader.Open(InFileName, PdfDocumentOpenMode.Modify);
            }
            catch (FileNotFoundException)
            {
                return null;
            }

            using (MemoryStream stream = new MemoryStream())
            {
                doc.Save(stream, false);
                return stream.ToArray();
            }


So far, it's worked fine for most PDFs, but for one PDF it shrinks the size. The input looks like a normal PDF with normal, legal-sized pages. The output's pages looks like this:
Image

Note that when I highlight it, the blue selection box is roughly legal size but the actual PDF shown is still small. What could cause that?

Thanks,
Danny

Author:  TH-Soft [ Sat Jul 30, 2016 7:16 am ]
Post subject:  Re: Saving PDF shrinks the page size

Hi!
slothario wrote:
So far, it's worked fine for most PDFs, but for one PDF it shrinks the size.
How can we investigate the problem without this PDF?

See also:
viewtopic.php?f=2&t=832


Maybe the modification from this post will help:
viewtopic.php?p=9591#p9591

Author:  slothario [ Mon Aug 01, 2016 8:02 pm ]
Post subject:  Re: Saving PDF shrinks the page size

I'm running the latest prerelease version due to its ability to open later version PDFs, the 1.50.4000-beta3b NuGet package, on an ASP.NET MVC project. Here's the PDFs in question:

Unprocessed:
https://dl.dropboxusercontent.com/u/4013525/I-9%20-%20unprocessed.pdf

Processed and shrunk:
https://dl.dropboxusercontent.com/u/401 ... shrunk.pdf

I'll take a look at those modifications you mentioned.

Author:  slothario [ Thu Aug 04, 2016 9:05 pm ]
Post subject:  Re: Saving PDF shrinks the page size

Now that I had some time to look more closely, it seems I was able to fix the problem with the "quick and dirty" hack listed at the top of the page (ported to C#):

Code:
            for (int k=0; k<PdfDoc.Pages.Count; k++)
            {
                var page = PdfDoc.Pages[k];
                if (page.MediaBox.Width != page.Width.Point || page.MediaBox.Height != page.Height.Point)
                {
                    page.MediaBox = new PdfRectangle(new XPoint(0, 0), new XPoint(page.Width.Point, page.Height.Point));
                }
            }


I'm not sure if the rest of the code would apply to me because my problem doesn't seem to have anything to do with rotation. My question is: if we know that these things are going on and we're able to write fixes, why aren't there pull requests on the PDFSharp library? Do they need help integrating these homegrown fixes into the library?

Nethertheless, the solution works. Thanks for your help!

Author:  TH-Soft [ Fri Aug 05, 2016 8:35 am ]
Post subject:  Re: Saving PDF shrinks the page size

slothario wrote:
Do they need help integrating these homegrown fixes into the library?
No. It just takes some time between releases.

It seems you tried the hack that was not implemented in the current internal version of PDFsharp.
It would be better to have one tested solution and not three proposed hacks.

The PDFsharp team wants one solution that works for all files.

The sizes of Page and MediaBox can be different - that's why both settings are in the PDF.
Your hack really is a hack as it ignores the settings from the file.
My proposed change cures the root problem: treating rotation incorrectly which brings the two boxes out of sync.


slothario wrote:
I'm not sure if the rest of the code would apply to me because my problem doesn't seem to have anything to do with rotation.
The page in your document is marked with "/ROTATE 90", so this is exactly the problem that does not occur when a few lines in PDFsharp are commented out.

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