1. Home
  2. Foxit Quick PDF Library
  3. Programmatically render PDF files in Windows Forms with .NET and C#

Programmatically render PDF files in Windows Forms with .NET and C#

Foxit Quick PDF Library can render a PDF as an image so that you can place it in your .NET application. Sample code using C# is provided below.

private void Form1_Load(object sender, EventArgs e)
{
// Remember to initialize the library and call the 
// UnlockKey function with your license key before
// calling the following code. 
 
// Open PDF File
int Handle = DPL.DAOpenFile("C:\\sample.pdf", null);
 
// Get Total Number of Pages in a PDF File
int PageCount = DPL.DAGetPageCount(Handle);
 
int PageNo = 1;
 
// It will get Reference of page 1 from PDF file
int PageRefNo = DPL.DAFindPage(Handle, PageNo);
 
// You can change this parameter for Zoom In/Zoom Out purpose
int Zoom = 76;
double pageWidth = DPL.DAGetPageWidth(Handle, PageRefNo) / Zoom;
double pageHeight = DPL.DAGetPageHeight(Handle, PageRefNo) / Zoom;
 
// DPI use for rendering the page. Increase DPI will increate quality of image
int dpi = 92;
 
// Calculate Dimension of final output image
Bitmap b = new Bitmap(Convert.ToInt32(pageWidth * dpi), Convert.ToInt32(pageHeight * dpi));
 
// This will Draw render image on GDI
using (Graphics g = Graphics.FromImage(b))
{
IntPtr dc = (int)g.GetHdc();
qp.DARenderPageToDC(Handle, PageRefNo, dpi, dc);
g.ReleaseHdc((IntPtr)dc);
}
 
// Assign rendered image to PictureBox Control which will display PDF on Windows Form.
pictureBox1.Image = b;
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
}

The above code will work correctly if you do not use the C# import class. If you are using the C# import class then it is not necessary to convert dc into an int as the import class accepts a IntPtr as one of the parameters for the DARenderPageToDC function. So the code would look like this instead.

IntPtr dc = g.GetHdc();
qp.DARenderPageToDC(Handle, PageRefNo, dpi, dc);
g.ReleaseHdc(dc);
Updated on April 10, 2017

Was this article helpful?

Related Articles

Ready to try Foxit PDF SDK?
Click the link below to download your trial
Free Trial