如何使用iTextSharp将TIFF字节数组转换为PDF字节数组?

时间:2018-05-03 19:38:56

标签: c# itext

我一直在尝试在Web应用程序中的浏览器上显示TIFF图像。但由于浏览器不支持TIFF,我尝试将TIFF字节数组转换为PDF字节数组,我唯一可以使用的第三方库是GET

但是我在某种程度上做错了,我身上的iTextSharp错误或生成的PDF都已损坏。

代码:

MemoryStream

如果有人能指出这里的错误,那真的很棒!

提前致谢!

1 个答案:

答案 0 :(得分:0)

我实际上对MemoryStream进行了一些修改,并在ConvertToPDFArray方法中进行了更改并使其正常工作:

 /// <summary>
        /// 
        /// </summary>
        /// <param name="inboundFaxBytes"></param>
        /// <returns></returns>
        public static byte[] ConvertToPDFArray(byte[] inboundFaxBytes)
        {
            byte[] imagePdfBytes = null;

            try
            {                          

                using (MemoryStream ms = new MemoryStream())
                {

                    iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);

                    MemoryStream msBmp = new MemoryStream();
                    msBmp.Write(inboundFaxBytes, 0, inboundFaxBytes.Length);
                    Bitmap faxDocBitmap = new Bitmap(msBmp);

                    iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms);

                    int totalPages = faxDocBitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);

                    //document.SetPageSize(PageSize.A4);
                    document.Open();

                    PdfContentByte cb = writer.DirectContent;
                    for (int page = 0; page < totalPages; ++page)
                    {

                        faxDocBitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, page);

                        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(faxDocBitmap, System.Drawing.Imaging.ImageFormat.Bmp);


                        img.ScalePercent(72f / img.DpiX * 100, 72f / img.DpiY * 100);
                        img.SetAbsolutePosition(0, 0);

                        // Memory Stream is not expandable
                        cb.AddImage(img);

                        document.NewPage();

                    }
                    document.Close();
                    imagePdfBytes = ms.ToArray();
                    ms.Dispose();


                }

            }
            catch (Exception e)
            {
                throw e;
            }
            return imagePdfBytes;
        }