Skip to main content
Participant
July 11, 2024

I have error Adobe.PDFServicesSDK

  • July 11, 2024
  • 0 replies
  • 139 views

My code : 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Text.Json;
using System.IO.Compression;

using log4net.Repository;
using log4net.Config;
using log4net;

using Adobe.PDFServicesSDK;
using Adobe.PDFServicesSDK.auth;
using Adobe.PDFServicesSDK.pdfops;
using Adobe.PDFServicesSDK.io;
using Adobe.PDFServicesSDK.exception;
using Adobe.PDFServicesSDK.options.extractpdf;
using System.IO;

namespace PDFapi
{
    public partial class Form1 : Form
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Program));

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select your PDF file";
            openFileDialog1.FileName = "";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Clear();

                string filePath = openFileDialog1.FileName;

                String output = "ExtractTextInfoFromPDF.zip";
                if (File.Exists(Directory.GetCurrentDirectory() + output))
                {
                    File.Delete(Directory.GetCurrentDirectory() + output);
                }

                // Initial setup, create credentials instance.
                Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()
                    .WithClientId("36*****c85")
                    .WithClientSecret("p8****MN")
                    .Build();

                // Create an ExecutionContext using credentials and create a new operation instance.
                ExecutionContext executionContext = ExecutionContext.Create(credentials);

                //----------------------------------------------------------------

                ExtractPDFOperation extractPdfOperation = ExtractPDFOperation.CreateNew();

                // Provide an input FileRef for the operation.
                FileRef sourceFileRef = FileRef.CreateFromLocalFile(filePath);
                extractPdfOperation.SetInputFile(sourceFileRef);

                // Build ExtractPDF options and set them into the operation.
                ExtractPDFOptions extractPdfOptions = ExtractPDFOptions.ExtractPDFOptionsBuilder()
                    .AddElementsToExtract(new List<ExtractElementType>(new[] { ExtractElementType.TEXT }))
                    .Build();
                extractPdfOperation.SetOptions(extractPdfOptions);

                //----------------------------------------------------------------

                //----------------------------------------------------------------
                // Execute the operation.
                FileRef result = extractPdfOperation.Execute(executionContext);

                // Save the result to the specified location.
                result.SaveAs(Directory.GetCurrentDirectory() + output);

                //----------------------------------------------------------------


                //----------------------------------------------------------------
                ZipArchive archive = ZipFile.OpenRead(Directory.GetCurrentDirectory() + output);
                ZipArchiveEntry jsonEntry = archive.GetEntry("structuredData.json");
                StreamReader osr = new StreamReader(jsonEntry.Open());
                String contents = osr.ReadToEnd();

                JsonElement data = JsonSerializer.Deserialize<JsonElement>(contents);

                //----------------------------------------------------------------

                JsonElement elements = data.GetProperty("elements");
                foreach (JsonElement element in elements.EnumerateArray())
                {
                    JsonElement pathElement = element.GetProperty("Path");
                    String path = pathElement.GetString();
                    if (path.EndsWith("/H1"))
                    {
                        JsonElement textElement = element.GetProperty("Text");
                       // Console.Write(textElement.GetString() + "\n");
                        textBox1.AppendText(textElement.GetString() + Environment.NewLine);
                    }
                }

            }
        }
    }
}

 

 

After select PDF file i have error:

 

Adobe.PDFServicesSDK.exception.ServiceApiException was unhandled
ErrorCode=UNKNOWN
HResult=-2146233088
Message=Error response received for the request
RequestId=b0d08b45-7b45-4645-a814-7ed33dd433d0
Source=Adobe.PDFServicesSDK

 

In line : FileRef result = extractPdfOperation.Execute(executionContext); 

My internet  connection is norm.

 

Why doesn't it work?