Resources Base
PDFs are widely used in many and various industries, and presently are the only non-image file type that DWT
supports. In this next section, we will address all the input and output operations that allow the user to properly handle PDF files.
Service mode and WASM mode.
To include the PDF addon, simply add a reference to the corresponding JavaScript file, included in the resources folder.
If you are using the dwt package, the barcode reader is already included in the main JavaScript file (
dynamsoft.webtwain.min.js
ordynamsoft.webtwain.min.mjs
) which means you can skip this step.
<script src="dynamsoft.webtwain.addon.pdf.js"></script>
When loading in a PDF file, DWT
tries to extract images from that file, which is why the SDK can handle image-based PDF documents by default. However, most existing PDF files contain much more than just images. In this case, we need to make use of the PDF Rasterizer (PDFR
for short), the main component of the PDF addon.
How PDFR works: As the name suggests,
PDFR
rasterizes a PDF file page by page much like a scanner. You set a resolution and you get the resulting images in that resolution after the rasterization.
The following code shows the basic usage
var onSuccess = function() {
console.log("Loaded a file successfully!");
};
var onFailure = function(errorCode, errorString) {
console.log(errorString);
};
DWObject.IfShowFileDialog = true;
// PDF Addon is used here to ensure PDF support
DWObject.Addon.PDF.SetResolution(200);
DWObject.Addon.PDF.SetConvertMode(Dynamsoft.EnumDWT_ConvertMode.CM_RENDERALL);
DWObject.LoadImageEx("", Dynamsoft.EnumDWT_ImageType.IT_ALL, onSuccess, onFailure);
The method SetConvertMode()
decides how PDFR
works and SetResolution()
specifies the resolution. These two methods configure PDFR
to detect and, if necessary, rasterize any PDF file that comes thereafter.
A: PDFR
is only required for text-based PDFs, to find out whether a file is text-based. Use the method IsTextBasedPDF()
.
A: Once PDFR
has been configured, it will automatically detect if a file needs to be rasterized and if so, it will convert it to an image(s) with the set resolution(if no resolution is set, the default is 200). This happens when you call any of the following methods
PDFR
also works when you drag and drop the file onto the viewer to load it
LoadImage()
LoadImageEx()
LoadImageFromBase64Binary()
LoadImageFromBinary()
FTPDownload()
FTPDownloadEx()
HTTPDownload()
HTTPDownloadEx()
HTTPDownloadThroughPost()
HTTPDownloadDirectly()
GetConvertMode()
: This method returns the current convert mode.SetPassword()
: This method sets a password which is used to open encrypted PDF file(s).DWT
can output one or multiple images in the buffer as image-based PDF file(s). This feature is built into the core module and no addon is required as was covered in the output section.
However, some advanced features are only possible with the help of the PDF addon. At present, that means configuring the resulting file(s) with the API Write.Setup()
as shown below
DWObject.Addon.PDF.Write.Setup({
author: "Dynamsoft-Support-Team",
compression: Dynamsoft.EnumDWT_PDFCompressionType.PDF_JP2000,
creator: "DWT",
creationDate: "D:20200930",
keyWords: "TWAIN, DWT, Dynamsoft",
modifiedDate: "D:20200930",
producer: "Dynamsoft Corporation",
subject: "Demoing File",
title: "Sample PDF Made by DWT",
version: 1.5,
quality: 80
});
DWObject.IfShowFileDialog = true;
DWObject.SaveAllAsPDF(' ', function() {}, function() {})
latest version