How to Export PDF Documents to Bitmaps
- 12 minutes to read
The PDF Viewer provides an API allowing you to export either the entire loaded PDF document or its specific pages to bitmaps. Refer to the following sections to learn how to:
Export a PDF document to a TIFF file;
Export an entire PDF document to a set of PNG files;
Save specific PDF document pages as PNG files.;
Save a PDF document page as a specifically sized image.
How to Export a PDF Document to a TIFF File
The PDF Viewer provides two overloaded variants of the dxPDFDocumentExportToTIFF function for saving all pages of the loaded PDF document as multiple TIFF frames:
The first variant saves the document content into the specified smart image container;
The second variant saves the document content directly to the TIFF file.
To export the loaded PDF file with the current view parameters (that is, zoom factor and rotation angle) to a TIFF file with the same name within the same folder you need to:
Identify the source PDF file name and folder by using the PDF Viewer’s Document.Information.FileName property);
Generate the full path to the resulting file by concatenating the source folder, file name, and the “.tiff” extension;
Call the second overloaded version of the dxPDFDocumentExportToTIFF global function passing the PDF Viewer’s document object, the full path to the resulting file, the current zoom factor and rotation angle as parameters.
All of the previous actions are performed within the following code example:
var
ADocument: TdxPDFDocument; // This variable is used to access the loaded document
AZoomFactor: Double;
AFileName, AFolder: string;
begin
if not dxPDFViewer1.IsDocumentLoaded then Exit; // Do nothing if the PDF Viewer has no loaded document
ADocument := dxPDFViewer1.Document;
AZoomFactor := dxPDFViewer1.OptionsZoom.ZoomFactor/100; // Obtains the current zoom factor from a percentage value
AFolder := TPath.GetDirectoryName(ADocument.Information.FileName); // Extracts the path to the folder that stores the source PDF file
AFileName := TPath.GetFileNameWithoutExtension(ADocument.Information.FileName); // Extracts the source file name without an extension for use as the resulting file name
AFileName := AFolder + '\' + AFileName + '.tiff'; // Generates the full path to the resulting TIFF file
dxPDFDocumentExportToTIFF(ADocument, ADestinationFileName, AZoomFactor, nil, dxPDFViewer1.RotationAngle); // Saves the PDF document as a set of images in a new TIFF file
end;
Alternatively, you can use the first overloaded dxPDFDocumentExportToTIFF function variant. You also need to create a TdxSmartImage object before calling the function. Following the successful dxPDFDocumentExportToTIFF function call, save the content of the previously created smart image container to a file and then delete the object to prevent memory leaks:
uses
dxGDIPlusClasses;
//...
var
AImage: TdxSmartImage; // A smart image container used as a temporary storage for the resulting TIFF image
//...
AImage := TdxSmartImage.Create; // Creates a smart image container
//...
if(dxPDFDocumentExportToTIFF(ADocument, AZoomFactor, AImage, nil, dxPDFViewer1.RotationAngle)) then // Exports the document to a TIFF image saving it to the smart image container
AImage.SaveToFile(AFileName); // Saves the TIFF image within the smart image container to a file
AImage.Free; // Destroys the smart image container to prevent memory leaks
Both overloaded variants of the dxPDFDocumentExportToTIFF function can export only the entire loaded PDF document. Use the dxPDFDocumentExportToPNG function instead if you need to export only specific document pages.
How to Save an Entire PDF Document to a Set of PNG Files
As with exporting the PDF Viewer content to the TIFF format, there are two overloaded variants of the dxPDFDocumentExportToPNG function for saving the loaded document to the PNG image format:
The first variant saves the specified document page to a smart image container;
The second variant saves all pages of the loaded document as multiple PNG files within the specified folders;
You need to perform the following operations to export the entire loaded PDF file with the current view parameters (zoom factor and rotation angle) as a series of PNG files (instead of multiple frames within a single TIFF image). Names are generated from a single prefix followed by a corresponding page index.
Identify the source PDF file name (used as the prefix for the resulting PNG file names) and folder by using the PDF Viewer’s Document.Information.FileName property);
Generate the destination folder name by appending the result file prefix and the backslash character to the source folder;
Create the destination folder if it does not yet exist;
Call the second overloaded version of the dxPDFDocumentExportToPNG global function passing the PDF Viewer’s document object, the path to the destination folder, the resulting file prefix, the current zoom factor, and rotation angle as parameters.
All of the above operations are performed in the following code example:
var
ADocument: TdxPDFDocument; // This variable is used to access the loaded document;
AZoomFactor: Double;
AFilePrefix, AFolder: string;
begin
if not dxPDFViewer1.IsDocumentLoaded then Exit; // Do nothing if the PDF Viewer has no loaded document
ADocument := dxPDFViewer1.Document;
AZoomFactor := dxPDFViewer1.OptionsZoom.ZoomFactor/100; // Obtains the current zoom factor from a percentage value
AFilePrefix := TPath.GetFileNameWithoutExtension(ADocument.Information.FileName); // Extracts the source file name as the result file prefix
AFolder := TPath.GetDirectoryName(ADocument.Information.FileName) + AFilePrefix + '\'; // Extracts the path to the folder containing the source PDF file and generates the destination folder name
if not DirectoryExists(AFolder) then CreateDir(AFolder); // Creates the destination folder if it does not yet exist
dxPDFDocumentExportToPNG(ADocument, AFolder, AFilePrefix, AZoomFactor, nil, dxPDFViewer1.RotationAngle); // Saves all PDF document pages as a series of PNG files whose names start with the prefix
end;
For instance, if the source file is named “Demo.pdf”, the resulting PNG files within the “Demo" folder are named “Demo1.png”, “Demo2.png”, etc.
Alternatively, you can use the first overloaded dxPDFDocumentExportToPNG function variant. In this case, in addition to the operations above you need to create a smart image container prior to calling the function to destroy the object when the export process is complete to avoid memory leaks. Moreover, the first overloaded variant exports only a single specified PDF document page per call. You need to cycle through all of its pages, specifying a new page index every time to export the entire loaded document:
uses
dxGDIPlusClasses;
//...
var
AImage: TdxSmartImage; // A smart image container is used to temporarily store a single exported PDF document page
I: Integer; // A cycle counter
//...
AImage := TdxSmartImage.Create; // Create a smart image container
//...
for I := 0 to ADocument.PageCount - 1 do // Cycles through all pages of the loaded PDF document
begin
if(dxPDFDocumentExportToPNG(ADocument, I, AZoomFactor, AImage, dxPDFViewer1.RotationAngle)) then // Exports a single document page to the smart image container as a PNG image
AImage.SaveToFile(ADestinationFolder + AResultFilePrefix + IntToStr(I) + '.png'); // Saves the bitmap within the current smart image container as a PNG image
end;
AImage.Free; // Destroys the smart image container to prevent memory leaks
end;
How to Save Specific PDF Document Pages as PNG Files
The PDF Viewer provides the first overloaded variant of the dxPDFDocumentExportToPNG global function for exporting selected document pages as PNG images. If you need to save a single PDF document page as a PNG file, refer to the following code example:
uses
dxGDIPlusClasses;
//...
var
ADocument: TdxPDFDocument; // This variable is used to access the loaded document;
AImage: TdxSmartImage; // A smart image container is used to temporarily store a single exported PDF document page
AZoomFactor: Double;
AFilePrefix, AFolder: string;
begin
if not dxPDFViewer1.IsDocumentLoaded then Exit; // Do nothing if the PDF Viewer has no loaded document
AImage := TdxSmartImage.Create; // Creates a smart image container
ADocument := dxPDFViewer1.Document;
AZoomFactor := dxPDFViewer1.OptionsZoom.ZoomFactor/100; // Obtains the current zoom factor from a percentage value
try
if(dxPDFDocumentExportToPNG(ADocument, 0, AZoomFactor, AImage, dxPDFViewer1.RotationAngle)) then // Exports the first page of the loaded document
AImage.SaveToFile('Demo.png'); // Saves the exported image to a PNG file
finally
AImage.Free; // Destroys the smart image container to prevent memory leaks
end;
end;
How to Save a PDF Document Page as a Specifically Sized Image
You can call the dxPDFDocumentExportToImage global function to save one or more document pages in the desired bitmap format. This function, unlike dxPDFDocumentExportToPNG and dxPDFDocumentExportToTIFF, zooms the exported page according to the specified image width instead of accepting a zoom factor value. The height of the resulting bitmap, saved to the passed smart image container, is calculated from the specified width and the exported page’s height-to-width ratio. Following a successful export operation, you can convert the resulting bitmap to the desired format and then save it to a file or stream.
The following code example saves the first document page as a 600 pixel wide JPEG image:
uses
dxGDIPlusClasses;
//...
var
AImage: TdxSmartImage; // A smart image container is used to temporarily store a single exported PDF document page
ADocument: TdxPDFDocument; // This variable is used to access the loaded document
//...
begin
if not dxPDFViewer1.IsDocumentLoaded then Exit; // Do nothing if the PDF Viewer has no loaded document
AImage := TdxSmartImage.Create; // Creates a smart image container
ADocument := dxPDFViewer1.Document;
if(dxPDFDocumentExportToImage(ADocument, 0, 600, AImage, dxPDFViewer1.RotationAngle)) then // Exports the first document page as a 600 pixel wide bitmap
begin
AImage.ImageDataFormat := dxImageJpeg; // Converts the stored bitmap to the JPEG format
AImage.SaveToFile('Demo.jpg'); // Saves the stored JPEG image to a file
end;
AImage.Free; // Destroys the smart image container to prevent memory leaks
end;
The resulting JPEG image, depicting the exported page, is 600 by 774 pixels.