Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to resolve the issue related to Excel while reading data from a PDF file via C#?
I am extracting the data from a PDF file and exporting the same to Excel sheet via a windows service. But on doing the same below issue is seen:
RETRIEVING THE COM CLASS FACTORY FOR COMPONENT WITH CLSID {00024500-0000-0000-C000-000000000046} FAILED DUE TO THE FOLLOWING ERROR: 80070005 ACCESS IS DENIED.

What I have tried:

The core code of extracting data from PDF File is as below:
C#
#region GetFillData
      //Copy the Standard Excel Template to DATA folder fetching PDF Data/Move PDF from the File Store to DONE Folder afterwards at last
      private void GetFillData(string SourcePDFFullPath)
      {

          WriteServiceLog("" + SmartPDFLogCaption + "Entered " + MethodInfo.GetCurrentMethod().Name + " Method");
          try
          {

              //Delimiter with which data is fetched from SMART PDF (split with '|')
              Char delimiter1 = '|';
              //Delimiter with which data is fetched from SMART PDF (split with '~' after spliting with '|')
              Char delimiter2 = '~';

              //Pass the PDF File in the FileStore
              PdfReader pdfReader = new PdfReader(SourcePDFFullPath);
              AcroFields pdfFormFields = pdfReader.AcroFields;
              string PDFFileName = Path.GetFileName(SourcePDFFullPath);
              //Get Template Id from Hidden Field(Template ID set Statically) in SMART PDF
              string TemplateID = pdfFormFields.GetField("hidTemplateId");
              //Get PDF Data in control fields from Hidden  Field(PDF) in SMART PDF
              string CapturedPDFValues = pdfFormFields.GetField("hidPDFDataExport");
              string[] PDFValues = CapturedPDFValues.Split(delimiter1);

              //Fetch Unique No(Quotation No) from PDF
              string UniqueNo = PDFValues[0].Substring(PDFValues[0].LastIndexOf(delimiter2) + 1);
              //Set Current Date while rernaming Excel Template in DATA Folder (ddmmyyyy format)configured in APP.Config
              string CurrentDate = DateTime.Now.ToString("" + FileRenameDateFormat + "");


              //Validate If the Template Id is empty for the PDF Files in FileStore Folder
              if (TemplateID == "")
              {
                  WriteServiceLog("" + SmartPDFLogCaption + "" + TemplateIdValidationMsg1 + "" + SourcePDFFullPath + MethodInfo.GetCurrentMethod().Name + " Method");
                  return;

              }
              //Capture PDF Data If the Template Id Exists for the PDF File in FileStore Folder
              else
              {
                  //Validate Excel Template  exists in the Template Folder
                  string ExcelTemplateFullPath = string.Empty;
                  string ExcelExtension = string.Empty;
                  //Save the Excel Template with data in the Destination Excel Folder(DATA Folder in FileStore) with UniqueNo+Currentdate(ddmmyyyy format)
                  string DestinationFullPathExcel = string.Empty;
                  if (File.Exists(ExcelTemplatePath+TemplateNamingStart+TemplateID+".xls"))
                  {
                      ExcelExtension = ".xls";
                      ExcelTemplateFullPath = ExcelTemplatePath + TemplateNamingStart + TemplateID + ExcelExtension;
                      DestinationFullPathExcel = DestinationExcelPath + TemplateNamingStart + TemplateID + "_" + UniqueNo + CurrentDate + ExcelExtension;

                  }
                  else if (File.Exists(ExcelTemplatePath + TemplateNamingStart + TemplateID + ".xlsx"))
                  {
                      ExcelExtension = ".xlsx";
                      ExcelTemplateFullPath = ExcelTemplatePath + TemplateNamingStart + TemplateID + ExcelExtension;
                      DestinationFullPathExcel = DestinationExcelPath + TemplateNamingStart + TemplateID + "_" + UniqueNo + CurrentDate + ExcelExtension;

                  }
                  else
                  {

                      WriteServiceLog("" + SmartPDFLogCaption + "Template Id:" + TemplateNamingStart + TemplateID + " doesn't exists in the Template Folder" + MethodInfo.GetCurrentMethod().Name + " Method");
                      return;
                  }
                     //Capture PDF Data If Template Id Exists for the Excel Template in Template Folder
                          //Set the   Excel App object and with avoidance of Excel alerts
                          _Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                          xlApp.DisplayAlerts = false;
                          //fetch the Quotation No after Splitting pdf data
                          object misValue = System.Reflection.Missing.Value;
                          _Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
                          //read the Standard Excel Template fILE(Path Included)(Renamed) set in the Template Folder configured in App.Config
                          xlWorkBook = xlApp.Workbooks.Open(ExcelTemplateFullPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                          _Worksheet xlWorkSheet = (_Worksheet)xlWorkBook.Worksheets.get_Item(1);
                          //Setting all PDF DATA/Values fetched to the Std Excel Template moved to the DATA Folder
                          for (int i = 0; i < PDFValues.Length; i++)
                          {
                              xlWorkSheet.Cells[i + 1, 2] = "'" + PDFValues[i].Substring(PDFValues[i].LastIndexOf(delimiter2) + 1);

                          }
                          //Format A1:D1 as bold, vertical alignment = center.
                          //xlWorkSheet.get_Range("A1", "D1").Font.Bold = true;
                          //Excel saved with data from PDF File to DATA Folder
                          xlWorkBook.SaveAs(DestinationFullPathExcel, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                          //Move the PDF from File Store Folder to the DONE Folder
                          //Fetch the Destination PDF Path configured in App.Config with file name of the Source PDF File in Filestore Folder
                          string DestinationPDFFullPath = DestinationPDFPath + Path.GetFileName(SourcePDFFullPath);
                          //Delete the PDF if it's already exisitng in the Destination PDF Path
                          if (File.Exists(DestinationPDFFullPath))
                          {
                              File.Delete(DestinationPDFFullPath);

                          }
                          //Move the PDF file from FileStore to Done Folder
                          File.Move(SourcePDFFullPath, DestinationPDFFullPath);
                          //Close the Excel App and Release Excel Object
                          xlWorkBook.Close(true, misValue, misValue);
                          xlApp.Quit();

                          releaseObject(xlWorkSheet);
                          releaseObject(xlWorkBook);
                          releaseObject(xlApp);



                      //Close the PDF Reader Object
                      pdfReader.Close();


                  }



          }
          catch (Exception ex)
          {
              WriteErrorLog(MethodInfo.GetCurrentMethod().Name + " " + ex.Message, ex.StackTrace, true);
          }
          finally
          {

          }
          WriteServiceLog("" + SmartPDFLogCaption + "Finished " + MethodInfo.GetCurrentMethod().Name + " Method");
      }
      #endregion
Posted
Updated 16-Mar-16 4:35am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900