@Bhumika.Shah
System.Drawing.Printing.PrinterSettings was replaced with Aspose.Pdf.Printing.PrinterSettings starting from Aspose.PDF 23.10. This is made to support printing under Linux as well as under Windows.
public static void PrintingPDFFile()
{
// Create PdfViewer object
PdfViewer viewer = new PdfViewer();
// Open input PDF file
viewer.BindPdf(_dataDir + "sample.pdf");
// Set attributes for printing
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
// Create objects for printer and page settings and PrintDocument
Aspose.Pdf.Printing.PrinterSettings ps = new Aspose.Pdf.Printing.PrinterSettings();
Aspose.Pdf.Printing.PageSettings pgs = new Aspose.Pdf.Printing.PageSettings();
System.Drawing.Printing.PrintDocument prtdoc = new System.Drawing.Printing.PrintDocument();
// Set printer name
ps.PrinterName = prtdoc.PrinterSettings.PrinterName;
// Set PageSize (if required)
pgs.PaperSize = new Aspose.Pdf.Printing.PaperSize("A4", 827, 1169);
// Set PageMargins (if required)
pgs.Margins = new Aspose.Pdf.Devices.Margins(0, 0, 0, 0);
// Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
// Close the PDF file after printing
viewer.Close();
}
public static void PrintingPDFDisplayPrintDialog()
{
// Create PdfViewer object
PdfViewer viewer = new PdfViewer();
// Open input PDF file
viewer.BindPdf(_dataDir + "sample.pdf");
// Set attributes for printing
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
// Create objects for printer and page settings and PrintDocument
Aspose.Pdf.Printing.PageSettings pgs = new Aspose.Pdf.Printing.PageSettings
{
// Set PageSize (if required)
PaperSize = new Aspose.Pdf.Printing.PaperSize("A4", 827, 1169),
// Set PageMargins (if required)
Margins = new Aspose.Pdf.Devices.Margins(0, 0, 0, 0)
};
System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog();
if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// Document printing code goes here
// Print document using printer and page settings
Aspose.Pdf.Printing.PrinterSettings ps = printDialog.PrinterSettings;
viewer.PrintDocumentWithSettings(pgs, ps);
}
// Close the PDF file after priting
viewer.Close();
}
Note: PrintDialog isn’t available in the NET Core Version 3.1 and later. Can it be invoked from Application.Current.Dispatcher.CurrentThread ? Check this.
public static void PrintingPDFToSoftPrinter()
{
// Create PdfViewer object
PdfViewer viewer = new PdfViewer();
// Open input PDF file
viewer.BindPdf(_dataDir + "sample.pdf");
// Set attributes for printing
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
viewer.PrintAsImage = false;
// Create objects for printer and page settings and PrintDocument
Aspose.Pdf.Printing.PrinterSettings ps = new Aspose.Pdf.Printing.PrinterSettings();
Aspose.Pdf.Printing.PageSettings pgs = new Aspose.Pdf.Printing.PageSettings();
// Set printer name
//ps.PrinterName = "HP Universal Printing PS (v7.0.0)";
// Or set the PDF printer
ps.PrinterName = "Adobe PDF";
// Set PageSize (if required)
pgs.PaperSize = new Aspose.Pdf.Printing.PaperSize("A4", 827, 1169);
// Set PageMargins (if required)
pgs.Margins = new Aspose.Pdf.Devices.Margins(0, 0, 0, 0);
// Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
// Close the PDF file after priting
viewer.Close();
}
public static void PrintingPDFHidePrintDialog()
{
// Create PdfViewer object
PdfViewer viewer = new PdfViewer();
// Open input PDF file
viewer.BindPdf(_dataDir + "sample.pdf");
// Set attributes for printing
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
// Create objects for printer and page settings and PrintDocument
Aspose.Pdf.Printing.PrinterSettings ps = new Aspose.Pdf.Printing.PrinterSettings();
Aspose.Pdf.Printing.PageSettings pgs = new Aspose.Pdf.Printing.PageSettings();
// Set XPS/PDF printer name
ps.PrinterName = "OneNote for Windows 10";
// Set PageSize (if required)
pgs.PaperSize = new Aspose.Pdf.Printing.PaperSize("A4", 827, 1169);
// Set PageMargins (if required)
pgs.Margins = new Aspose.Pdf.Devices.Margins(0, 0, 0, 0);
// Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
// Close the PDF file after priting
viewer.Close();
}
public static void PrintingPDFasGrayscale()
{
// Create PdfViewer object
PdfViewer viewer = new PdfViewer();
// Open input PDF file
viewer.BindPdf(_dataDir + "sample.pdf");
// Set attributes for printing
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
viewer.PrintAsGrayscale = false;
// Create objects for printer and page settings and PrintDocument
Aspose.Pdf.Printing.PrinterSettings ps = new Aspose.Pdf.Printing.PrinterSettings();
Aspose.Pdf.Printing.PageSettings pgs = new Aspose.Pdf.Printing.PageSettings();
// Set XPS/PDF printer name
ps.PrinterName = "OneNote for Windows 10";
// Set PageSize (if required)
pgs.PaperSize = new Aspose.Pdf.Printing.PaperSize("A4", 827, 1169);
// Set PageMargins (if required)
pgs.Margins = new Aspose.Pdf.Devices.Margins(0, 0, 0, 0);
// Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
// Close the PDF file after priting
viewer.Close();
}
public static void CheckingPrintJobStatus()
{
// Create PdfViewer object
PdfViewer viewer = new PdfViewer();
// Open input PDF file
viewer.BindPdf(_dataDir + "sample1.pdf");
// Set attributes for printing
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
viewer.PrintAsImage = false;
// Create objects for printer and page settings and PrintDocument
Aspose.Pdf.Printing.PrinterSettings ps = new Aspose.Pdf.Printing.PrinterSettings();
Aspose.Pdf.Printing.PageSettings pgs = new Aspose.Pdf.Printing.PageSettings();
// Set XPS/PDF printer name
ps.PrinterName = "HP Universal Printing PS (v7.0.0)";
// Set output file name and PrintToFile attribute
ps.PrintFileName = _dataDir + "PdfToPostScript_out.ps";
ps.PrintToFile = true;
// Set PageSize (if required)
pgs.PaperSize = new Aspose.Pdf.Printing.PaperSize("A4", 827, 1169);
// Set PageMargins (if required)
pgs.Margins = new Aspose.Pdf.Devices.Margins(0, 0, 0, 0);
// Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
// Check the print status
if (viewer.PrintStatus != null && viewer.PrintStatus is Exception ex)
{
Console.WriteLine(ex.Message);
}
else
{
// No errors were found. Printing job has been completed successfully
Console.WriteLine("Printing completed without any issue..");
}
// Close the PDF file after printing
viewer.Close();
}
struct PrintingJobSettings
{
public int ToPage { get; set; }
public int FromPage { get; set; }
public string OutputFile { get; set; }
public Aspose.Pdf.Printing.Duplex Mode { get; set; }
}
public static void PrintingPagesInSimplexAndDuplexMode()
{
int printingJobIndex = 0;
string inPdf = _dataDir + "sample-8page.pdf";
string output = _dataDir;
IList<PrintingJobSettings> printingJobs = new List<PrintingJobSettings>();
PrintingJobSettings printingJob1 = new PrintingJobSettings
{
FromPage = 1,
ToPage = 3,
OutputFile = output + "sample_1_3.xps",
Mode = Aspose.Pdf.Printing.Duplex.Default
};
printingJobs.Add(printingJob1);
PrintingJobSettings printingJob2 = new PrintingJobSettings
{
FromPage = 4,
ToPage = 6,
OutputFile = output + "sample_4_6.xps",
Mode = Aspose.Pdf.Printing.Duplex.Simplex
};
printingJobs.Add(printingJob2);
PrintingJobSettings printingJob3 = new PrintingJobSettings
{
FromPage = 7,
ToPage = 7,
OutputFile = output + "sample_7.xps",
Mode = Aspose.Pdf.Printing.Duplex.Default
};
printingJobs.Add(printingJob3);
PdfViewer viewer = new PdfViewer();
viewer.BindPdf(inPdf);
viewer.AutoResize = true;
viewer.AutoRotate = true;
viewer.PrintPageDialog = false;
Aspose.Pdf.Printing.PrinterSettings ps = new Aspose.Pdf.Printing.PrinterSettings();
Aspose.Pdf.Printing.PageSettings pgs = new Aspose.Pdf.Printing.PageSettings();
ps.PrinterName = "Microsoft XPS Document Writer";
ps.PrintFileName = System.IO.Path.GetFullPath(printingJobs[printingJobIndex].OutputFile);
ps.PrintToFile = true;
ps.FromPage = printingJobs[printingJobIndex].FromPage;
ps.ToPage = printingJobs[printingJobIndex].ToPage;
ps.Duplex = printingJobs[printingJobIndex].Mode;
ps.PrintRange = Aspose.Pdf.Printing.PrintRange.SomePages;
pgs.PaperSize = new Aspose.Pdf.Printing.PaperSize("A4", 827, 1169);
ps.DefaultPageSettings.PaperSize = pgs.PaperSize;
pgs.Margins = new Aspose.Pdf.Devices.Margins(0, 0, 0, 0);
viewer.EndPrint += (sender, args) =>
{
if (++printingJobIndex < printingJobs.Count)
{
ps.PrintFileName = System.IO.Path.GetFullPath(printingJobs[printingJobIndex].OutputFile);
ps.FromPage = printingJobs[printingJobIndex].FromPage;
ps.ToPage = printingJobs[printingJobIndex].ToPage;
ps.Duplex = printingJobs[printingJobIndex].Mode;
viewer.PrintDocumentWithSettings(pgs, ps);
}
};
viewer.PrintDocumentWithSettings(pgs, ps);
}
You can please use latest version of Aspose.Pdf.Drawing in .NET 6.0/7.0 environment. Please let us know if using the latest version does not resolve your issue.