Q: How do I batch convert PDFs to JPEG/PNG in .NET?
--------------------------
A: As a starting point you may want to take a look at
PDF2ImageBatch.zip in the file section of this group. Please note that
this sample is not included as part of standard PDFNet distribution.
http://pdfnet-sdk.googlegroups.com/web/PDF2ImageBatch.zip
You may also want to take at PDFDraw sample (http://www.pdftron.com/
pdfnet/samplecode.html#PDFDraw).
//------------------------------------------------------------------------------------------
// Copyright (c) 2001-2010 by PDFTron Systems Inc. All Rights
Reserved.
using System;
using System.IO;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;
// This sample shows how to use pdftron.PDF.PDFDraw to batch convert
all
// PDFs stored under a given folder to JPEG.
namespace PDFNetSamples
{
class PDF2ImageBatch
{
static int counter = 0;
static Obj jpeg_hint = null;
public static void ProcessDir(string srcDir, string outDir)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(srcDir, "*.pdf");
foreach (string fileName in fileEntries)
{
Console.WriteLine(fileName);
using (PDFDoc doc = new PDFDoc(fileName))
{
if (doc.InitSecurityHandler())
{ // No password required.
using (PDFDraw draw = new PDFDraw())
{
for (PageIterator i = doc.GetPageIterator(); i.HasNext();
i.Next())
{
string fname = outDir + "fileName_" + (counter++).ToString() +
".jpg";
draw.Export(i.Current(), fname, "jpeg", jpeg_hint);
}
}
}
}
}
// Recurse into subdirectories of this directory.
string[] subdirEntries = Directory.GetDirectories(srcDir);
foreach (string subdir in subdirEntries)
// Do not iterate through reparse points
if ((File.GetAttributes(subdir) & FileAttributes.ReparsePoint)
!= FileAttributes.ReparsePoint)
ProcessDir(subdir, outDir);
}
static void Main(string[] args)
{
try
{
PDFNet.Initialize();
// Setup image export hint. The hint can be used to control
desired
// compression, quality, color spaces, etc.
ObjSet objset = new ObjSet();
jpeg_hint = objset.CreateArray();
jpeg_hint.PushBackName("JPEG");
jpeg_hint.PushBackName("Quality");
jpeg_hint.PushBackNumber(80);
ProcessDir("../../../../TestFiles/", "../../../../TestFiles/
Output/");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
}
}
}