简体   繁体   English

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

[英]Convert Tiff to pdf with PDFSharp-gdi C#

I am stuck. 我被卡住了。 Currently I am trying to simulate pulling a binary blob from a database that is supposed to be a TIFF image. 目前我正在尝试模拟从应该是TIFF图像的数据库中提取二进制blob。 I use this gist image.tif in the image variable to do so. 我在图像变量中使用这个gist image.tif来做到这一点。 I am pretty sure I am close to making this happen. 我很确定我接近实现这一目标。 It's that the issue probably has to do with how I am converting the string to byte array or something. 这个问题可能与我如何将字符串转换为字节数组等有关。 Basically this application throws an exception stating that it can't create a PDF with 0 frames. 基本上这个应用程序抛出一个异常,声明它无法创建0帧的PDF。 At this point I must admit that I may be in over my head on this one. 在这一点上,我必须承认我可能在这个问题上。 Could someone be so kind and help me the rest of the way with this one? 有人可以这么善良,并帮助我完成剩下的这个吗?

The code is included below: 代码包含在下面:

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;
        }
    }
}

IMPORTANT!!!! 重要!!!! First of all, your example tiff isn't valid at all . 首先,您的示例tiff完全无效 It can't be read by any file editor. 任何文件编辑器都无法读取它。 I have had to take these ones for testing. 我不得不把这些用于测试。

Then, the code has couple errors: 然后,代码有几个错误:

1) I don't understand what've done with strings but reading files and BLOBs are same: 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) method tiff2PDF should be like that 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) 3)

        public static MemoryStream ByteArrayToMemoryStream(byte[] bytestream)
        {

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

            return stream;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM