Options to use when creating custom bookmarks based when creating Adobe Portable Document Format (PDF) documents.
[SerializableAttribute()]
[DataContractAttribute()]
public struct PdfCustomBookmark
<DataContractAttribute()>
<SerializableAttribute()>
Public Structure PdfCustomBookmark
Inherits System.ValueType
[DataContractAttribute()]
[SerializableAttribute()]
public class PdfCustomBookmark
@interface LTPdfCustomBookmark : NSObject <NSCopying, NSCoding>
public class PdfCustomBookmark
JAVASCRIPT_NOSTRUCTS
[DataContractAttribute()]
[SerializableAttribute()]
public value class PdfCustomBookmark : public System.ValueType
Use the PdfCustomBookmark structure with PdfDocumentOptions when saving a document using the DocumentFormat.Pdf format.
Custom bookmarks will only be used if creating auto-bookmarks is disabled in the PDF options. So to use custom bookmarks, set the PdfDocumentOptions.AutoBookmarksEnabled property to false.
This example will use OCR to convert an input multi-page TIF file to searachble PDF creating a custom bookmark for each page in the output PDF file.
using Leadtools.Forms.DocumentWriters;
using Leadtools.Forms.Ocr;
using Leadtools;
using Leadtools.Codecs;
public void OcrToPDFWithCustomBookmarksExample()
{
var tifFileName = Path.Combine(LEAD_VARS.ImagesDir, "Example.tif");
var pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf");
int pageCount;
// Get the input multi-page TIF file
using (var codecs = new RasterCodecs())
{
codecs.Options.RasterizeDocument.Load.Resolution = 300;
CreateMultiPageTifFile(codecs, tifFileName);
// We are going to create a bookmark for each page, so get number of pages of input file
pageCount = codecs.GetTotalPages(tifFileName);
}
// Create the OCR engine
using (var ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false))
{
// Start re-useing the RasterCodecs we have for performance
ocrEngine.Startup(null, null, null, LEAD_VARS.OcrAdvantageRuntimeDir);
// Get the DocumentWriter instance used in this OCR engine
var docWriter = ocrEngine.DocumentWriterInstance;
// Get the current PDF options, modify and then set it back
var pdfOptions = docWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions;
pdfOptions.DocumentType = PdfDocumentType.PdfA;
pdfOptions.ImageOverText = true;
// Set bookmark options
pdfOptions.AutoBookmarksEnabled = false;
for (var pageNumber = 1; pageNumber <= pageCount; pageNumber++)
{
var bookmark = new PdfCustomBookmark();
bookmark.Name = string.Format("Page {0}", pageNumber);
bookmark.PageNumber = pageNumber;
bookmark.LevelNumber = 0;
bookmark.XCoordinate = 0;
bookmark.YCoordinate = 0;
pdfOptions.CustomBookmarks.Add(bookmark);
}
docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions);
// ocr and save document with bookmark options applied
ocrEngine.AutoRecognizeManager.Run(tifFileName, pdfFileName, null, DocumentFormat.Pdf, null);
}
}
private void CreateMultiPageTifFile(RasterCodecs codecs, string tifFileName)
{
// Create a multi-page tif file from shipping Ocr1, Ocr2, Ocr3 and Ocr4.tif
var tifFileNameTemplate = Path.Combine(LEAD_VARS.ImagesDir, "Ocr{0}.tif");
if (File.Exists(tifFileName))
File.Delete(tifFileName);
RasterImage image = null;
for (var pageNumber = 1; pageNumber <= 4; pageNumber++)
{
var tempImage = codecs.Load(string.Format(tifFileNameTemplate, pageNumber));
if (image == null)
{
image = tempImage;
}
else
{
image.AddPage(tempImage);
tempImage.Dispose();
}
}
codecs.Save(image, tifFileName, RasterImageFormat.CcittGroup4, 1);
image.Dispose();
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
public const string OcrAdvantageRuntimeDir = @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime";
}
Imports Leadtools.Forms.DocumentWriters
Imports Leadtools.Forms.Ocr
Imports Leadtools
Imports Leadtools.Codecs
Public Sub OcrToPDFWithCustomBookmarksExample()
Dim tifFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Example.tif")
Dim pdfFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Example.pdf")
Dim pageCount As Integer
' Get the input multi-page TIF file
Using codecs As New RasterCodecs()
codecs.Options.RasterizeDocument.Load.Resolution = 30
CreateMultiPageTifFile(codecs, tifFileName)
' We are going to create a bookmark for each page, so get number of pages of input file
pageCount = codecs.GetTotalPages(tifFileName)
End Using
' Create the OCR engine
Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False)
' Start re-useing the RasterCodecs we have for performance
ocrEngine.Startup(Nothing, Nothing, Nothing, LEAD_VARS.OcrAdvantageRuntimeDir)
' Get the DocumentWriter instance used in this OCR engine
Dim docWriter As DocumentWriter = ocrEngine.DocumentWriterInstance
' Get the current PDF options, modify and then set it back
Dim pdfOptions As PdfDocumentOptions = DirectCast(docWriter.GetOptions(DocumentFormat.Pdf), PdfDocumentOptions)
pdfOptions.DocumentType = PdfDocumentType.PdfA
pdfOptions.ImageOverText = True
' Set bookmark options
pdfOptions.AutoBookmarksEnabled = False
For pageNumber As Integer = 1 To pageCount
Dim bookmark As New PdfCustomBookmark()
bookmark.Name = String.Format("Page {0}", pageNumber)
bookmark.PageNumber = pageNumber
bookmark.LevelNumber = 0
bookmark.XCoordinate = 0
bookmark.YCoordinate = 0
pdfOptions.CustomBookmarks.Add(bookmark)
Next
docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions)
' ocr and save document with bookmark options applied
ocrEngine.AutoRecognizeManager.Run(tifFileName, pdfFileName, Nothing, DocumentFormat.Pdf, Nothing)
End Using
End Sub
Private Sub CreateMultiPageTifFile(codecs As RasterCodecs, tifFileName As String)
' Create a multi-page tif file from shipping Ocr1, Ocr2, Ocr3 and Ocr4.tif
Dim tifFileNameTemplate As String = Path.Combine(LEAD_VARS.ImagesDir, "Ocr{0}.tif")
If File.Exists(tifFileName) Then File.Delete(tifFileName)
Dim image As RasterImage = Nothing
For pageNumber As Integer = 1 To 4
Dim tempImage As RasterImage = codecs.Load(String.Format(tifFileNameTemplate, pageNumber))
If IsNothing(image) Then
image = tempImage
Else
image.AddPage(tempImage)
tempImage.Dispose()
End If
Next
codecs.Save(image, tifFileName, RasterImageFormat.CcittGroup4, 1)
image.Dispose()
End Sub
Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
Public Const OcrAdvantageRuntimeDir As String = "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"
End Class
Leadtools.Forms.DocumentWriters Namespace
Programming with LEADTOOLS Document Writers
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET