使用Spire.PDF将PDF转换为Tiff图像会出现异常

时间:2019-08-19 09:35:56

标签: c# .net asp.net-mvc pdf tiff

我正在尝试将服务器上存在的pdf文件转换为tiff图像(因为我的PDF可能有1帧以上)。我尝试了多个链接,发现spire.pdf

我正在关注一个教程 https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Conversion/Save-PDF-Document-as-tiff-image.html

public ActionResult OpenFilee(string fileID)
      {
        PdfDocument doc = new PdfDocument();

  doc.LoadFromFile(Path.Combine(Server.MapPath("~/SafetyUploadedFiles/") + 
 fileID));

        JoinTiffImages(SaveAsImage(doc), "res.tiff", EncoderValue.CompressionLZW);

        System.Diagnostics.Process.Start("res.tiff");
        return View();
    }

    private static Image[] SaveAsImage(PdfDocument document)
    {
        Image[] images = new Image[document.Pages.Count];
        for (int i = 0; i < document.Pages.Count; i++)
        {
            images[i] = document.SaveAsImage(i);
        }
        return images;
    }

    private static ImageCodecInfo GetEncoderInfo(string mimeType)
    {
        ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
        for (int j = 0; j < encoders.Length; j++)
        {
            if (encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
    }

    public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
    {
        //use the save encoder
        Encoder enc = Encoder.SaveFlag;
        EncoderParameters ep = new EncoderParameters(2);
        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
        ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
        Image pages = images[0];
        int frame = 0;
        ImageCodecInfo info = GetEncoderInfo("image/tiff");
        foreach (Image img in images)
        {
            if (frame == 0)
            {
                pages = img;
                //save the first frame
                pages.Save(outFile, info, ep);
            }

            else
            {
                //save the intermediate frames
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                pages.SaveAdd(img, ep);
            }
            if (frame == images.Length - 1)
            {
                //flush and close.
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                pages.SaveAdd(ep);
            }
            frame++;
        }
    }

遇到此错误

  

GDI +中发生一般错误。

在以下行上:

pages.Save(outFile, info, ep);

0 个答案:

没有答案