2
votes

Is there a way to print a PDF and select the paper tray to use programmatically?

I'm open to suggestions such as converting the PDF to a different format and printing from there.

I can print to the correct tray using PaperSource() and PrintDocument() is it possible to convert PDFs into a format that these functions can understand?

Thanks.

1
Have you tried generating the PDF documents on different paper sizes? What code are you using to generate you PDFs now? What scripts does your printer understand? - zipzit
I'm using PDF forms and editing it using iTextSharp then saving as a PDF, not sure what scripts the printer will understand - I Hate Java
I think you want PaperCut or something just like it. I suspect with the right printer scripting tool you could determine which paper bin to use via a file name extract. E. G. 'Filename_big.pdf' versus 'Filename_little.pdf' - zipzit
Thanks, I'll have a look at it! - I Hate Java
It's not at all clear on what you are trying to do but if you are using forms to obtain input from others Google Forms (pushing to a Google Apps Script) are pretty awesome. - zipzit

1 Answers

2
votes

Based on getting the paper tray PaperSource from something like here on MSDN, if you don't mind using Ghostscript.NET, this should work for you:

public void PrintPdf(string filePath, string printQueueName, PaperSource paperTray)
{
    using (ManualResetEvent done = new ManualResetEvent(false))
    using (PrintDocument document = new PrintDocument())
    {
        document.DocumentName = "My PDF";
        document.PrinterSettings.PrinterName = printQueueName;
        document.DefaultPageSettings.PaperSize = new PaperSize("Letter", 850, 1100);
        document.DefaultPageSettings.PaperSource = paperTray;
        document.OriginAtMargins = false;

        using (var rasterizer = new GhostscriptRasterizer())
        {
            var lastInstalledVersion =
                GhostscriptVersionInfo.GetLastInstalledVersion(
                        GhostscriptLicense.GPL | GhostscriptLicense.AFPL,
                        GhostscriptLicense.GPL);

            rasterizer.Open(filePath, lastInstalledVersion, false);

            int xDpi = 96, yDpi = 96, pageNumber = 0;

            document.PrintPage += (o, p) =>
            {
                pageNumber++;
                p.Graphics.DrawImageUnscaledAndClipped(
                    rasterizer.GetPage(xDpi, yDpi, pageNumber),
                    new Rectangle(0, 0, 850, 1100));
                p.HasMorePages = pageNumber < rasterizer.PageCount;
            };

            document.EndPrint += (o, p) =>
            {
                done.Set();
            };

            document.Print();
            done.WaitOne();
        }
    }
}