使用 iTextSharp 将某些 TIF 压缩格式转换为 PDF 时出现问题

Issues converting certain TIF compressions to PDF using iTextSharp

我正在使用 iTextSharp 将单页 TIF 文件转换并拼接为多页 PDF 文件。单页 TIF 文件具有不同的位深度和压缩率。

这是代码-

private void button1_Click(object sender, EventArgs e)
{
    List<string> TIFfiles = new List<string>();
    Document document;
    PdfWriter pdfwriter;
    Bitmap tifFile;
    pdfFilename = <file path>.PDF;
    TIFfiles = <load the path to each TIF file in this array>;

    //Create document 
    document = new Document();

    // creation of the different writers
    pdfwriter = PdfWriter.GetInstance(document, new System.IO.FileStream(pdfFilename, FileMode.Create));

    document.Open();
    document.SetMargins(0, 0, 0, 0);

    foreach (string file in TIFfiles)
    {

        //load the tiff image  
        tifFile = new Bitmap(file);

        //Total number of pages

        iTextSharp.text.Rectangle pgSize = new iTextSharp.text.Rectangle(tifFile.Width, tifFile.Height);
        document.SetPageSize(pgSize);
        document.NewPage();

        PdfContentByte cb = pdfwriter.DirectContent;

        tifFile.SelectActiveFrame(FrameDimension.Page, 0);
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(tifFile, ImageFormat.Tiff);

        // scale the image to fit in the page
        img.SetAbsolutePosition(0, 0);
        cb.AddImage(img);

    }

    document.Close();
}

此代码运行良好,可以将 tif 文件拼接并转换为 PDF。问题在于处理某些类型的 TIF 时创建的处理时间和 pdf 文件大小。

例如

原始 TIF --> B&W/Bit 深度 1/压缩 CCITT T.6 --> 处理速度更快,PDF 文件大小约为 TIF 文件大小的 1.1 倍。

原始 TIF --> Color/Bit 深度 8/压缩 LZW --> 处理速度更快,PDF 文件大小约为 TIF 文件大小的 1.1 倍。

原始TIF --> Color/Bit深度24/压缩JPEG--> 慢速处理,PDF文件大小是~12.5倍 TIF 文件大小。

为什么转换 Color/Bit 深度 24/压缩 JPEG 文件没有得到与其他 tif 文件相似的结果?

此外,此问题仅与 iTextSharp 有关。我让一位同事使用 Java-iText 测试了同一组 TIF 样本,生成的 PDF 尺寸更小(1.1 倍)并且处理速度更快。

不幸的是,我需要使用 .Net 进行此 TIF 到 PDF 的转换,所以我坚持使用 iTextSharp。 关于如何获取这些压缩 JPEG TIF 文件以创建更小尺寸的 PDF 的任何 ideas/suggestions,就像它对其他 TIF 压缩所做的那样?

感谢您的帮助!

此致, AG

我能够使用您提供的代码重现您的问题,但发现在我使用 Image.GetInstance 而不是示例中使用的位图后问题就消失了。使用下面的代码时,文件大小和 运行 时间在 Java 和 C# 之间是相同的。如果您对样本有任何疑问,请随时提出。

        List<string> TIFfiles = new List<string>();
        Document document;
        PdfWriter pdfwriter;
        iTextSharp.text.Image tifFile;
        String pdfFilename = pdfFile;
        TIFfiles = new List<string>();
        TIFfiles.Add(tifFile1);
        TIFfiles.Add(tifFile2);
        TIFfiles.Add(tifFile3);
        TIFfiles.Add(tifFile4);
        TIFfiles.Add(tifFile5);
        TIFfiles.Add(tifFile6);
        TIFfiles.Add(tifFile7);

        //Create document 
        document = new Document();

        // creation of the different writers
        pdfwriter = PdfWriter.GetInstance(document, new System.IO.FileStream(pdfFilename, FileMode.Create));

        document.Open();
        document.SetMargins(0, 0, 0, 0);
        int i = 0;
        while (i < 50)
        {
            foreach (string file in TIFfiles)
            {

                //load the tiff image  
                tifFile = iTextSharp.text.Image.GetInstance(file);

                //Total number of pages

                iTextSharp.text.Rectangle pgSize = new iTextSharp.text.Rectangle(tifFile.Width, tifFile.Height);
                document.SetPageSize(pgSize);
                document.NewPage();

                PdfContentByte cb = pdfwriter.DirectContent;

                // scale the image to fit in the page
                tifFile.SetAbsolutePosition(0, 0);
                cb.AddImage(tifFile);

            }
            i++;
        }
        document.Close();