User Guide > How to Work with... > Common Operations > Printing |
This topic provides information about the following:
The following example demonstrates using the System.Windows.Controls.Canvas class and how to have a function that can print different file types, (images, PDF, CAD, etc.). This is done by loading the file in an ImGearPage, drawing the ImGearPage to a new Canvas, then printing that Canvas.
C# |
Copy Code |
---|---|
using System.Printing; using System.Windows.Controls; using System.Drawing.Printing; using ImageGear.Core; using ImageGear.Formats; using ImageGear.Vector; public void PrintPage() { ImGearPage pageToPrint = null; SaveFileDialog dialog = new SaveFileDialog(); // Run the dialog and load a file into the ImGearPage, pageToPrint. if (dialog.ShowDialog(this) == DialogResult.OK) { Stream stream = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read); pageToPrint = ImGearFileFormats.LoadPage(stream, 0); } System.Windows.Controls.PrintDialog pDialog = new System.Windows.Controls.PrintDialog(); if (pDialog.ShowDialog() == true) { // Set up scale settings. ImGearRenderSettings rs = new ImGearRenderSettings(); // Use native scale settings. Provide specific resolution. ImGearNativeDisplayScaleSettings settings = new ImGearNativeDisplayScaleSettings(); System.Printing.PrintTicket pt = pDialog.PrintTicket; rs.ImageScaleSettings = settings; Canvas printCanvas = new Canvas(); // Set up Display settings to print whole image. ImGearPresentationPageDisplay pageDisplay = new ImGearPresentationPageDisplay(pageToPrint); pageDisplay.Layout.FitMode = ImGearFitModes.FIT_TO_DEVICE; // printCanvas.Width, printCanvas.Height must be set before pageDisplay.DrawToCanvas, when printing without a pageView. printCanvas.Width = pDialog.PrintableAreaWidth; printCanvas.Height = pDialog.PrintableAreaHeight; // Draw the image on the printCanvas. pageDisplay.DrawToCanvas(printCanvas, rs); // Measure and arrange canvas. printCanvas.Measure(new System.Windows.Size(pDialog.PrintableAreaWidth, pDialog.PrintableAreaHeight)); printCanvas.Arrange(new Rect(printCanvas.DesiredSize)); // Print the canvas. pDialog.PrintVisual(printCanvas, "Print ImageGear page"); } } |
VB.NET |
Copy Code |
---|---|
Imports System.Printing Imports System.Windows.Controls Imports System.Drawing.Printing Imports ImageGear.Core Imports ImageGear.Formats Imports ImageGear.Vector Public Sub PrintPage() Dim pageToPrint As ImGearPage = Nothing Dim dialog As New SaveFileDialog() ' Run the dialog and load a file into the ImGearPage, pageToPrint. If dialog.ShowDialog(Me) = DialogResult.OK Then Dim stream As Stream = New FileStream(dialog.FileName, FileMode.Open, FileAccess.Read) pageToPrint = ImGearFileFormats.LoadPage(stream, 0) End If Dim pDialog As New System.Windows.Controls.PrintDialog() If pDialog.ShowDialog() = True Then ' Set up scale settings. Dim rs As New ImGearRenderSettings() ' Use native scale settings. Provide specific resolution. Dim settings As New ImGearNativeDisplayScaleSettings() Dim pt As System.Printing.PrintTicket = pDialog.PrintTicket rs.ImageScaleSettings = settings Dim printCanvas As New Canvas() ' Set up Display settings to print whole image. Dim pageDisplay As New ImGearPresentationPageDisplay(pageToPrint) pageDisplay.Layout.FitMode = ImGearFitModes.FIT_TO_DEVICE ' printCanvas.Width, printCanvas.Height must be set before pageDisplay.DrawToCanvas, when printing without a pageView. printCanvas.Width = pDialog.PrintableAreaWidth printCanvas.Height = pDialog.PrintableAreaHeight ' Draw the image on the printCanvas. pageDisplay.DrawToCanvas(printCanvas, rs) ' Measure and arrange canvas. printCanvas.Measure(New System.Windows.Size(pDialog.PrintableAreaWidth, pDialog.PrintableAreaHeight)) printCanvas.Arrange(New Rect(printCanvas.DesiredSize)) ' Print the canvas. pDialog.PrintVisual(printCanvas, "Print ImageGear page") End If End Sub |
Be sure to add the required references to your project for the formats you are printing. |
The above snippet uses Native Scaling. More information on Native Scaling and WPF scaling can be found here:
The following example demonstrates how to print using the ImGearPageDisplay.Print method. This method draws the image on .NET graphics for printing.
C# |
Copy Code |
---|---|
using System.Printing; using System.Windows.Controls; using System.Drawing.Printing; using ImageGear.Core; using ImageGear.Formats; using ImageGear.Vector; private void PrintPage(object sender, EventArgs e) { // Load an image. ImGearPage igPage; using (FileStream localFile = new FileStream("test1.tif", FileMode.Open)) { igPage = ImGearFileFormats.LoadPage(localFile, 0); } // Create a new ImGearPageDisplay based upon an ImGearPage. ImGearPageDisplay igPageDisplay = new ImGearPageDisplay(igPage); // Set the current ImGearPageView to use this new display. imGearPageView.Display = igPageDisplay; // Initialize a PrintDialog for the user to select a printer. using (PrintDocument document = new PrintDocument()) using (PrintDialog printDialog = new PrintDialog()) { printDialog.Document = document; // Set the page range to 1 page. printDialog.AllowSomePages = true; printDialog.PrinterSettings.MinimumPage = 1; printDialog.PrinterSettings.MaximumPage = 1; printDialog.PrinterSettings.FromPage = 1; printDialog.PrinterSettings.ToPage = 1; if (DialogResult.OK == printDialog.ShowDialog(this)) { // Set a name for the print job. document.DocumentName = this.Text; // Define a PrintPage event handler and start printing. document.PrintPage += new PrintPageEventHandler(HandlePrinting); document.Print(); } } } void HandlePrinting(object sender, PrintPageEventArgs args) { // Clone the current Display for use as a printing display. ImGearPageDisplay igPageDisplayPrinting = imGearPageView.Display.Clone(); igPageDisplayPrinting.Page = imGearPageView.Display.Page; // Get the current Zoom settings and disabled fixed zoom. ImGearZoomInfo igZoomInfo = igPageDisplayPrinting.GetZoomInfo(imGearPageView); igZoomInfo.Horizontal.Fixed = igZoomInfo.Vertical.Fixed = false; igPageDisplayPrinting.UpdateZoomFrom(igZoomInfo); // Disable any background before printing. igPageDisplayPrinting.Background.Mode = ImGearBackgroundModes.NONE; // Print to the Graphics device chosen from the PrintDialog. igPageDisplayPrinting.Print(args.Graphics); // Let the PrintDialog know there are no more pages. args.HasMorePages = false; } |
VB.NET |
Copy Code |
---|---|
Imports System.Printing Imports System.Windows.Controls Imports System.Drawing.Printing Imports ImageGear.Core Imports ImageGear.Formats Imports ImageGear.Vector Private Sub PrintPage(sender As Object, e As EventArgs) ' Load an image Dim igPage As ImGearPage Using localFile As New FileStream("test1.tif", FileMode.Open) igPage = ImGearFileFormats.LoadPage(localFile, 0) End Using ' Create a new ImGearPageDisplay based upon an ImGearPage. Dim igPageDisplay As New ImGearPageDisplay(igPage) ' Set the current ImGearPageView to use this new display. imGearPageView.Display = igPageDisplay ' Initialize a PrintDialog for the user to select a printer. Using document As New PrintDocument() Using printDialog As New PrintDialog() printDialog.Document = document ' Set the page range to 1 page. printDialog.AllowSomePages = True printDialog.PrinterSettings.MinimumPage = 1 printDialog.PrinterSettings.MaximumPage = 1 printDialog.PrinterSettings.FromPage = 1 printDialog.PrinterSettings.ToPage = 1 If DialogResult.OK = printDialog.ShowDialog(Me) Then ' Set a name for the print job. document.DocumentName = Me.Text ' Define a PrintPage event handler and start printing. document.PrintPage += New PrintPageEventHandler(AddressOf HandlePrinting) document.Print() End If End Using End Using End Sub Private Sub HandlePrinting(sender As Object, args As PrintPageEventArgs) ' Clone the current Display for use as a printing display. Dim igPageDisplayPrinting As ImGearPageDisplay = imGearPageView.Display.Clone() igPageDisplayPrinting.Page = imGearPageView.Display.Page ' Get the current Zoom settings and disabled fixed zoom. Dim igZoomInfo As ImGearZoomInfo = igPageDisplayPrinting.GetZoomInfo(imGearPageView) igZoomInfo.Horizontal.Fixed = InlineAssignHelper(igZoomInfo.Vertical.Fixed, False) igPageDisplayPrinting.UpdateZoomFrom(igZoomInfo) ' Disable any background before printing. igPageDisplayPrinting.Background.Mode = ImGearBackgroundModes.NONE ' Print to the Graphics device chosen from the PrintDialog. igPageDisplayPrinting.Print(args.Graphics) ' Let the PrintDialog know there are no more pages. args.HasMorePages = False End Sub |
Native PDF document printing renders the document directly to the printer; it’s fast and requires less memory. The following is an example that demonstrates how to print a PDF with the ImGearPDFDocument.Print() method.
C# |
Copy Code |
---|---|
using System.Drawing.Printing; using ImageGear.Core; using ImageGear.Formats; using ImageGear.Formats.PDF; public void PrintPDF(ImGearDocument igDocument) { using (ImGearPDFDocument igPDFDocument = (ImGearPDFDocument)igDocument) { ImGearPDFPrintOptions printOptions = new ImGearPDFPrintOptions(); PrintDocument printDocument = new PrintDocument(); // Use default Windows printer. printOptions.DeviceName = printDocument.PrinterSettings.PrinterName; // Print all pages. printOptions.StartPage = 0; printOptions.EndPage = igDocument.Pages.Count; igPDFDocument.Print(printOptions); } return; } |
VB.NET |
Copy Code |
---|---|
Imports System.Drawing.Printing Imports ImageGear.Core Imports ImageGear.Formats Imports ImageGear.Formats.PDF Public Sub PrintPDF(igDocument As ImGearDocument) Using igPDFDocument As ImGearPDFDocument = DirectCast(igDocument, ImGearPDFDocument) Dim printOptions As New ImGearPDFPrintOptions() Dim printDocument As New PrintDocument() ' Use default Windows printer. printOptions.DeviceName = printDocument.PrinterSettings.PrinterName ' Print all pages. printOptions.StartPage = 0 printOptions.EndPage = igDocument.Pages.Count igPDFDocument.Print(printOptions) End Using Return End Sub |
The .NET reference System.Drawing.dll is required for this snippet. |
ImageGear23.Formats.Pdf Assembly > ImageGear.Formats.PDF Namespace : ImGearPDFPrintOptions Class
ImageGear23.Twain Assembly > ImageGear.TWAIN Namespace : ImGearPrinter Enumeration
ImageGear23.Wpf Assembly > ImageGear.WPF.XPS Namespace > ImGearXPSDocument Class : Print Method
ImageGear.Web.UI Library > ImageGear.Web.UI_namespace > ImageGear.Web.UI.PageView : showPrintDialog Method
ImageGear.Web.UI Library > ImageGear.Web.UI_namespace > ImageGear.Web.UI.PageView : print Method