PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Apr 19, 2024 8:23 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sat Jul 30, 2016 1:44 am 
Offline

Joined: Sat Jul 30, 2016 1:35 am
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 30, 2016 7:16 am 
Online
PDFsharp Expert
User avatar

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

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 01, 2016 8:02 pm 
Offline

Joined: Sat Jul 30, 2016 1:35 am
Posts: 3
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 04, 2016 9:05 pm 
Offline

Joined: Sat Jul 30, 2016 1:35 am
Posts: 3
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!


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 05, 2016 8:35 am 
Online
PDFsharp Expert
User avatar

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

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

All times are UTC


Who is online

Users browsing this forum: TH-Soft and 241 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