Using an OcrTransformer to add a Searchable Text Layer

The code below shows you how to use an OcrTransformer to add a searchable text layer to a PDF.

This sample requires a Professional+OCR license.

C#
C++
VB.Net
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.IO;
using SolidFramework.Converters.Plumbing;
using SolidFramework.Plumbing;
using SolidFramework.Pdf;
using SolidFramework.Pdf.Transformers;
 
namespace PDFTransformers
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Call your Solid Documents License
            SolidFramework.License.Import(@"C:\MyFolder\license.xml");
 
            // Set the location of your the file you want to convert
            String pdfPath = @"C:\YourFolder\yourpdf.pdf";
 
            String searchablePdfPath = Path.ChangeExtension(pdfPath, "searchable.pdf");
 
            //*PDF Transformer*//
 
            // Create a new PdfDocument Object called document and assign the selected PDF file to it
            using (PdfDocument document = new PdfDocument(pdfPath))
            {               
                // Create a new OCRTransformer Object
                OcrTransformer transformer = new OcrTransformer();
 
                // Set the OcrType to Create a Searchable TextLayer
                transformer.OcrType = OcrType.CreateSearchableTextLayer;
 
                // Set the OCR Language to the Language in your PDF File - "en" for English, "es" for Spanish etc.
                transformer.OcrLanguage = "en";
 
                // Preserve the Original PDF Files Image Compression
                transformer.OcrImageCompression = SolidFramework.Imaging.Plumbing.ImageCompression.PreserveOriginal;
 
                // Add the user selected PDF file to your transformer
                transformer.AddDocument(document);
 
                // Transform the PDF File
                transformer.Transform();
 
                // Save the new Searchable PDF file to the extension you specified earlier
                document.SaveAs(searchablePdfPath, OverwriteMode.ForceOverwrite);
            }
        }
    }
}
C++

This sample is not currently available in C++.

VB.Net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Imports System.IO
Imports SolidFramework.Converters.Plumbing
Imports SolidFramework.Plumbing
Imports SolidFramework
Imports SolidFramework.Pdf
 
Module PDFTransformers
 
    Sub Main()
 
        ' Call your Solid Documents License
        SolidFramework.License.Import("C:\MyFolder\license.xml")
 
        'Define a variable for your source file
        Dim sPdfPath As String
 
        ' Define a String for the output file
        Dim searchPath As String
 
        ' Define a PDF Document object
        Dim myDocument As SolidFramework.Pdf.PdfDocument
 
        ' Define your Solid Framework Converter
        Dim myConverter As SolidFramework.Pdf.Transformers.OcrTransformer
 
        ' Set your file path
        sPdfPath = "C:\YourFolder\filepdf.pdf"
 
        myDocument = New PdfDocument(sPdfPath)
 
        ' Set your output file and location
        searchPath = Path.ChangeExtension(sPdfPath, ".searchable.pdf")
 
        ' Set the converter
        myConverter = New SolidFramework.Pdf.Transformers.OcrTransformer
 
        ' Set the OcrType to Create a Searchable TextLayer
        myConverter.OcrType = OcrType.CreateSearchableTextLayer
 
        ' Set the OCR Language to the Language in your PDF File - "en" for English, "es" for Spanish etc.
        myConverter.OcrLanguage = "de"
 
        ' Preserve the Original PDF Files Image Compression
        myConverter.OcrImageCompression = Imaging.Plumbing.ImageCompression.PreserveOriginal
 
        ' Add the user selected PDF file to your transformer
        myConverter.AddDocument(myDocument)
 
        ' Transform the PDF File
        myConverter.Transform()
 
        ' Save the new Searchable PDF file to the extension you specified earlier
        myDocument.SaveAs(searchPath, OverwriteMode.ForceOverwrite)
 
        ' Close the document and clean up
        myDocument.Close()
        myDocument.Dispose()
 
    End Sub
 
End Module