private void btnPrintPdf_Click(object sender, EventArgs e)
{
if (pdfFileTextBox.Text.Trim().Equals(String.Empty))
{
MessageBox.Show("Please choose a source PDF file", "Choose PDF file", MessageBoxButtons.OK);
return;
}
// the source pdf file
string pdfFileName = pdfFileTextBox.Text.Trim();
// start page number
int startPageNumber = int.Parse(textBoxStartPage.Text.Trim());
// end page number
// when it is 0 the conversion will continue up to the end of document
int endPageNumber = 0;
if (textBoxEndPage.Text.Trim() != String.Empty)
endPageNumber = int.Parse(textBoxEndPage.Text.Trim());
// create the PDF printer
PdfPrint pdfPrint = new PdfPrint();
// set the license key
pdfPrint.LicenseKey = "OrSltaGgtaW1o7ultaaku6Snu6ysrKy1pQ==";
// set the document name
pdfPrint.DocumentName = "PDF Silent Printing";
// enable or disable color printing
pdfPrint.DefaultPageSettings.Color = cbPrintColor.Checked;
// set the PDF printing color and resolution
pdfPrint.Color = GetSelectedPrintColor();
pdfPrint.Resolution = int.Parse(textBoxResolution.Text);
// select the printer
string selectedPrinterName = GetSelectedPrinterName();
if (selectedPrinterName != null)
pdfPrint.PrinterSettings.PrinterName = selectedPrinterName;
// set paper size
PaperSize selectedPaperSize = GetSelectedPaperSize();
if (selectedPaperSize != null)
pdfPrint.DefaultPageSettings.PaperSize = selectedPaperSize;
// set paper orientation
pdfPrint.DefaultPageSettings.Landscape = GetSelectedPageOrientation() == "Landscape";
// set paper margins
pdfPrint.DefaultPageSettings.Margins = new Margins((int)(float.Parse(leftMarginTextBox.Text) * 100),
(int)(float.Parse(rightMarginTextBox.Text) * 100),
(int)(float.Parse(topMarginTextBox.Text) * 100),
(int)(float.Parse(bottomMarginTextBox.Text) * 100));
// the demo output directory
string outputDirectory = Path.Combine(Application.StartupPath, @"DemoFiles\Output");
Cursor = Cursors.WaitCursor;
try
{
pdfPrint.Print(pdfFileName, startPageNumber, endPageNumber);
}
catch (Exception ex)
{
// The conversion failed
MessageBox.Show(String.Format("An error occurred. {0}", ex.Message), "Error");
return;
}
finally
{
Cursor = Cursors.Arrow;
}
MessageBox.Show("Print Completed", "Print Completed", MessageBoxButtons.OK);
}