ocrOperation.Execute(executionContext) unable to work in C# winform?
- January 17, 2024
- 0 replies
- 355 views
Hi Everyone,
I have tested in windows console project. It is working.
but ocrOperation.Execute(executionContext) unable to work in C# winform?
Anyone know how to make it run in C# winform project?
private void simpleButton1_Click(object sender, EventArgs e)
{
try
{
CryptoEngine crypto = new CryptoEngine(CryptoEngine.AlgorithmType.TripleDES);
string client_ID = appSettings["Client_ID"];
string client_secret =appSettings["Client_Key"];
string ID = crypto.Decrypt(client_ID);
string key = crypto.Decrypt(client_secret);
if (ID.Length > 0 && key.Length > 0)
{
string inputFileName = "";
string outputFileName = "";
// Initial setup, create credentials instance.
Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()
.WithClientId(ID)
.WithClientSecret(key)
.Build();
//Create an ExecutionContext using credentials and create a new operation instance.
Adobe.PDFServicesSDK.ExecutionContext executionContext = Adobe.PDFServicesSDK.ExecutionContext.Create(credentials);
OCROperation ocrOperation = OCROperation.CreateNew();
// Set operation input from a source file.
//if (args.Length > 0)
{
inputFileName = @"CI033.pdf";//args[0];
outputFileName = @"CI033_ocrpdf"; //args[1];
}
if (inputFileName.Length > 0 && outputFileName.Length > 0)
{
FileRef sourceFileRef = FileRef.CreateFromLocalFile(inputFileName);
ocrOperation.SetInput(sourceFileRef);
// Build OCR options from supported locales and OCR-types and set them into the operation
OCROptions ocrOptions = OCROptions.OCROptionsBuilder()
.WithOcrLocale(OCRSupportedLocale.EN_US)
.WithOcrType(OCRSupportedType.SEARCHABLE_IMAGE_EXACT)
.Build();
ocrOperation.SetOptions(ocrOptions);
// Execute the operation.
FileRef result = ocrOperation.Execute(executionContext);
//Generating a file name
String outputFilePath = CreateOutputFilePath();
// Save the result to the specified location.
// result.SaveAs(Directory.GetCurrentDirectory() + outputFilePath);
result.SaveAs(outputFileName);
}
}
}
catch (ServiceUsageException ex)
{
log.Error("Exception encountered while executing operation", ex);
}
catch (ServiceApiException ex)
{
log.Error("Exception encountered while executing operation", ex);
}
catch (SDKException ex)
{
log.Error("Exception encountered while executing operation", ex);
}
catch (IOException ex)
{
log.Error("Exception encountered while executing operation", ex);
}
catch (Exception ex)
{
log.Error("Exception encountered while executing operation", ex);
}
}
