@TRobson
Printing facilities of the Aspose.PDF for .NET library are represented by the Aspose.Pdf.Facades.PdfViewer class. It exposes the PdfViewer.PdfQueryPageSettings event that allows to set individual page settings, including a paper source.
For an example of using the PdfViewer.PdfQueryPageSettings please refer to the following:
public static void Print(string inputFile, string printerName, int duplexMode, int firstTray, int followerTray)
{
_firstTray = firstTray;
_followerTray = followerTray;
PdfViewer viewer = new PdfViewer();
Aspose.Pdf.Printing.PrinterSettings settings = new Aspose.Pdf.Printing.PrinterSettings();
Aspose.Pdf.Printing.PageSettings pageSettings = new Aspose.Pdf.Printing.PageSettings();
PrintDocument doc = new PrintDocument();
viewer.OpenPdfFile(inputFile);
settings.PrinterName = doc.PrinterSettings.PrinterName;
if (ConverterConfig.Instance != null)
viewer.Resolution = ConverterConfig.Instance.PdfSettings.PdfPrintResolution;
PrintSettings = settings;
if (!printerName.IsNullOrEmpty())
settings.PrinterName = printerName;
if ((duplexMode == 1 || duplexMode == 2 || duplexMode == 3) && doc.PrinterSettings.CanDuplex)
settings.Duplex = duplexMode == 1
? Aspose.Pdf.Printing.Duplex.Simplex
: duplexMode == 2
? Aspose.Pdf.Printing.Duplex.Vertical
: Aspose.Pdf.Printing.Duplex.Horizontal;
currentPageNumber = 1;
// Subscribe to the PdfQueryPageSettings event to pass customized settings for each page
viewer.PdfQueryPageSettings += new PdfQueryPageSettingsEventHandler(doc_QueryPageSettings);
viewer.PrintDocumentWithSettings(pageSettings, settings);
}
static void doc_QueryPageSettings(object sender, PdfQueryPageSettingsEventArgs e, PdfPrintPageInfo info)
{
currentPageNumber++;
foreach (System.Drawing.Printing.PaperSource source in PrintSettings.PaperSources)
{
if (source.RawKind == _firstTray)
{
e.PageSettings.PaperSource = source.ToAsposePaperSource();
}
}
if (currentPageNumber > 1)
{
foreach (System.Drawing.Printing.PaperSource source in PrintSettings.PaperSources)
{
if (source.RawKind == _followerTray)
{
e.PageSettings.PaperSource = source.ToAsposePaperSource();
}
}
}
}
Also, if the you prefer to work with the PrintDocument directly, you may use the Aspose.Pdf.PrintController class. For an example of changing page settings with the PrintController, please refer to code snippets:
internal class Class2
{
internal void PDFNEWNET_45624()
{
var pdfLicence = new Aspose.Pdf.License();
pdfLicence.SetLicense("Aspose.Total.NET.lic");
string outFile = "C:\\Test\\45624.pdf";
var doc = new PrintDocument();
doc.PrintPage += new PrintPageEventHandler(mPrintDoc_PrintPage);
// Subscribe to the PrintDocument.QueryPageSettings event to set individual page settings for every page
doc.QueryPageSettings += new QueryPageSettingsEventHandler(mPrintDoc_QueryPageSettings);
var printController = new Aspose.Pdf.PrintController();
printController.FileName = outFile;
doc.PrintController = printController;
doc.Print();
}
private void mPrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawRectangle(new Pen(System.Drawing.Color.Red), 0, 0, 100, 100);
e.Graphics.FillEllipse(new SolidBrush(System.Drawing.Color.Blue), 100, 100, 200, 200);
}
private void mPrintDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
{
e.PageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 10, 5);
e.PageSettings.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0); // Default margins of the Aspose.Pdf.PrintController are (72, 72, 72, 72)
}
}
If you want to set the same page settings for the whole document, you may use the PrintDocument.DefaultPageSettings property instead:
internal class Class2
{
internal void PDFNEWNET_45624()
{
var pdfLicence = new Aspose.Pdf.License();
pdfLicence.SetLicense("Aspose.Total.NET.lic");
string outFile = "C:\\Test\\45624.pdf";
var doc = new PrintDocument();
doc.PrintPage += new PrintPageEventHandler(mPrintDoc_PrintPage);
// Use the PrintDocument.DefaultPageSettings property to set page settings for the whole document
doc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 10, 5);
doc.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0); // Default margins of the Aspose.Pdf.PrintController are (72, 72, 72, 72)
var printController = new Aspose.Pdf.PrintController();
printController.FileName = outFile;
doc.PrintController = printController;
doc.Print();
}
private void mPrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawRectangle(new Pen(System.Drawing.Color.Red), 0, 0, 100, 100);
e.Graphics.FillEllipse(new SolidBrush(System.Drawing.Color.Blue), 100, 100, 200, 200);
}
}
Please also note that your desired page size cannot be less than page margins summary size, otherwise you’ll get an exception saying that there is not enough space on the page.
There is a bug in a library - Margins set in a page settings are not applied, the Aspose.Pdf.PrintController always uses its default margins of (72, 72, 72, 72). This bug is fixed in the 24.7 version.
In versions prior to 24.7 please use the following formulas to define the page size:
e.PageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 72 + 72 + desiredWidth, 72 + 72 + desiredHeight);