Creating an application to print files can be difficult. Luckily, LEADTOOLS supports rasterizing and rendering various file formats via a Windows service or console application. The project below can print any of LEAD’s 150+ supported file formats.
using (var document = DocumentFactory.LoadFromFile(@"filename.docx", new LoadDocumentOptions()))
PrintDocument(document);
static void PrintDocument(LEADDocument document)
{
using (var printDocument = new PrintDocument())
{
printDocument.PrinterSettings.MinimumPage = 1;
printDocument.PrinterSettings.MaximumPage = document.Pages.Count;
printDocument.PrinterSettings.FromPage = 1;
printDocument.PrinterSettings.ToPage = document.Pages.Count;
printDocument.PrinterSettings.PrinterName = "Adobe PDF";
printDocument.DefaultPageSettings = new PageSettings();
var pageNumber = printDocument.PrinterSettings.FromPage;
printDocument.PrintPage += (object sender, PrintPageEventArgs e) => PrintPageHandler(e, document, printDocument, ref pageNumber);
printDocument.Print();
}
}
static void PrintPageHandler(PrintPageEventArgs e, LEADDocument document, PrintDocument printDocument, ref int pageNumber)
{
PrintPage(document, pageNumber, e);
pageNumber++;
e.HasMorePages = (pageNumber <= printDocument.PrinterSettings.ToPage);
if (!e.HasMorePages)
pageNumber = 1;
}
static void PrintPage(LEADDocument document, int pageNumber, PrintPageEventArgs e)
{
var page = document.Pages[pageNumber - 1];
// Get page size in pixels
var pixelSize = page.SizeToPixels(page.Size);
// Convert to DPI
var size = LeadSizeD.Create(pixelSize.Width * 96.0 / page.Resolution, pixelSize.Height * 96.0 / page.Resolution).ToLeadSize();
// Fit in the margin bounds
var destRect = LeadRect.Create(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, e.MarginBounds.Height);
destRect = RasterImage.CalculatePaintModeRectangle(size.Width, size.Height, destRect, RasterPaintSizeMode.Fit, RasterPaintAlignMode.Center, RasterPaintAlignMode.Center);
// Get the page image
using (var rasterImage = page.GetImage())
using (var bitmap = RasterImageConverter.ConvertToImage(rasterImage, ConvertToImageOptions.None))
e.Graphics.DrawImage(bitmap, destRect.X, destRect.Y, destRect.Width, destRect.Height);
}
Don’t want to print to a physical printer? Print a file using LEADTOOL’s Virtual Printer driver. The LEADTOOLS virtual printer driver offers the ultimate in flexibility. It can redirect print jobs to multiple targets, including physical printers, PDF files on disk, and blobs in a DB. Also, pages can be processed with LEADTOOLS advanced image processing functions, including the ability to redact sensitive information before being sent to the target. This flexibility provides a means to extend any third-party application that can print.
Try for Free
Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days, and comes with free chat and email support.
Any Questions?
Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at 704-332-5532.