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. But these readable PDF files are not supported by Dynamic Web TWAIN. To load them, you must first convert the file to images, which is exactly what the PDF Rasterizer does.

Environment

node

NOTE:

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

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@15.0.2

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 SDK is not yet available, follow the prompt to install it

PDF-Rasterizer-3.png

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

DynamsoftService.exe

dwt_trial_15.0.0.0625.dll

DynamicPdfR_10.3.1.0124.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('', 0);

How it works

Open PDFRasterizer.html in a text editor and check the source code of the page.

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 can also be found 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 code referenced above will try 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 in 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 will be invoked automatically to raster the file into an image(s).

results matching ""

    No results matching ""

    results matching ""

      No results matching ""