使用 PDFSharp-gdi C# 将 Tiff 转换为 pdf

Convert Tiff to pdf with PDFSharp-gdi C#

我卡住了。目前我正在尝试模拟从应该是 TIFF 图像的数据库中提取二进制 blob。我在图像变量中使用这个要点 image.tif 来做到这一点。我很确定我即将实现这一目标。这个问题可能与我如何将字符串转换为字节数组或其他东西有关。基本上这个应用程序会抛出一个异常,说明它无法创建具有 0 帧的 PDF。在这一点上,我必须承认我可能无法理解这一点。有人能这么好心帮我完成剩下的路吗?

代码如下:

using System;
using System.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Text;

namespace ConvertTifToPDFFile
{
    class Program
    {

        static void Main(string[] args)
        {
            string image = "";

            byte[] imageAsByteStream = Encoding.ASCII.GetBytes(image);

            int imageByteStreamLength = imageAsByteStream.Length;

            string base64EncodedImage = Convert.ToBase64String(imageAsByteStream);

            imageAsByteStream = Encoding.ASCII.GetBytes(base64EncodedImage);

            Stream imageStream = TiffImageSplitter.ByteArrayToMemoryStream(imageAsByteStream);
            // Image splitImage = TiffImageSplitter.getTiffImage(imageStream, 1);
            TiffImageSplitter.tiff2PDF(imageStream);


        }
    }

    public class TiffImageSplitter
    {
        private static TiffImageSplitter tiff = new TiffImageSplitter();

        public static void tiff2PDF(Stream imageByteStream)
        {

            PdfDocument doc = new PdfDocument();

            int pageCount = getPageCount(imageByteStream);

            for (int i = 0; i < pageCount; i++)
            {
                PdfPage page = new PdfPage();
                Image img = getTiffImage(imageByteStream, 1);
                XImage imgFrame = XImage.FromGdiPlusImage(img);

                page.Width = imgFrame.PointWidth;
                page.Height = imgFrame.PointHeight;
                doc.Pages.Add(page);

                XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[i]);

                xgr.DrawImage(img, 0, 0);
            }

            doc.Save("C:/temp/test.pdf");

            doc.Close();

        }


        public static Image getTiffImage(Stream imageStream, int pageNumber)
        {
            MemoryStream ms = null;
            Image returnImage = null;

            try
            {
                ms = new MemoryStream();
                Image sourceImage = Image.FromStream(imageStream, true, true);

                Guid objGuid = sourceImage.FrameDimensionsList[0];

                FrameDimension objDimension = new FrameDimension(objGuid);

                sourceImage.SelectActiveFrame(objDimension, pageNumber);

                sourceImage.Save(ms, ImageFormat.Tiff);

                returnImage = Image.FromStream(ms);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex);
                returnImage = null;
            }

            return returnImage;
        }

        public static MemoryStream ByteArrayToMemoryStream(byte[] bytestream)
        {

            MemoryStream stream = new MemoryStream();
            stream.Write(bytestream, 0, bytestream.Length);

            return stream;
        }

        public static int getPageCount(Stream imageStream)
        {
            int pageCount = -1;
            try
            {
                Image img = Image.FromStream(imageStream, true, true);
                pageCount = img.GetFrameCount(FrameDimension.Page);
                img.Dispose();

            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex);
                pageCount = 0;
            }

            return pageCount;
        }
    }
}

重要!!!!首先,您的示例 tiff 根本无效。任何文件编辑器都无法读取它。我不得不参加 these ones 考试。

然后,代码有几个错误:

1) 我不明白对字符串做了什么,但读取文件和 BLOB 是一样的:

        static void Main(string[] args)
        {
            //string image = "";

            //byte[] imageAsByteStream = Encoding.ASCII.GetBytes(image);

            byte[] imageAsByteStream = File.ReadAllBytes("../../../MARBIBM.TIF");

            //int imageByteStreamLength = imageAsByteStream.Length;

            //string base64EncodedImage = Convert.ToBase64String(imageAsByteStream);

            //imageAsByteStream = Encoding.ASCII.GetBytes(base64EncodedImage);

            Stream imageStream = TiffImageSplitter.ByteArrayToMemoryStream(imageAsByteStream);
            // Image splitImage = TiffImageSplitter.getTiffImage(imageStream, 1);
            TiffImageSplitter.tiff2PDF(imageStream);


        }

2) 方法tiff2PDF应该是这样的


        public static void tiff2PDF(Stream imageByteStream)
        {

            PdfDocument doc = new PdfDocument();

            int pageCount = getPageCount(imageByteStream);

            for (int i = 0; i < pageCount; i++)
            {
                PdfPage page = new PdfPage();
                Image img = getTiffImage(imageByteStream, i); //<---HERE WAS ANOTHER ERROR, LOOK AT i
                XImage imgFrame = XImage.FromGdiPlusImage(img);

3)

        public static MemoryStream ByteArrayToMemoryStream(byte[] bytestream)
        {

            MemoryStream stream = new MemoryStream(bytestream);
            //stream.Write(bytestream, 0, bytestream.Length); 

            return stream;
        }