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 assumes you have already completed the Getting Started sample.
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
using System;
using SolidFramework.Converters.Plumbing;
using SolidFramework.Pdf;
using SolidFramework.Pdf.Transformers;
namespace CSharp_Tutorials
{
public static partial class Tutorials
{
public static bool OcrTransformer(string pdfPath, string outputPath)
{
// Ensure license allows OCR
if (!SolidFramework.License.Allows(SolidFramework.Plumbing.LicensePermissions.Ocr))
{
Console.WriteLine("License doesn't allow OCR - skipping OcrTransformer");
Console.WriteLine();
return false;
}
// Create a PdfDocument and open the PDF file
using (var document = new PdfDocument(pdfPath))
{
document.Open();
// Create an OcrTransformer
var transformer = new OcrTransformer();
// Set the OCR options to your liking
transformer.OcrType = OcrType.CreateSearchableTextLayer;
transformer.OcrEngine = TextRecoveryEngine.SolidOCR;
transformer.OcrLanguage = "en";
transformer.OcrImageCompression = SolidFramework.Imaging.Plumbing.ImageCompression.PreserveOriginal;
// Add the PdfDocument to the transformer
transformer.AddDocument(document);
Console.WriteLine("Transforming " + outputPath + " using an OcrTransformer");
// Transform the PdfDocument
transformer.Transform();
// Save the updated document
document.SaveAs(outputPath, SolidFramework.Plumbing.OverwriteMode.ForceOverwrite);
}
Console.WriteLine("Successfully transformed " + outputPath + " using an OcrTransformer");
Console.WriteLine();
return true;
}
}
}
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
Imports System
Imports SolidFramework.Converters.Plumbing
Imports SolidFramework.Pdf
Imports SolidFramework.Pdf.Transformers
Namespace VBNet_Tutorials
Partial Module Tutorials
Function OcrTransformer(ByVal pdfPath As String, ByVal outputPath As String) As Boolean
' Ensure license allows OCR
If Not SolidFramework.License.Allows(SolidFramework.Plumbing.LicensePermissions.Ocr) Then
Console.WriteLine("License doesn't allow OCR - skipping OcrTransformer")
Console.WriteLine()
Return False
End If
' Create a PdfDocument And open the PDF file
Using document As PdfDocument = New PdfDocument(pdfPath)
document.Open()
' Create an OcrTransformer
Dim transformer As OcrTransformer = New OcrTransformer()
' Set the OCR options to your liking
transformer.OcrType = OcrType.CreateSearchableTextLayer
transformer.OcrEngine = TextRecoveryEngine.SolidOCR
transformer.OcrLanguage = "en"
transformer.OcrImageCompression = SolidFramework.Imaging.Plumbing.ImageCompression.PreserveOriginal
' Add the PdfDocument to the transformer
transformer.AddDocument(document)
Console.WriteLine("Transforming " & outputPath & " using an OcrTransformer")
' Transform the PdfDocument
transformer.Transform()
' Save the updated document
document.SaveAs(outputPath, SolidFramework.Plumbing.OverwriteMode.ForceOverwrite)
End Using
Console.WriteLine("Successfully transformed " & outputPath & " using an OcrTransformer")
Console.WriteLine()
Return True
End Function
End Module
End Namespace