pvginkel / PdfiumViewer

PDF viewer based on Google's PDFium.
Apache License 2.0
966 stars 418 forks source link

Removing blank PDF margins #97

Open ricardopieper opened 7 years ago

ricardopieper commented 7 years ago

Hello!

We are printing some PDFs in one of those thermal printers: http://s7d1.scene7.com/is/image/officedepot/462917_vw_etz00_1017272773?$OD-Search$

Unfortunately, the PDF file comes with some blank margins that are undesirable when we have a long list of items to print. For instance, if we have to render a long table with many rows spanning across multiple pages, those blank margins are rendered in the middle of the table.

I noticed that Foxit (which is what Pdfium is based on) and Adobe have a feature that eliminates those margins from the printing.

It seems like PdfiumViewer itself doesn't have such specific feature, I wonder if you know whether Pdfium supports this.

garysharp commented 7 years ago

Hello @ricardopieper

I'm going to have to make some assumptions about which parts of the API you're using. Are you passing ShrinkToMargin to the PdfDocument.CreatePrintDocument(PdfPrintMode) method? Have you considered passing CutMargin (which is the default if the method is called without the parameter)?

If I've misunderstood your situation, any chance you could post some source code of what it is you're doing, along with a sample PDF?

References:

ricardopieper commented 7 years ago

Of course, I'll attach 2 images: one printed with Foxit/Adobe (setting them to trim those margins off) and one with PDFium. Unfortunately I can't put the PDF here in public, as it has some sensitive information, but I can send it to you by email or something.. Here's the relevant piece of code:

 _document = PdfDocument.Load(memoryStream);
 var print = _document.CreatePrintDocument(PdfPrintMode.CutMargin);
 print.PrinterSettings.PrinterName = printerName;
 print.DefaultPageSettings.PaperSize = paperSize; //sets to {[PaperSize Print width 80mm 30cm Kind=Custom Height=1206 Width=289]}
 print.Print();

This is what is happening on PDFium: gap_pdfium

This is what it should look like: nogap_foxit_adobe_realsize

ricardopieper commented 7 years ago

Now, the PDF does have those gaps, here is a screenshot of it:

image

It just turns out that Adobe and Foxit are able to remove them somehow. This is all portuguese, but the selected option "Tamanho Real" (translates to Real Size) does the trick for me:

image

ricardopieper commented 7 years ago

As an update, I think this is not possible to do in Google's Pdfium. I tried to find a way to at least measure the size of those margins, but apparently there is no way to do that. I don't know how the PDF format works, really. Maybe the margin isn't encoded the way i'm imagining (in a way that's easily queryable).

However, for this document, we found a set of negative margins that do the trick, although i'm trying to find a better solution.

garysharp commented 7 years ago

Curious, any reason why the original PDF is generated on multiple pages instead of a single page? It would seem that this would resolve the issue. Have you tried opening the PDF from within Chrome and printing it there? While less than optimal, you could rasterize the PDF pages, remove the whitespace from in-between pages and then concatenate the images together before printing. It could be that there are some attributes within the PDF document that indicates the pages are to be joined together. If you email the document I'm happy to have a look (I'll contact you via your live.com account).

ricardopieper commented 7 years ago

Thank you very much for your attention :P

I answered your email with the PDF file and some extra context.