←Select platform

PdfDocumentOptions Class

Summary
Provides extra options to use when saving a document using the Adobe Portable Document Format (PDF).
Syntax
C#
Objective-C
C++/CLI
Java
Python
[SerializableAttribute()] 
[DataContractAttribute()] 
public class PdfDocumentOptions : DocumentOptions 
@interface LTPdfDocumentOptions : LTDocumentOptions<NSCopying, NSCoding> 
public class PdfDocumentOptions extends DocumentOptions 
[DataContractAttribute()] 
[SerializableAttribute()] 
public ref class PdfDocumentOptions : public DocumentOptions  
class PdfDocumentOptions(DocumentOptions): 
Remarks

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

Before using the LEADTOOLS Document Writer to create PDF documents, you must unlock the RasterSupportType.DocumentWritersPdf key unless the document writers are being used from the LEADTOOLS OCR engine. For more information, refer to Unlocking Special LEAD Features.

To change the options used with the PDF format, perform the following steps:

  1. Use the DocumentWriter.GetOptions method of the DocumentWriter object being used. Pass DocumentFormat.Pdf to the format parameter. Note that the resulting object from the base DocumentOptions class needs to be cast to PdfDocumentOptions.
  2. Use the various PdfDocumentOptions properties to change the options.
  3. Use DocumentWriter.SetOptions to set the new options in the engine.
  4. Now you can call the DocumentWriter.BeginDocument method (again, with DocumentFormat.Pdf for the format parameter) to create a new document and add the pages.

The PdfDocumentOptions class contains the following features:

Feature Description
Set the PDF document type to PDF or PDF/A. Use the PdfDocumentOptions.DocumentType property to set the document type to either PDF or PDF/A.
Control the font embedding mode Use the PdfDocumentOptions.FontEmbedMode property to control how the 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 the file.
Add an image as an overlay on top of the PDF content. Use the PdfDocumentOptions.ImageOverText property to add the original raster image as an overlay on top of the PDF content. The resulting document will look exactly like the original document. If this option is used, the overlay image must be set in the Image property of the DocumentWriterPage object passed to the DocumentWriter.AddPage method. Use the ImageOverTextSize and ImageOverTextMode properties to control the quality of this image.
Create linearized PDF documents optimized for fast web viewing. A linearized PDF file is a file that has been organized in a special way 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. To enable the creation of linearized PDF documents, use the PdfDocumentOptions.Linearized property. PDF linearization is supported in both PDF and PDF/A formats.
Set the 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. Use the PdfDocumentOptions.Title, PdfDocumentOptions.Subject, PdfDocumentOptions.Author and PdfDocumentOptions.Keywords to set the document metadata.
Set the PDF initial view properties

The user can control the intial view properties of the generated PDF file as follows:

Property Description
PageModeType Page mode such 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 of the page to scroll and/or pan to.
ZoomPercent Zoom percentage to use when the document is opened.
HideToolbar, HideMenubar and HideWindowUI Show or hide various user interface elements.
FitWindow and CenterWindow 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.Protected property to true and the PdfDocumentOptions.UserPassword property to the password value.

  • Protected against editing. PDF editor 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.Protected 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 Right
Low (PdfDocumentOptions.EncryptionMode set to PdfDocumentEncryptionMode.RC40Bit) Printing (PdfDocumentOptions.PrintEnabled), Copying text (PdfDocumentOptions.CopyEnabled), Editing (PdfDocumentOptions.EditEnabled) and 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) and adding/removing pages PdfDocumentOptions.AssemblyEnabled.
Add bookmarks in the final PDF document. Use PdfAutoBookmark or PdfCustomBookmark to create either auto or custom (user) bookmarks in the final PDF documents.
Create an annotated PDF document by adding an annotation objects. Annotated PDF can be created by passing valid LEADTOOLS annotation container for each page that needs to be annotated inside the PDF document. Set the AnnotationContainer property to a valid LEADTOOLS annotation container before calling the DocumentWriter.AddPagemethod.
Property Description
PageModeType Page mode such 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 of the page to use 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 How the document is viewed in the window.
DisplayDocTitle Display the document title in the viewer.
Encryption Owner Access Right
Low (PdfDocumentOptions.EncryptionMode set to PdfDocumentEncryptionMode.RC40Bit) Printing (PdfDocumentOptions.PrintEnabled), Copying text (PdfDocumentOptions.CopyEnabled), Editing (PdfDocumentOptions.EditEnabled) and 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) and adding/removing pages PdfDocumentOptions.AssemblyEnabled.

Saving PDF files with minimum file size

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

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). Revert to flate compression if the image has 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 result 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:

void SetPDFOptions(DocumentWriter documentWriter, bool useImageOverText) 
{ 
   // Get the current PDF options 
   PdfDocumentOptions pdfOptions = documentWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions; 
             
   // Use JBIG2 for B/W images 
   pdfOptions.OneBitImageCompression = OneBitImageCompressionType.Jbig2; 
   // Use JPEG2000 or Flate for colored images 
   pdfOptions.ColoredImageCompression = ColoredImageCompressionType.FlateJpx; 
   // Embed fonts automatically 
   pdfOptions.FontEmbedMode = DocumentFontEmbedMode.Auto; 
             
   if (useImageOverText) 
   { 
      // Use image over text 
      pdfOptions.ImageOverText = true; 
      // Re-size the overlay image by 2 
      pdfOptions.ImageOverTextSize = DocumentImageOverTextSize.Half; 
      // Convert grayscale to black and white if possible 
      pdfOptions.ImageOverTextMode = 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; 
   } 
             
   // Set our options 
   documentWriter.SetOptions(DocumentFormat.Pdf, pdfOptions); 
} 
Example

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

C#
Java
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 
using Leadtools; 
using Leadtools.Codecs; 
 
 
public void PdfDocumentOptionsExample() 
{ 
   var inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.docx"); 
   var outputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf"); 
 
   // Setup a new RasterCodecs object 
   var codecs = new RasterCodecs(); 
   codecs.Options.RasterizeDocument.Load.Resolution = 300; 
 
   // Get the number of pages in the input document 
   var pageCount = codecs.GetTotalPages(inputFileName); 
 
   // Create a new instance of the LEADTOOLS Document Writer 
   var docWriter = new DocumentWriter(); 
 
   // Change the PDF options 
   var pdfOptions = docWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions; 
   pdfOptions.DocumentType = PdfDocumentType.Pdf; 
   pdfOptions.FontEmbedMode = DocumentFontEmbedMode.None; 
   pdfOptions.ImageOverText = false; 
   pdfOptions.Linearized = false; 
   pdfOptions.Title = "Add your title here"; 
   pdfOptions.Subject = "Add your subject here"; 
   pdfOptions.Keywords = "Add your keywords here"; 
   pdfOptions.Author = "Add author name here"; 
   pdfOptions.Protected = true; 
   pdfOptions.UserPassword = "User password"; 
   pdfOptions.OwnerPassword = "Owner password"; 
   pdfOptions.EncryptionMode = PdfDocumentEncryptionMode.RC128Bit; 
   pdfOptions.PrintEnabled = false; 
   pdfOptions.HighQualityPrintEnabled = true; 
   pdfOptions.CopyEnabled = false; 
   pdfOptions.EditEnabled = true; 
   pdfOptions.AnnotationsEnabled = true; 
   pdfOptions.AssemblyEnabled = false; 
   pdfOptions.OneBitImageCompression = OneBitImageCompressionType.Flate; 
   pdfOptions.ColoredImageCompression = ColoredImageCompressionType.FlateJpeg; 
   pdfOptions.QualityFactor = 2; 
 
   // Use default resolution 
   pdfOptions.DocumentResolution = 0; 
   pdfOptions.PageRestriction = DocumentPageRestriction.Relaxed; 
 
   // Setup empty page size (Letter size) 
   pdfOptions.EmptyPageWidth = 8.5; 
   pdfOptions.EmptyPageHeight = 11; 
   pdfOptions.EmptyPageResolution = 300; 
 
   docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions); 
 
   // Create a new PDF document 
   Debug.WriteLine("Creating new PDF document: {0}", outputFileName); 
   docWriter.BeginDocument(outputFileName, DocumentFormat.Pdf); 
 
   // Loop through all the pages 
   for (var pageNumber = 1; pageNumber <= pageCount; pageNumber++) 
   { 
      // Get the page as SVG 
      Debug.WriteLine("Loading page {0}", pageNumber); 
      var page = new DocumentWriterSvgPage(); 
      page.SvgDocument = codecs.LoadSvg(inputFileName, pageNumber, null); 
 
      // Add the page 
      Debug.WriteLine("Adding page {0}", pageNumber); 
      docWriter.AddPage(page); 
 
      page.SvgDocument.Dispose(); 
   } 
 
   // Finally finish writing the PDF file on disk 
   docWriter.EndDocument(); 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.IOException; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
 
 
public void pdfDocumentOptionsExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
 
   String inputFileName = combine(LEAD_VARS_IMAGES_DIR, "LEADTOOLSEditor.docx"); 
   String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "Example.pdf"); 
 
   // Setup a new RasterCodecs object 
   RasterCodecs codecs = new RasterCodecs(); 
   codecs.getOptions().getRasterizeDocument().getLoad().setResolution(300); 
 
   // Get the number of pages in the input document 
   int pageCount = codecs.getTotalPages(inputFileName); 
 
   // Create a new instance of the LEADTOOLS Document Writer 
   DocumentWriter docWriter = new DocumentWriter(); 
 
   // Change the PDF options 
   PdfDocumentOptions pdfOptions = (PdfDocumentOptions) docWriter.getOptions(DocumentFormat.PDF); 
   pdfOptions.setDocumentType(PdfDocumentType.PDF); 
   pdfOptions.setFontEmbedMode(DocumentFontEmbedMode.NONE); 
   pdfOptions.setImageOverText(false); 
   pdfOptions.setLinearized(false); 
   pdfOptions.setTitle("Add your title here"); 
   pdfOptions.setSubject("Add your subject here"); 
   pdfOptions.setKeywords("Add your keywords here"); 
   pdfOptions.setAuthor("Add author name here"); 
   pdfOptions.setProtected(true); 
   pdfOptions.setUserPassword("User password"); 
   pdfOptions.setOwnerPassword("Owner password"); 
   pdfOptions.setEncryptionMode(PdfDocumentEncryptionMode.RC128BIT); 
   pdfOptions.setPrintEnabled(false); 
   pdfOptions.setHighQualityPrintEnabled(true); 
   pdfOptions.setCopyEnabled(false); 
   pdfOptions.setEditEnabled(true); 
   pdfOptions.setAnnotationsEnabled(true); 
   pdfOptions.setAssemblyEnabled(false); 
   pdfOptions.setOneBitImageCompression(OneBitImageCompressionType.FLATE); 
   pdfOptions.setColoredImageCompression(ColoredImageCompressionType.FLATE_JPEG); 
   pdfOptions.setQualityFactor(2); 
 
   // Use default resolution 
   pdfOptions.setDocumentResolution(0); 
   pdfOptions.setPageRestriction(DocumentPageRestriction.RELAXED); 
 
   // Setup empty page size (Letter size) 
   pdfOptions.setEmptyPageWidth(8.5); 
   pdfOptions.setEmptyPageHeight(11); 
   pdfOptions.setEmptyPageResolution(300); 
 
   docWriter.setOptions(DocumentFormat.PDF, pdfOptions); 
 
   // Create a new PDF document 
   System.out.printf("Creating new PDF document: %s%n", outputFileName); 
   docWriter.beginDocument(outputFileName, DocumentFormat.PDF); 
 
   // Loop through all the pages 
   for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) { 
 
      // Get the page as SVG 
      System.out.printf("Loading page %s%n", pageNumber); 
      DocumentWriterSvgPage page = new DocumentWriterSvgPage(); 
      page.setSvgDocument(codecs.loadSvg(inputFileName, pageNumber, null)); 
 
      // Add the page 
      System.out.printf("Adding page %s%n", pageNumber); 
      docWriter.addPage(page); 
 
      page.getSvgDocument().dispose(); 
   } 
 
   // Finally finish writing the PDF file on disk 
   docWriter.endDocument(); 
   codecs.dispose(); 
   System.out.println("no exceptions or errors. Program run successfully"); 
} 
Requirements

Target Platforms

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

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