0

I have the following c# Console app I would run this in ssis but i am using a couple of PDF manipulating librarys. so i am going to call an exe from my ssis package while passing in a file path.

But i am getting the following error when trying to run via the exe.

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at ConsoleApp.program.Main(String[] args) line 87

BUT if i run in debug it works fine. Once i get it working on its own via the exe, i want to pass the filepath as a parameter in ssis.

see c# below

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using org.apache.pdfbox.pdmodel;
    using org.apache.pdfbox.util;
    using System.IO;

    namespace PDF_Read_ConsoleApp
    {
        class Program
        {       
            public static void FilePath(string path)
            {

                //Console.WriteLine("Please enter full pdf path \n\n ");

                //path = Console.ReadLine();

                string fp;

                fp = @path;

                string[] files = Directory.GetFiles(path, "*.pdf");

                foreach (string s in files)    
                {

                    string txtOutput = s.Replace(".pdf", ".txt");    
                    if (File.Exists(txtOutput))
                    {
                        File.Delete(txtOutput);
                    }

                    string output;   

                    PDDocument doc = null;

                    try
                    {
                        doc = PDDocument.load(s);
                        PDFTextStripper stripper = new PDFTextStripper();
                        stripper.getText(doc);

                        output = stripper.getText(doc);

                        StreamWriter NewFile;
                        NewFile = new StreamWriter(txtOutput);

                        //NewFile.Write(output.ToString());
                        NewFile.Write(output.ToString());
                        NewFile.Close();      

                    }
                    finally
                    {
                        //if (doc != null)
                        //{
                        doc.close();

                        //    Console.WriteLine("\n\n File saveed - ({0} ", txtOutput);

                        //}
                    }
                }
            }

            static void Main(string[] args)
            {    
                args[0] = @"C:\SSIS_Packages\PDF_Import\PDF_Import\PO_pdfs";   ////   TESTING FILE PATH1

                FilePath(args[0]);    

            }
        }
    }

Kind Regards

Rob

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Rob
  • 59
  • 1
  • 12
  • Check the count of the parameters, it may not be the value as you expected – kennyzx Dec 03 '19 at 13:57
  • Why are you trying to fill the arg inside the main? just use a variable. the arg use used when you call your program from the cmd – gandalf Dec 03 '19 at 13:59
  • is args in Main method initalized? – Stepagrus Dec 03 '19 at 13:59
  • I don't think we can help you since the issue seems to be with how you call the application. I can only tell you one of many debugging ideas. Add a call to `Console.WriteLine($"There are {args.Length} arguments");` in your Main method before calling `FilePath()`. This will print how many arguments your application has received. You will get the exception if the value is 0, however if it's anything above that you should not get a `System.IndexOutOfRangeException`. You can now call it from any place you'd like and check if the value is above 0. If not, you're not calling it correctly. – Joelius Dec 03 '19 at 17:55

1 Answers1

0

I have managed to get it working, I need to enter an argument within the debug screen, see information in URL below

Console app arguments, how arguments are passed to Main method

THank you for everyone's comments

Rob
  • 59
  • 1
  • 12