)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to generate PDF from Print document

Platform: WinForms |
Control: PDF |
Published Date: January 28, 2016 |
Last Revised Date: September 13, 2019
Tags: pdf, metafile

The Syncfusion Essential PDF is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.

This sample explains how to generate PDF file from Print document in C# and VB.NET.

Steps to generate PDF file from Print document programmatically in C#:

  1. Create a new C# console application project. Create a console application in visual studio
  2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org. Refer NuGet package to the project
  3. Include the following namespaces in Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.IO

 

  1. The PDF document can be generated from a print document by using the PdfImage class. Include the following code snippet in main method of program.cs file.

C#

//Initialize PrintDocument
PrintDocument printDocument = new PrintDocument();
 
//Create an event to print the document
printDocument.PrintPage += printDocument_PrintPage;
printDocument.Print();
 
//Get the EMF files in folder into string array
string[] files = Directory.GetFiles("../../Input", "*.emf");
 
//Create a new PDF document
PdfDocument document = new PdfDocument();
 
//Draw the images
for (int i = 0; i < files.Length; i++)
{
    //Add page to the PDF document
    PdfPage page = document.Pages.Add();
 
    //Get the image into PdfImage
    PdfImage image = PdfImage.FromFile(files[0]);
 
    //Draw the image on PDF page
    page.Graphics.DrawImage(image, new PointF(0, 0));
}
 
//Save the PDF document
document.Save("Output.pdf");
 
//Close the instance of PdfDocument
document.Close(true);

 

VB.NET

'Initialize PrintDocument
Dim printDocument As PrintDocument = New PrintDocument
 
'Create an event to print the document
AddHandler printDocument.PrintPage, AddressOf printDocument_PrintPage
printDocument.Print()
 
'Get the EMF files in folder into string array
Dim files() As String = Directory.GetFiles("../../Input", "*.emf")
 
'Create a new PDF document
Dim document As PdfDocument = New PdfDocument
 
'Draw the images
For i As Integer = 0 To files.Length - 1 Step 1
    'Add page to the PDF document
    Dim page As PdfPage = document.Pages.Add
 
    'Get the image into PdfImage
    Dim image As PdfImage = PdfImage.FromFile(files(0))
 
    'Draw the image on PDF page
    page.Graphics.DrawImage(image, New PointF(0, 0))
Next
 
'Save the PDF document
document.Save("Output.pdf")
 
'Close the instance of PdfDocument
document.Close(True)

 

  1. Include the following code snippet in definition part of event.

C#

//Create font and brush to draw the string
Font font = new Font("Arial", 16, FontStyle.Regular);
Brush brush = Brushes.Black;
 
//Initialize Bitmap with PageBounds
Bitmap bitmap = new Bitmap(args.PageBounds.Width, args.PageBounds.Height);
 
//Get the image in folder into Image
Image image = new Bitmap("../../Input/1-Pictures-icon.png");
Image metafile = null;
 
//Initialize memory stream
MemoryStream stream = new MemoryStream();
 
//Get the graphics from Bitmap
using (Graphics graphics = Graphics.FromImage(bitmap))
{
    IntPtr hdc = graphics.GetHdc();
    RectangleF rect = new RectangleF(0, 0, bitmap.Width, bitmap.Height);
    metafile = new Metafile(stream, hdc, rect, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual);
    graphics.ReleaseHdc();
    graphics.Dispose();
}
 
//Get the graphics from metafile
using (Graphics graphics = Graphics.FromImage(metafile))
{
    //Draw the string and image
    graphics.DrawString("Generate PDF from Print Document", font, brush, new PointF(213, 0));
    graphics.DrawImage(image, new RectangleF(170, 20, 350, 350));
    graphics.Dispose();
}
 
//Get the emf image in folder into FileStream
using (FileStream fileStream = new FileStream("../../Input/image.emf", FileMode.OpenOrCreate))
{
    stream.WriteTo(fileStream);
}
 
//Dispose the instance of MemoryStream
stream.Dispose();

 

VB.NET

'Create font and brush to draw the string
Dim font As Font = New Font("Arial", 16, FontStyle.Regular)
Dim brush As Brush = Brushes.Black
 
'Initialize Bitmap with PageBounds
Dim bitmap As Bitmap = New Bitmap(args.PageBounds.Width, args.PageBounds.Height)
 
'Get the image in folder into Image
Dim image As Image = New Bitmap("../../Input/1-Pictures-icon.png")
Dim metafile As Image = Nothing
 
'Initialize memory stream
Dim stream As MemoryStream = New MemoryStream
 
'Get the graphics from Bitmap
Using graphics As Graphics = Graphics.FromImage(bitmap)
    Dim hdc As IntPtr = graphics.GetHdc()
    Dim rect As RectangleF = New RectangleF(0, 0, bitmap.Width, bitmap.Height)
    metafile = New Metafile(stream, hdc, rect, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual)
    graphics.ReleaseHdc()
    graphics.Dispose()
End Using
 
'Get the graphics from metafile
Using graphics As Graphics = Graphics.FromImage(metafile)
    graphics.DrawString("Generate PDF from Print Document", font, brush, New PointF(213, 0))
    graphics.DrawImage(image, New RectangleF(170, 20, 350, 350))
    graphics.Dispose()
End Using
 
'Get the emf image in folder into FileStream
Using fileStream As FileStream = New FileStream("../../Input/image.emf", FileMode.OpenOrCreate)
    stream.WriteTo(fileStream)
End Using
 
'Dispose the instance of MemoryStream
stream.Dispose()

 

A complete work sample to generate a PDF file from Print document can be downloaded from PDFfromPrintDoc.zip.

By executing the program, you will get the PDF document as follows.

Output document

Take a moment to peruse the documentation, where you will find other options like adding new page in a PDF document, importing pages from existing PDF document, re-arranging and changing page numbers in existing PDF document, rotating PDF page, splitting a PDF document into individual pages.

Click here to explore the rich set of Syncfusion Essential PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon