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.

  • When we speak of “rasterizing” a PDF, we mean convert it to bitmapped image data. PDF documents contain vector elements such as lines, text, gradients, etc. that are defined formulaically, and hence can be accurately viewed at any resolution. When you convert a PDF page to an image, you are converting it to raster data at a specific resolution.