简体   繁体   中英

How can I convert TIFF byte array to a PDF byte array using iTextSharp?

I have been trying to display a TIFF image on the browser in my Web Application. But as browsers don't support TIFF, I tried converting the TIFF byte array to a PDF byte array and the only 3rd party library that I can use is iTextSharp .

But I have been somehow doing something wrong that the MemoryStream errors out on me or the generated PDF is corrupted.

The code:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace TiffToPDFApplication
{
    public class UsingiTextSharp
    {

       /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        public string TiffToPdfConverter(string path, string targetLoc, string fileName)
        {

            string fileNameWithLoc = String.Empty;
            string fileNameWithLoc2 = String.Empty;
            if (fileName == String.Empty)
            {
                string[] pathArr = path.Split('\\');
                string[] fileArr = pathArr.Last().Split('.');
                fileName = fileArr[0];

            }
            fileName = fileName + ".pdf";

            if (File.Exists(path))
            {
                try
                {
                    FileStream fileStream = File.OpenRead(path);
                    byte[] tiffByteArray = new byte[fileStream.Length];
                    fileStream.Read(tiffByteArray, 0, tiffByteArray.Length);
                    fileStream.Close();

                    convertImage(tiffByteArray);

                    // Convert to PDF Byte Array
                    byte[] pdfbyteArray = ConvertToPDFArray(tiffByteArray);


                    //FileName and Location
                    fileNameWithLoc = targetLoc + "\\" + fileName;

                    // Writing File to the Local Location
                    using (Stream file = File.OpenWrite(fileNameWithLoc))
                    {
                        file.Write(pdfbyteArray, 0, pdfbyteArray.Length);
                    }


                }
                catch(Exception ex)
                {
                    throw ex;
                }
       }

            return fileNameWithLoc;
        }






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

            try
            {


                using (MemoryStream ms = new MemoryStream())
                {
                    Bitmap faxDocBitmap;
                     ms.Write(inboundFaxBytes, 0, inboundFaxBytes.Length);
                    faxDocBitmap = new Bitmap(ms);
                    iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);

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

It would be really great if someone can point out what am doing wrong here!

Thanks in advance!

I actually did a little bit of tinkering around with the MemoryStream and made changes in the ConvertToPDFArray method and got it working :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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