【问题标题】:Issues converting certain TIF compressions to PDF using iTextSharp使用 iTextSharp 将某些 TIF 压缩转换为 PDF 的问题
【发布时间】:2015-06-13 22:24:28
【问题描述】:

我正在使用 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 --> 黑白/位深 1/压缩 CCITT T.6 --> 处理速度更快,PDF 文件大小约为 TIF 文件大小的 1.1 倍。

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

原始 TIF --> 颜色/位深 24/压缩 JPEG--> 处理,PDF 文件大小是 TIF 文件大小的 ~12.5x 倍

为什么转换 Color/Bit depth 24/Compression JPEG 文件不会得到与其他 tif 文件类似的结果?

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

不幸的是,我需要使用 .Net 进行 TIF 到 PDF 的转换,所以我坚持使用 iTextSharp。 关于如何让这些压缩 JPEG TIF 文件像其他 TIF 压缩一样创建更小尺寸的 PDF 的任何想法/建议?

感谢您的帮助!

问候, AG

【问题讨论】:

  • 您使用的是哪个版本的 iTextSharp。对于每个新版本,我们都增加了对更多 TIFF 版本的支持。
  • 我使用的是 v5.5.4.0。如果有比这更新的版本,请告诉我。我也可以用那个版本进行测试。
  • 5.5.5.0 是最新的,但 5.5.4.0 对于 TIFF 更新来说已经足够新了。也许这应该发布给 iText 支持团队,因为 iTextSharp 和 iText 之间不应该有任何实质性区别。他们可以在哪里与您联系以获取重现问题的示例 TIFF 文件?
  • 我已将我的电子邮件地址添加到我的 StackOverflow 个人资料中。请请求 iText 支持团队与我联系以获取示例文件。感谢你的帮助!再次感谢!
  • 您的请求收到票号 DEV-1364。它目前的优先级最低,因为我不知道您的雇主/客户是否是 iText Software 客户。如果他已经在生产环境中使用 iText,他可能已经在使用,因为在商业环境中使用 iText 5.5.4 或 iTextSharp 5.5.4.0 需要商业许可。

标签: itextsharp itext


【解决方案1】:

我能够使用您提供的代码重现您的问题,但发现在我使用 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();

【讨论】:

  • 太棒了!我做了你建议的改变,它也适用于我。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2011-02-16
  • 2017-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-03-07
  • 1970-01-01
相关资源
最近更新 更多