AVAILABLE NOW: Spring 2025 Release
By Apryse | 2013 Jul 20
A common question we receive from our customers is how do I create thumbnails of a PDF document? The best class to use for this is PDFDraw, which rasterizes* PDF pages one at a time, and provides methods for saving these images in a variety of formats. The C# code snippet below shows how to extract all of the pages from a document and save them as JPEGs with a quality of 80.
// A collection of rendering 'hints'.
ObjSet hint_set=new ObjSet();
try
{
using (PDFDoc doc = new PDFDoc("myDocument.pdf"))
{
// Initialize the security handler, in case the PDF is encrypted.
doc.InitSecurityHandler();
// Set the output resolution is to 72 DPI.
draw.SetDPI(72);
// Use optional encoder parameter to specify JPEG quality.
Obj encoder_param = hint_set.CreateDict();
encoder_param.PutNumber("Quality", 80);
// Traverse all pages in the document.
for (PageIterator itr=doc.GetPageIterator(); itr.HasNext(); itr.Next())
{
string outname = string.Format("{0}myDocument{1:d}.jpg", output_path, itr.GetPageNumber());
Console.WriteLine("Result saved in {0}", outname);
draw.Export(itr.Current(), outname, "JPEG", encoder_param);
}
}
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
One thing of note in this sample is the use of a PageIterator. Using a PageIterator is more efficient than repeatedly calling GetPage(i), because GetPage(int pageNumber) is an O(n) operation.
For more example code on how to use PDFDraw, see the PDFDraw sample on our sample code page.
Apryse
Share this post
PRODUCTS
Platform Integrations
End User Applications
Popular Content