0

I tried to create a little program to convert PDF to a TIF file using ghostscript but unfortunately it results in an error ("null"). Can't figure out why it's failing:

    void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "PDF Files|*.pdf";
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            strfilename = openFileDialog1.FileName;
        }
    }

    void button2_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog targetfolder = new FolderBrowserDialog();
        if (targetfolder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            folder = targetfolder.SelectedPath;
        }
    }

    void button3_Click(object sender, EventArgs e)
    {
        const string DLL_64BITS = "gsdll64.dll";
        string NomeGhostscriptDLL;
        NomeGhostscriptDLL = DLL_64BITS;
        GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(NomeGhostscriptDLL);
        ///var xDpi = 300;
        var yDpi = 300;
        using (var rasterizer = new GhostscriptRasterizer())
        {
            byte[] buffer = File.ReadAllBytes(strfilename);
            MemoryStream ms = new MemoryStream(buffer);
            rasterizer.Open(ms, gvi, true);
            int PdfPages = rasterizer.PageCount;
            for (int pageNumber = 1; pageNumber < rasterizer.PageCount; pageNumber++)
            {
                string outputTIFPath = Path.Combine(folder, "00" + pageNumber.ToString() + ".tiff");
                Image pdf2TIF = rasterizer.GetPage(yDpi, pageNumber);
                MessageBox.Show(outputTIFPath);
                pdf2TIF.Save(outputTIFPath, ImageFormat.Tiff);
            }
            rasterizer.Close();
        }
    }

The error looks like this enter image description here

Can anyone help me to sort this out?

Moritz
  • 377
  • 1
  • 6
  • 21
  • it seems that it does not load the dll correctly (when I install Ghostscript and change it slightly it works) – Moritz Sep 16 '22 at 13:12

1 Answers1

0

try adding this

MyPlaceHolder.Controls.Add(pd2TIF);

below:

Image pdf2TIF = rasterizer.GetPage(yDpi, pageNumber);

I just read that on a different thread. im not 100% sure if it works

  • not sure if this will work as "pdf2tiff" is a system image and not a control. I tink there might be a problem with the dll (not correctly loaded or something like this) – Moritz Sep 16 '22 at 12:33