jackrabbit
Member
- Joined
- Jan 29, 2018
- Messages
- 7
- Programming Experience
- 10+
In my legacy application, the user may select many reports saved as a PDF file for preview or for automatic printing. With each report, I have a list of required settings (paper type, duplex printing, etc) so that when I want to print a report, I do not want the user to manually select all those settings from the Print windows, I want to print directly to the printer with the saved settings.
I wrote a small program that does exactly that, my only problem: it seems to create huge files when sending the PDF to the printer, so I suspect my PDF document gets converted to a 600 DPI bitmap in the process. This is a show stopped, because it only prints a page every 10-15 seconds. If I print the same PDF file directly, it goes as fast as the printer can handle it.
I cannot send my PDF document as a RAW document, because I need to set properties like DUPLEX to print on both sides of a page. My program uses the Pdfium library to load PDF documents.
Is there another way to print a document, while automatically set printing properties like Duplex without any user intervention? My laser printer only understands Postscript and PDF, it does not support PCL or PJL.
Here is my code, it works but it is very slow on complex PDF reports:
I wrote a small program that does exactly that, my only problem: it seems to create huge files when sending the PDF to the printer, so I suspect my PDF document gets converted to a 600 DPI bitmap in the process. This is a show stopped, because it only prints a page every 10-15 seconds. If I print the same PDF file directly, it goes as fast as the printer can handle it.
I cannot send my PDF document as a RAW document, because I need to set properties like DUPLEX to print on both sides of a page. My program uses the Pdfium library to load PDF documents.
Is there another way to print a document, while automatically set printing properties like Duplex without any user intervention? My laser printer only understands Postscript and PDF, it does not support PCL or PJL.
Here is my code, it works but it is very slow on complex PDF reports:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Printing;
using PdfiumViewer;
//---------------------------------------------
// Program to load a report in PDF format
// Set printing attributes (Paper type, Duplex, etc)
// Print without user intervention (no print window)
//
// Required free library to load a PDF document into DotNet
// - Right-click project, Manage Nuget packages
// - Add PdfiumViewer
// - Add PdfiumViewer.Native.x86.v8-xfa
//---------------------------------------------
namespace Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Select report properties
private void btnMakeReport_Click(object sender, EventArgs e)
{
// Select a PDF report that was generated by another module
string Pdf = @"C:\Temp\Doc.pdf";
// Configure printing properties required for this report
string printer = ""; // Default printer
PaperSize paperSize = null; // Default paper size
PaperSource paperSource = null; // Default paper source
int orientation = 0; // Portrait
Duplex duplex = Duplex.Vertical; // Duplex - long edge
short copies = 0; // Default = single copy
int fromPage = 0; // From first page
int toPage = 0; // To last page
// Print report without any user intervention
PrintPDF(Pdf, printer, paperSize, paperSource, orientation, duplex, copies, fromPage, toPage);
}
/// <summary>
/// Send a PDF document to printer with pre-defined settings
/// </summary>
/// <param name="filename">Name of PDF document to print</param>
/// <param name="printer">Name of output printer</param>
/// <param name="paperSize">RawKind of selected paper size (1=Letter / 5=Legal / 17=Ledger)</param>
/// <param name="paperSource">RawKind of selected paper source (different for each printer driver)</param>
/// <param name="orientation">0=Portrait / 1=Landscape</param>
/// <param name="duplex">Duplex.Simplex / Duplex.Vertical / Duplex.Horizontal</param>
/// <param name="copies">Number of printed copies</param>
/// <param name="fromPage">First page to print</param>
/// <param name="toPage">Last page to print</param>
private void PrintPDF(string filename, string printer, PaperSize paperSize, PaperSource paperSource, int orientation, Duplex duplex, short copies, int fromPage, int toPage)
{
using (var document = PdfDocument.Load(filename))
{
using (var pd = document.CreatePrintDocument())
{
// Create the printer settings for our printer
if (printer.Length > 0)
pd.PrinterSettings.PrinterName = printer;
if (paperSize != null)
pd.DefaultPageSettings.PaperSize = paperSize;
if (paperSource != null)
pd.DefaultPageSettings.PaperSource = paperSource;
pd.DefaultPageSettings.Landscape = (orientation > 0);
pd.PrinterSettings.Duplex = duplex;
if (copies > 0)
pd.PrinterSettings.Copies = copies;
if (fromPage > 0)
pd.PrinterSettings.FromPage = fromPage;
if (toPage > 0)
pd.PrinterSettings.ToPage = toPage;
pd.PrintController = new StandardPrintController();
pd.Print();
}
}
}
}
}