Quickly convert PDF to images with the PDF Rasterizer

Introduction

PDF is one of the most popular formats on the market. In most cases, PDF files are readable, meaning that they contain text content internally. One way to convert this to an image is to take a screenshot of the page. However, this is very inefficient. By using the PDF Rasterizer, you can get it done quickly.

Environment

node

NOTE:

The PDF Rasterizer itself doesn't rely on Node.js, and it's needed in this article just because it's faster for us to use its package manager (npm) to get required files.

Steps

Step 1 Create a new directory, open the command line tool inside (shortcut is Ctrl+Shift+right click). Download the core control used in this article through npm

npm install dwt@14.2.0

Then you can see the following in this directory

PDF-Rasterizer-1.png

Step 2 Open to the following directory

node_modules\dwt\sample

where you can see

PDF-Rasterizer-2.png

Step 3 In this article, we are going to check PDFRasterizer.html. Double click it to open. If the related controls are not yet available, follow the prompts to install them

PDF-Rasterizer-3.png

Under normal circumstances, the installed files can be found in the C:\Windows\SysWOW64\Dynamsoft\DynamsoftService directory. The core files here are mainly

DynamsoftService.exe

dwt_trial_14.1.0.0828.dll

DynamicPdf_10.3.0.0712.dll

Step 4 After the installation is complete, refresh the page, click the second button and open a local PDF file. Soon this PDF file will show up as an image(s) in the image viewer on the page

PDF-Rasterizer-4.png

At this point, you can open the browser's developer interface (F12) and try to execute the following code which allows you to view multiple images at a time.

DWObject.SetViewMode(3,3);

PDF-Rasterizer-5.png

You can also save these images to your local disk (in formats like JPEG, BMP, PNG, TIF, etc.)

DWObject.IfShowFileDialog = true;
DWObject.SaveAsJPEG('');

How it is done

Open PDFRasterizer.html in a text editor

References to the Core JavaScript files

<script type="text/javascript" src="../dist/dynamsoft.webtwain.initiate.js"></script>
<script type="text/javascript" src="../dist/dynamsoft.webtwain.config.js"></script>
<script type="text/javascript" src="../dist/addon/dynamsoft.webtwain.addon.pdf.js"></script>

Here the files referenced are

JS library for the core SDK Dynamic Web TWAIN

node_modules\dwt\dist\dynamsoft.webtwain.initiate.js node_modules\dwt\dist\dynamsoft.webtwain.config.js

JS library for the PDF Rasterizer addon

node_modules\dwt\dist\addon\dynamsoft.webtwain.addon.pdf.js

If you have previously installed the Dynamic Web TWAIN locally, the same files are located in the following directory.

C:\Program Files (x86)\Dynamsoft\Dynamic Web TWAIN SDK {version number} {Trial}\Resources

Runtime installation of the necessary files

When you open the page, the JavaScript files execute trying to set up the runtime environment. If the local Dynamsoft Service and the library files including the Dynamic Web TWAIN and PDF Rasterizer are missing, the code will show the prompt (Step 3) so that you can download and install the files.

Use the addon

if (DWObject.Addon && DWObject.Addon.PDF) {
    DWObject.Addon.PDF.SetResolution(300);
    DWObject.Addon.PDF.SetConvertMode(EnumDWT_ConvertMode.CM_RENDERALL);
}
DWObject.LoadImageEx('', 5,
    function () {
    },
    function (errorCode, errorString) {
        alert('Load Image:' + errorString);
    }
);

The core code is

DWObject.Addon.PDF.SetResolution(300); //Set the resolution for the conversion
DWObject.Addon.PDF.SetConvertMode(EnumDWT_ConvertMode.CM_RENDERALL);//Set the conversion mode, generally just set it to CM_RENDERALL

After the above settings, when you import a PDF file by calling one of the methods LoadImage, LoadImageEx, HTTPDownload, etc., PDF Rasterizer is called automatically to raster the file into an image.

results matching ""

    No results matching ""

    results matching ""

      No results matching ""