PDF to Printer
The below example demonstrates how you can take any PDF document and using DynamicPDF Rasterizer for .NET convert to a rasterized System.Drawing.Bitmap object of that PDF. Then it shows how to send that object directly to a printer.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using ceTe.DynamicPDF.Rasterizer;
namespace ConsoleApplication1
{
class MyClass
{
static void Main()
{
// Instantiate the PdfPrint sub class
PdfPrint pdfPrint = new PdfPrint(@"C:\\DocumentA.pdf");
// Print the PDF
pdfPrint.Print();
}
class PdfPrint
{
private PdfRasterizer rasterizer;
private int currentPage = 0;
public PdfPrint(string filePath)
{
this.rasterizer = new PdfRasterizer(filePath);
}
public void Print()
{
// Create a PrintDocument
PrintDocument printDocument = new PrintDocument();
// Set its margins
printDocument.DefaultPageSettings.Margins.Left = 0;
printDocument.DefaultPageSettings.Margins.Top = 0;
printDocument.DefaultPageSettings.Margins.Right = 0;
printDocument.DefaultPageSettings.Margins.Bottom = 0;
printDocument.OriginAtMargins = true;
// Set the PrintPage event handler
printDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
// Start printing
printDocument.Print();
}
// This event handler will be called for each page
public void OnPrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Point;
// Get a bitmap and add it to the page
Bitmap bitmap = rasterizer.Pages[currentPage++].DrawToBitmap(BitmapColorFormat.Rgb, ImageSize.Dpi300);
e.Graphics.DrawImage(bitmap, 0, 0);
// Sets HasMorePages to false when the last page has been reached
e.HasMorePages = currentPage != rasterizer.Pages.Count;
}
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Text
Imports ceTe.DynamicPDF.Rasterizer
Module myModule
Sub Main()
' Instantiate the PdfPrint sub class
Dim pdfPrint As PdfPrint = New PdfPrint("C:\DocumentA.pdf")
' Print the PDF
pdfPrint.Print()
End Sub
Class PdfPrint
Dim rasterizer As PdfRasterizer
Dim currentPage As Integer = 0
Public Sub New(ByVal filePath As String)
Me.rasterizer = New PdfRasterizer(filePath)
End Sub
Public Sub Print()
' Create a PrintDocument
Dim printDocument As PrintDocument = New PrintDocument()
' Set its margins
printDocument.DefaultPageSettings.Margins.Left = 0
printDocument.DefaultPageSettings.Margins.Top = 0
printDocument.DefaultPageSettings.Margins.Right = 0
printDocument.DefaultPageSettings.Margins.Bottom = 0
printDocument.OriginAtMargins = True
' Set the PrintPage event handler
AddHandler printDocument.PrintPage, New PrintPageEventHandler(AddressOf OnPrintPage)
' Start printing
printDocument.Print()
End Sub
' This event handler will be called for each page
Public Sub OnPrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
e.Graphics.PageUnit = GraphicsUnit.Point
' Get a bitmap and add it to the page
Dim bitmap As Bitmap = rasterizer.Pages(currentPage).DrawToBitmap(BitmapColorFormat.Rgb, ImageSize.Dpi300)
currentPage = currentPage + 1
e.Graphics.DrawImage(bitmap, 0, 0)
' Sets HasMorePages to false when the last page has been reached
e.HasMorePages = currentPage <> rasterizer.Pages.Count
End Sub
End Class
End Module