Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction - All Topics | Help Version 19.0.6.22
|
Leadtools.Forms.DocumentWriters Namespace : PdfDocumentOptions Class |
[DataContractAttribute()] [SerializableAttribute()] public class PdfDocumentOptions : DocumentOptions
'Declaration
<DataContractAttribute()> <SerializableAttribute()> Public Class PdfDocumentOptions Inherits DocumentOptions
'Usage
Dim instance As PdfDocumentOptions
public sealed class PdfDocumentOptions : DocumentOptions
@interface LTPdfDocumentOptions : LTDocumentOptions <NSCopying, NSCoding>
public class PdfDocumentOptions extends DocumentOptions
function Leadtools.Forms.DocumentWriters.PdfDocumentOptions()
[DataContractAttribute()] [SerializableAttribute()] public ref class PdfDocumentOptions : public DocumentOptions
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:
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 DocumentPage 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:
|
||||||||||||||||||||
Security, access rights and Encryption |
PDF documents can be protected (secured) using two methods:
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:
|
||||||||||||||||||||
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. |
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 |
---|---|---|
JBIG2 format offers the best 1-bit image compression. |
||
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. |
||
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. |
||
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. |
||
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). |
|
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);
}
This example will create a new Adobe Portable Document Format document (PDF) file using the various supported options.
Imports Leadtools.Forms.DocumentWriters Imports Leadtools.Forms.Ocr Imports Leadtools Imports Leadtools.Codecs Public Sub PdfDocumentOptionsExample() Dim inputFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.docx") Dim outputFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf") ' Setup a new RasterCodecs object Dim codecs As New RasterCodecs() codecs.Options.RasterizeDocument.Load.Resolution = 300 ' Get the number of pages in the input document Dim pageCount As Integer = codecs.GetTotalPages(inputFileName) ' Create a new instance of the LEADTOOLS Document Writer Dim docWriter As New DocumentWriter() ' Change the PDF options Dim pdfOptions As PdfDocumentOptions = DirectCast(docWriter.GetOptions(DocumentFormat.Pdf), 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 Console.WriteLine("Creating new PDF document: {0}", outputFileName) docWriter.BeginDocument(outputFileName, DocumentFormat.Pdf) ' Loop through all the pages For pageNumber As Integer = 1 To pageCount ' Get the page as SVG Console.WriteLine("Loading page {0}", pageNumber) Dim page As New DocumentSvgPage() page.SvgDocument = codecs.LoadSvg(inputFileName, pageNumber, Nothing) ' Add the page Console.WriteLine("Adding page {0}", pageNumber) docWriter.AddPage(page) page.SvgDocument.Dispose() Next ' Finally finish writing the PDF file on disk docWriter.EndDocument() codecs.Dispose() End Sub
using Leadtools.Forms.DocumentWriters; using Leadtools.Forms.Ocr; using Leadtools; using Leadtools.Codecs; public void PdfDocumentOptionsExample() { var inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.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 Console.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 Console.WriteLine("Loading page {0}", pageNumber); var page = new DocumentSvgPage(); page.SvgDocument = codecs.LoadSvg(inputFileName, pageNumber, null); // Add the page Console.WriteLine("Adding page {0}", pageNumber); docWriter.AddPage(page); page.SvgDocument.Dispose(); } // Finally finish writing the PDF file on disk docWriter.EndDocument(); codecs.Dispose(); }