PdfDocumentOptions Object

Summary

Provides extra options to use when saving a document using the Adobe Portable Document Format (PDF).

Syntax
TypeScript
JavaScript
function lt.Document.Writer.PdfDocumentOptions 
   extends DocumentOptions 
class lt.Document.Writer.PdfDocumentOptions 
   extends DocumentOptions 
Remarks

The options set in the PdfDocumentOptions class will be used when the user saves a document using the DocumentFormat.Pdf format.

The PdfDocumentOptions class contains the following member features:

Member Description
PdfDocumentOptions.DocumentType Sets the document type to either PDF or PDF/A.
PdfDocumentOptions.FontEmbedMode Controls how fonts are embedded in the resulting PDF document.
Note that PdfDocumentOptions.FontEmbedMode is not used when
saving PDF/A files. All fonts are always embedded in PDF/A files.
PdfDocumentOptions.ImageOverText Adds the original raster image as an overlay on top of the PDF
content. The resulting document will look exactly like the original
document. Use the ImageOverTextSize and ImageOverTextMode
properties to control the quality of this image.
PdfDocumentOptions.Linearized A linearized PDF file is a file that has been organized in such
a way as to enable efficient incremental access in a network
environment. This allows the first page of the PDF file to be
displayed in a user Web browser before the entire file is
downloaded from the Web server.
PdfDocumentOptions.Linearized enables the creation of linearized
PDF documents. Both PDF and PDF/A formats are supported.
PDF Document Metadata The resulting PDF can contain optional metadata associated with
the document. This metadata can be used by external search and
indexing engines to search and classify PDF documents.
Set the document metadata elements with PdfDocumentOptions.Title,
PdfDocumentOptions.Subject, PdfDocumentOptions.Author, and
PdfDocumentOptions.Keywords.

PDF Initial View Properties

Control the initial view properties of the generated PDF file with the following properties.

Property Description
PageModeType Page mode, such as page only, page with bookmarks, page with thumbnails, etc.
PageLayoutType Page layout, such single page, one column, two pages, etc.
PageFitType Page fit, such as none, fit width, height, etc.
InitialPageNumber Initial page number to go to.
XCoordinate and YCoordinate Position on the page to go to initially.
ZoomPercent Zoom percentage to use when the document is opened.
HideToolbar, HideMenubar, and HideWindowUI Show or hide various user interface elements.
FitWindow and CenterWindow Specify how the document is viewed in the window.
DisplayDocTitle Display the document title in the viewer.

Security, Access Rights, and Encryption

PDF documents can be protected (secured) using two methods:

  • Protected against viewing. PDF viewers will request a password from the user when the document is opened for viewing. This password is called the user password. The created PDF document can be protected by setting the PdfDocumentOptions.IsProtected property to true and the PdfDocumentOptions.UserPassword property to the password value.

  • Protected against editing. PDF editors will request a password from the user when the document is opened for editing. This password is called the owner password. The created PDF document can be protected by setting the PdfDocumentOptions.IsProtected property to true and the PdfDocumentOptions.OwnerPassword property to the password value.

When a PDF document is protected against editing (through the use of an owner password), an encryption level and owner access rights can be granted or denied in the resulting document. The following table lists the PDF access rights supported by the LEADTOOLS Document Writers:

Encryption Owner Access Rights
Low (PdfDocumentOptions.EncryptionMode set to
PdfDocumentEncryptionMode.RC40Bit)
Printing (PdfDocumentOptions.PrintEnabled)
Copying text (PdfDocumentOptions.CopyEnabled)
Editing (PdfDocumentOptions.EditEnabled)
Annotations and comments (PdfDocumentOptions.AnnotationsEnabled)
High (PdfDocumentOptions.EncryptionMode set to
PdfDocumentEncryptionMode.RC128Bit)
Adds the following to owner access rights supported by low level encryption:
High quality printing (PdfDocumentOptions.HighQualityPrintEnabled)
Adding/Removing pages (PdfDocumentOptions.AssemblyEnabled)

Minimizing PDF File Size When Saving

The following table shows the suggested PDF document options to use to achieve the smallest output PDF file size while maintaining an acceptable quality.

Property Value Remarks
OneBitImageCompression OneBitImageCompressionType.Jbig2 The JBIG2 format offers the best 1-bit image compression.
ColoredImageCompression ColoredImageCompressionType.FlateJpx JPX offers the best colored image compression (8 or 24 bits per pixel).
Reverts to flate compression if the image has a bits per pixel value
other than 8 or 24.
ImageOverTextSize DocumentImageOverTextSize.Half Only used when using the image over text option (ImageOverText is
true). The overlay image is sized by half and stretched over the
page. This will save up to 75% of the size while maintaining the
image quality.
ImageOverTextMode DocumentImageOverTextMode.Relaxed Only used when using the image over text option (ImageOverText is
true). Any grayscale overlay image is converted to black and white
while ignoring noise and shadows.
QualityFactor 50 or 100 Use 50 when using the image over text option (ImageOverText is
true). Use 100 when image over text is not used (ImageOverText
is false).
PdfDocumentOptions.FontEmbedMode DocumentFontEmbedMode.None Do not embed the fonts in the PDF file. Warning: The resulting PDF is
not guaranteed to have the same fonts used to create it. Use this option
only if font substituting is acceptable in your particular scenario.
Otherwise, leave this to the default value of DocumentFontEmbedMode.Auto.

The following code snippet shows how to set the PDF options to produce minimum file size with acceptable quality:

function createPDFOptions(useImageOverText) { 
// Create options 
var pdfOptions = new lt.Documents.Writers.PdfDocumentOptions(); 
 
// Use JBIG2 for B/W images 
pdfOptions.oneBitImageCompression = lt.Documents.Writers.OneBitImageCompressionType.jbig2;; 
 
// Use JPEG2000 or Flate for colored images 
pdfOptions.coloredImageCompression = lt.Documents.Writers.ColoredImageCompressionType.flateJpx; 
 
// Embed fonts automatically 
pdfOptions.fontEmbedMode = lt.Documents.Writers.DocumentFontEmbedMode.auto; 
 
if (useImageOverText) { 
// Use image over text 
pdfOptions.imageOverText = true; 
 
// Re-size the overlay image by 2 
pdfOptions.imageOverTextSize = lt.Documents.Writers.DocumentImageOverTextSize.half; 
 
// Convert grayscale to black and white if possible 
pdfOptions.imageOverTextMode = lt.Documents.Writers.DocumentImageOverTextMode.relaxed; 
 
// Use quality factor of 50 
pdfOptions.qualityFactor = 50; 
} else { 
// Will not use image over text 
pdfOptions.imageOverText = false; 
 
// Use quality factor of 100 
pdfOptions.qualityFactor = 100; 
} 
 
// Use this 
return pdfOptions; 
} 
Example

This example will create a new encrypted Adobe Portable Document Format document (PDF) file using the various supported options.

Start with the example from LEADDocument and replace all the code inside the example function (search for the "// TODO: add example code here" comment) with the following code:

PDFDocumentOptions.ts
DocumentHelper.ts
PDFDocumentOptions.js
DocumentHelper.js
PDFDocumentOptions.html
import { DocumentHelper } from "../../utilities/DocumentHelper"; 
 
export class PdfDocumentOptionsExample { 
   private el: HTMLElement; 
 
   public constructor() { 
      lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/js/LEADTOOLSEVAL.txt", "EVAL", null); 
      DocumentHelper.initFactory(); 
   } 
 
   public run = (buttonID: string) => { 
      this.el = document.getElementById(buttonID); 
      this.el.onclick = this.loadDocument; 
   } 
 
   loadDocument = () => { 
      const url = "https://demo.leadtools.com/images/pdf/leadtools.pdf"; 
      DocumentHelper.log("Loading document..."); 
 
      lt.Document.DocumentFactory.loadFromUri(url, null) 
         .done((doc: lt.Document.LEADDocument) => { 
            DocumentHelper.log("Loaded, converting..."); 
            // Create a new PDF document with: PDF and no image/text 
            const pdfOptions = new lt.Document.Writer.PdfDocumentOptions(); 
            pdfOptions.documentType = lt.Document.Writer.PdfDocumentType.pdf; 
            pdfOptions.fontEmbedMode = lt.Document.Writer.DocumentFontEmbedMode.none; 
            pdfOptions.imageOverText = false; 
            pdfOptions.linearized = false; 
            pdfOptions.title = "TEST TITLE"; 
            pdfOptions.subject = "TEST SUBJECT"; 
            pdfOptions.keywords = "TEST KEYWORDS"; 
            pdfOptions.author = "TEST AUTHOR"; 
            pdfOptions.isProtected = true; 
            pdfOptions.userPassword = "password"; 
            pdfOptions.ownerPassword = "Owner password"; 
            pdfOptions.encryptionMode = lt.Document.Writer.PdfDocumentEncryptionMode.rc128Bit; 
            pdfOptions.printEnabled = false; 
            pdfOptions.highQualityPrintEnabled = true; 
            pdfOptions.copyEnabled = false; 
            pdfOptions.editEnabled = true; 
            pdfOptions.annotationsEnabled = true; 
            pdfOptions.assemblyEnabled = false; 
            pdfOptions.oneBitImageCompression = lt.Document.Writer.OneBitImageCompressionType.flate; 
            pdfOptions.coloredImageCompression = lt.Document.Writer.ColoredImageCompressionType.flateJpeg; 
            pdfOptions.qualityFactor = 2; 
            const jobData = new lt.Document.DocumentConverterJobData(); 
            jobData.documentFormat = lt.Document.Writer.DocumentFormat.pdf; 
            jobData.rasterImageFormat = lt.Document.RasterImageFormat.unknown; 
            // Set document options 
            jobData.documentOptions = pdfOptions; 
            doc.convert(jobData) 
               .done((result: any) => { 
                  // This is generic code, we know the result is in "document" since PDF supports that 
                  // But this code checks if the results have been archived into a ZIP file if this 
                  // example was converting to, say SVG 
                  const resultDocument = result.document != null ? result.document.url : result.archive.url; 
                  const link = document.createElement("a"); 
                  const url = lt.Document.DocumentFactory.serviceUri + "/" + resultDocument; 
                  link.href = url; 
                  link.innerHTML = url; 
                  link.target = "_default"; 
                  DocumentHelper.log("Converted: " + url, url); 
                  this.el.parentElement.insertBefore(link, this.el.parentElement.firstChild); 
               }) 
               .fail(DocumentHelper.showServiceError); 
         }) 
         .fail(DocumentHelper.showServiceError); 
   } 
} 
export class DocumentHelper { 
   static showServiceError = (jqXHR, statusText, errorThrown) => { 
      alert("Error returned from service. See the console for details."); 
      const serviceError = lt.Document.ServiceError.parseError(jqXHR, statusText, errorThrown); 
      console.error(serviceError); 
   } 
 
   static log = (message: string, data?: any) => { 
      const outputElement = document.getElementById("output"); 
      if (outputElement) { 
         const time = (new Date()).toLocaleTimeString(); 
         const textElement = document.createElement("p"); 
         textElement.innerHTML = (outputElement.childElementCount + 1) + " [" + time + "]: " + message; 
         outputElement.insertBefore(textElement, outputElement.firstChild); 
      } 
 
      if (!data) 
         console.log(message); 
      else 
         console.log(message, data); 
   } 
 
   static initFactory = () => { 
      // To communicate with the DocumentsService, it must be running! 
      // Change these parameters to match the path to the service. 
      lt.Document.DocumentFactory.serviceHost = "http://localhost:40000"; 
      lt.Document.DocumentFactory.servicePath = ""; 
      lt.Document.DocumentFactory.serviceApiPath = "api"; 
   } 
} 
import { DocumentHelper } from "../../utilities/DocumentHelper"; 
 
export class PdfDocumentOptionsExample { 
   el; 
 
   constructor() { 
      lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/js/LEADTOOLSEVAL.txt", "EVAL", null); 
      DocumentHelper.initFactory(); 
   } 
 
   run = (buttonID) => { 
      this.el = document.getElementById(buttonID); 
      this.el.onclick = this.loadDocument; 
   } 
 
   loadDocument = () => { 
      const url = "https://demo.leadtools.com/images/pdf/leadtools.pdf"; 
      DocumentHelper.log("Loading document..."); 
 
      lt.Document.DocumentFactory.loadFromUri(url, null) 
         .done((doc) => { 
            DocumentHelper.log("Loaded, converting..."); 
            // Create a new PDF document with: PDF and no image/text 
            const pdfOptions = new lt.Document.Writer.PdfDocumentOptions(); 
            pdfOptions.documentType = lt.Document.Writer.PdfDocumentType.pdf; 
            pdfOptions.fontEmbedMode = lt.Document.Writer.DocumentFontEmbedMode.none; 
            pdfOptions.imageOverText = false; 
            pdfOptions.linearized = false; 
            pdfOptions.title = "TEST TITLE"; 
            pdfOptions.subject = "TEST SUBJECT"; 
            pdfOptions.keywords = "TEST KEYWORDS"; 
            pdfOptions.author = "TEST AUTHOR"; 
            pdfOptions.isProtected = true; 
            pdfOptions.userPassword = "password"; 
            pdfOptions.ownerPassword = "Owner password"; 
            pdfOptions.encryptionMode = lt.Document.Writer.PdfDocumentEncryptionMode.rc128Bit; 
            pdfOptions.printEnabled = false; 
            pdfOptions.highQualityPrintEnabled = true; 
            pdfOptions.copyEnabled = false; 
            pdfOptions.editEnabled = true; 
            pdfOptions.annotationsEnabled = true; 
            pdfOptions.assemblyEnabled = false; 
            pdfOptions.oneBitImageCompression = lt.Document.Writer.OneBitImageCompressionType.flate; 
            pdfOptions.coloredImageCompression = lt.Document.Writer.ColoredImageCompressionType.flateJpeg; 
            pdfOptions.qualityFactor = 2; 
            const jobData = new lt.Document.DocumentConverterJobData(); 
            jobData.documentFormat = lt.Document.Writer.DocumentFormat.pdf; 
            jobData.rasterImageFormat = lt.Document.RasterImageFormat.unknown; 
            // Set document options 
            jobData.documentOptions = pdfOptions; 
            doc.convert(jobData) 
               .done((result) => { 
                  // This is generic code, we know the result is in "document" since PDF supports that 
                  // But this code checks if the results have been archived into a ZIP file if this 
                  // example was converting to, say SVG 
                  const resultDocument = result.document != null ? result.document.url : result.archive.url; 
                  const link = document.createElement("a"); 
                  const url = lt.Document.DocumentFactory.serviceUri + "/" + resultDocument; 
                  link.href = url; 
                  link.innerHTML = url; 
                  link.target = "_default"; 
                  DocumentHelper.log("Converted: " + url, url); 
                  this.el.parentElement.insertBefore(link, this.el.parentElement.firstChild); 
               }) 
               .fail(DocumentHelper.showServiceError); 
         }) 
         .fail(DocumentHelper.showServiceError); 
   } 
} 
export class DocumentHelper { 
   static showServiceError = (jqXHR, statusText, errorThrown) => { 
      alert("Error returned from service. See the console for details."); 
      const serviceError = lt.Document.ServiceError.parseError(jqXHR, statusText, errorThrown); 
      console.error(serviceError); 
   } 
 
   static log = (message, data) => { 
      const outputElement = document.getElementById("output"); 
      if (outputElement) { 
         const time = (new Date()).toLocaleTimeString(); 
         const textElement = document.createElement("p"); 
         textElement.innerHTML = (outputElement.childElementCount + 1) + " [" + time + "]: " + message; 
         outputElement.insertBefore(textElement, outputElement.firstChild); 
      } 
 
      if (!data) 
         console.log(message); 
      else 
         console.log(message, data); 
   } 
 
   static initFactory = () => { 
      // To communicate with the DocumentsService, it must be running! 
      // Change these parameters to match the path to the service. 
      lt.Document.DocumentFactory.serviceHost = "http://localhost:40000"; 
      lt.Document.DocumentFactory.servicePath = ""; 
      lt.Document.DocumentFactory.serviceApiPath = "api"; 
   } 
} 
<!doctype html> 
<html lang="en"> 
<title>Document Example | PDFDocumentOptions</title> 
 
<head> 
   <script src="https://code.jquery.com/jquery-2.2.4.min.js" 
      integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> 
 
   <script src="../../LT/Leadtools.js"></script> 
   <script src="../../LT/Leadtools.Controls.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Engine.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Designers.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Rendering.Javascript.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Automation.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Main.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Color.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Core.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Effects.js"></script> 
   <script src="../../LT/Leadtools.Document.js"></script> 
   <script src="../../LT/Leadtools.Document.Viewer.js"></script> 
   <link rel="stylesheet" type="text/css" href="../../css/examples.css"> 
 
   <!-- All demo files are bundled and appended to the window --> 
   <script src="../../bundle.js" type="text/javascript"></script> 
</head> 
 
<body> 
   <div> 
      <button type="button" id="exampleButton">Run Example</button> 
   </div> 
   <div id="output"></div> 
   <div> 
      <img id="img" /> 
   </div> 
</body> 
 
<script> 
   window.onload = () => { 
      const example = new window.examples.PDF.PDFDocumentOptions(); 
      example.run("exampleButton"); 
   }; 
</script> 
</html> 
Requirements

Target Platforms

Help Version 22.0.2023.2.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.