Unable to run C# code in PowerShell

I’ve found some C# code that would be incredibly useful for me, that I want to run using PowerShell as part of an automation. Basically I want to extract the Table of Contents from a pdf, import them into excel and then use them in a macro I created. I’ve tried my best to correctly use C# code in PowerShell without having to “translate” all of the code, as well as adjusting the locations of the input file (pdf) and the output file (txt), as I need it to be dynamic. I’ve also tried debugging, to the best of my ability, but new problems keep on appearing. Maybe some of you guys are able to see what I’ve done wrong and can help me out?

All help is appreciated :slight_smile:

Edit: To be clear, the there is nothing wrong with the C# code (except maybe what I’ve added when defining inputFile and outputFile), so I’m asking what I have done wrong before and after the $code = @"

"@

$folder = Get-Location

 

$folderPath = (Join-Path $folder "itext.kernel.dll")

 

$Assem = ($folderPath)

 

    $code = @"

    using System;

    using System.Collections.Generic;

    using System.IO;

    using iText.Kernel.Pdf;

 

    namespace iText.Samples.Sandbox.Interactive

    {

        public class FetchBookmarkTitles

        {

            public static void Main(String[] args)

            {

                string currentDirectory = Environment.CurrentDirectory;

                string inputFile = (Get-ChildItem *.pdf)[0].FullName;

                string outputFile = Path.Combine(currentDirectory, "bookmarks.txt");

 

                new FetchBookmarkTitles().ManipulatePdf(inputFile, outputFile);

            }

 

            public void ManipulatePdf(String src, String dest)

            {

                PdfDocument pdfDoc = new PdfDocument(new PdfReader(src));

 

                // This method returns a complete outline tree of the whole document.

                // If the flag is false, the method gets cached outline tree (if it was cached

                // via calling getOutlines method before).

                PdfOutline outlines = pdfDoc.GetOutlines(false);

                IList<PdfOutline> bookmarks = outlines.GetAllChildren();

 

                pdfDoc.Close();

 

                List<String> titles = new List<String>();

                foreach (PdfOutline bookmark in bookmarks)

                {

                    AddTitle(bookmark, titles);

                }

 

                // See title's names in the console

                foreach (String title in titles)

                {

                    Console.WriteLine(title);

                }

 

                CreateResultTxt(dest, titles);

            }

 

            // This recursive method calls itself if an examined bookmark entry has kids.

            // The method writes bookmark title to the passed list

            private void AddTitle(PdfOutline outline, List<String> result)

            {

                String bookmarkTitle = outline.GetTitle();

                result.Add(bookmarkTitle);

 

                IList<PdfOutline> kids = outline.GetAllChildren();

                if (kids != null)

                {

                    foreach (PdfOutline kid in kids)

                    {

                        AddTitle(kid, result);

                    }

                }

            }

 

            private void CreateResultTxt(String dest, List<String> titles)

            {

                using (StreamWriter writer = new StreamWriter(dest))

                {

                    for (int i = 0; i < titles.Count; i++)

                    {

                        writer.Write("Title " + i + ": " + titles[i] + "\n");

                    }

                }

            }

        }

    }

"@

 

Add-Type -TypeDefinition $code -Language CSharp -ReferencedAssemblies $Assem

[System.Reflection.Assembly]::LoadFrom($folderPath)

Invoke-Expression "iText.Samples.Sandbox.Interactive.FetchBookmarkTitles.Main('')"