Welcome Guest Search | Active Topics | Sign In | Register

Existing PDF file print fit to page Options
Jeff Whitlock
Posted: Tuesday, February 28, 2017 8:32:33 AM
Rank: Newbie
Groups: Member

Joined: 6/10/2016
Posts: 8
In the process of coding an application that needs to print existing PDF files (VB .NET app). Using the PDFDocument.Print method works just fine, except that the PDF page getting printed is getting cut off slightly at the margins. Normally when you print using Adobe Reader, you have an option to print to "Fit to page", which will scale the PDF page from the file down enough to print properly.

Here's the code that I'm using to do this so far:

Code: Visual Basic.NET
Dim wrkPDF As New PdfDocument("filename.pdf")
Dim printerSettings As New PrinterSettings()
Dim pageSettings As New PageSettings()
printerSettings.PrinterName = "HP Printer Name"
printerSettings.DefaultPageSettings.Margins = New Margins(0, 0, 0, 0)
For Each size As PaperSize In printerSettings.PaperSizes
    If size.Kind = PaperKind.Letter Then
        printerSettings.DefaultPageSettings.PaperSize = size
        Exit For
    End If
Next
wrkPDF.Print(printerSettings)


The code for setting the margins and the paper size are what I've been trying to adjust to get it to work, but they don't seem to have any effect.

What do I need to set in order to force the PDF file pages to fit the printer page size?

Thanks!
eo_support
Posted: Wednesday, March 1, 2017 5:15:29 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,067
Hi,

Thanks for posting in the forum and sorry about the delay. We have looked into this issue. Here are our findings:

1. The output are always automatically scaled based on paper size (printerSettings.DefaultPageSettings.PaperSize). In another word, it works similar to "fit to page" always --- it doesn't have an option not to resize accoridng to paper size;

2. Margins in page settings are always ignored. The margin of the PDF page is used. So for example, if the PDF page has an one inch margin on screen (logical inch since screen size varies), then you will have a one inch margin on paper;

I am not sure what you meant by "getting cut off slightly at the margins". If you can send us a test file we will try it here and see what we can find. See here for more information on how to send test files to us:

https://www.essentialobjects.com/forum/test_project.aspx

If you can also send us images of how the margins are cut off it would be very helpful as well.

Thanks!
Jeff Whitlock
Posted: Thursday, March 2, 2017 8:46:11 AM
Rank: Newbie
Groups: Member

Joined: 6/10/2016
Posts: 8
The PDF document that I'm trying to print has elements that go all the way to the edge of the 8.5x11 page. Most printers have hard margins of a few hundredths of an inch where nothing will be printed; they don't typically print all the way to the edge of the page. Hence, it appears that the Print method is looking at my PDF document as 8.5x11 and since it's being printed to Letter (same dimensions), it just is sending it to the printer as is without any scaling. That's where I'm getting some print elements being cut off around the edges; it's not taking the printer hard margins into account.

I'm assuming that the scaling you mentioned in #1 has to do with printing one PDF page size to another size and that it doesn't take printer hard margins into account. Is that correct?

Unfortunately, the document I'm printing has some proprietary information and I can't post it. I'll see if I can get something mocked up that I can send as a sample.

Thanks!
eo_support
Posted: Thursday, March 2, 2017 12:11:16 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,067
Hi,

No. We do not take hard margin into account at all. The scaling is always based on the full paper size.

One way to achieve what you wanted is to modify the PDF file in memory to produce the margins. You will need to load PDF file into a PdfDocument object, then call this method on the PdfPage object to produce the margin (and the desired scaling):

https://www.essentialobjects.com/doc/eo.pdf.pdfpage.transform.aspx

After that you can print the modified PDF file. Please let us know if this works for you.

Thanks!
Jeff Whitlock
Posted: Thursday, March 2, 2017 2:12:41 PM
Rank: Newbie
Groups: Member

Joined: 6/10/2016
Posts: 8
Thanks for the info. It appears that the PdfMatrix.Scale method should work for what I need, but I'm only getting blank pages when I apply it.

Here's the code I'm using to test:

Quote:

Dim wrkPDF as new PdfDocument("filename.pdf")
Dim matrix as New PdfMatrix
dim printerSettings as new PrinterSettings()
...
code for printer settings
...
matrix.Scale(-500, -700)
For Each p as PdfPage In wrkPDF.Pages
p.Transform(matrix)
Next
wrkPDF.Print(printerSettings)


I suspect that there's something wrong with how I'm using the Scale method, but I can't find any documentation or code samples on the site for it. Can you steer me in the right direction on this?

Thanks!
eo_support
Posted: Thursday, March 2, 2017 2:17:53 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,067
The argument you passed to scale is definitely wrong. The scale factor are the multiply factors for coordination, so Scale(2, 2) would enlarge the page to twice the original size and Scale(1, 1) would not change anything. Scale(-500, -700) is definitely wrong.

If you are not familiar with matrix transformation, you can find more details here:

https://msdn.microsoft.com/en-us/library/ms536397(VS.85).aspx

Thanks!
Jeff Whitlock
Posted: Thursday, March 2, 2017 2:31:59 PM
Rank: Newbie
Groups: Member

Joined: 6/10/2016
Posts: 8
Super! That did what I needed it to. When I applied the Scale, it still was up against the left hand side, so added a matrix.Translate method to pull the page image more towards the center of the page.

Here's the final VB code for reference, including the printer settings. Hopefully it will be helpful to someone. As always, this code comes with no warranty or guarantees of fitness for any particular purpose.

Code: Visual Basic.NET
Dim wrkPDF As New PdfDocument(wrkDocName)
Dim printerSettings As New PrinterSettings()
Dim matrix As New PdfMatrix
matrix.Scale(0.9F, 0.9F)
matrix.Translate(50, 0)
printerSettings.PrinterName = "HP Printer Name"
For Each p As PdfPage In wrkPDF.Pages
    p.Transform(matrix)
Next
For Each size As PaperSize In printerSettings.PaperSizes
    If size.Kind = PaperKind.Letter Then
        printerSettings.DefaultPageSettings.PaperSize = size
        Exit For
    End If
Next

wrkPDF.Print(printerSettings)


Thanks for your help!
eo_support
Posted: Thursday, March 2, 2017 4:04:40 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,067
You are very welcome and thank you very much for sharing!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.