需要 .NET 库将 TIFF 文件转换为 PDF

发布于 2024-09-15 22:15:47 字数 1491 浏览 6 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

謌踐踏愛綪 2024-09-22 22:15:47

这是使用 PDFSharp 的示例

using System;
using System.Collections.Generic;
using System.Text;
using PdfSharp.Drawing;
using PdfSharp.Pdf;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument s_document = new PdfDocument();

            PdfPage page = s_document.AddPage();

            XGraphics gfx = XGraphics.FromPdfPage(page);


            XImage image = XImage.FromFile(@"C:\Image.tif");

            page.Width = image.PointWidth;
            page.Height = image.PointHeight;

            gfx.DrawImage(image, 0, 0);

            s_document.Save(@"C:\Doc.pdf");
        }
    }
}

Here is an example using PDFSharp

using System;
using System.Collections.Generic;
using System.Text;
using PdfSharp.Drawing;
using PdfSharp.Pdf;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument s_document = new PdfDocument();

            PdfPage page = s_document.AddPage();

            XGraphics gfx = XGraphics.FromPdfPage(page);


            XImage image = XImage.FromFile(@"C:\Image.tif");

            page.Width = image.PointWidth;
            page.Height = image.PointHeight;

            gfx.DrawImage(image, 0, 0);

            s_document.Save(@"C:\Doc.pdf");
        }
    }
}
冬天的雪花 2024-09-22 22:15:47

您可以尝试使用我们的 LibTiff.Net 库。它是免费且开源的(BSD 许可证),并附带 tiff2pdf 实用程序,可能正是您所需要的。

You can try our LibTiff.Net library for this. It's free and open-source (BSD License) and comes with tiff2pdf utility that probably does exactly what you need.

天涯沦落人 2024-09-22 22:15:47

我使用免费的 iTextSharp 库创建 PDF 并将标准 System.Drawing.Image 对象插入到 PDF 中,每页一个。 CreateCompressedImageStream 只是获取 System.Drawing.Image 并将其保存为黑白 PNG 以减小文件大小。

http://www.itextpdf.com/

    public byte[] CreatePDF(IEnumerable<Image> images)
    {
        if (!images.Any())
        {
            throw new ArgumentException("You haven't specified any images.");
        }

        var stream = new MemoryStream();
        using(var doc = new Document())
        {
            PdfWriter.GetInstance(doc, stream);
            doc.Open();

            int i = 0;
            foreach (var image in images)
            {
                i++;
                var docImage = iTextSharp.text.Image.GetInstance(this.CreateCompressedImageStream(image));
                docImage.ScaleToFit(doc.PageSize.Width, doc.PageSize.Height);
                doc.Add(docImage);

                if (i + 1 < images.Count())
                {
                    doc.NewPage();
                }
            }

            doc.Close();
        }

        return stream.ToArray();
    }

    private Stream CreateCompressedImageStream(Image image)
    {
        MemoryStream imageStream = new MemoryStream();

        var info = ImageCodecInfo.GetImageEncoders().FirstOrDefault(i => i.MimeType.ToLower() == "image/png");
        EncoderParameter colorDepthParameter = new EncoderParameter(Encoder.ColorDepth, 1L);
        var parameters = new EncoderParameters(1);
        parameters.Param[0] = colorDepthParameter;

        image.Save(imageStream, info, parameters);

        imageStream.Position = 0;
        return imageStream;
    }

I used the free iTextSharp library to create a PDF and insert standard System.Drawing.Image objects into the PDF, one per page. The CreateCompressedImageStream simply takes the System.Drawing.Image and saves it as a black and white PNG to reduce the file size.

http://www.itextpdf.com/

    public byte[] CreatePDF(IEnumerable<Image> images)
    {
        if (!images.Any())
        {
            throw new ArgumentException("You haven't specified any images.");
        }

        var stream = new MemoryStream();
        using(var doc = new Document())
        {
            PdfWriter.GetInstance(doc, stream);
            doc.Open();

            int i = 0;
            foreach (var image in images)
            {
                i++;
                var docImage = iTextSharp.text.Image.GetInstance(this.CreateCompressedImageStream(image));
                docImage.ScaleToFit(doc.PageSize.Width, doc.PageSize.Height);
                doc.Add(docImage);

                if (i + 1 < images.Count())
                {
                    doc.NewPage();
                }
            }

            doc.Close();
        }

        return stream.ToArray();
    }

    private Stream CreateCompressedImageStream(Image image)
    {
        MemoryStream imageStream = new MemoryStream();

        var info = ImageCodecInfo.GetImageEncoders().FirstOrDefault(i => i.MimeType.ToLower() == "image/png");
        EncoderParameter colorDepthParameter = new EncoderParameter(Encoder.ColorDepth, 1L);
        var parameters = new EncoderParameters(1);
        parameters.Param[0] = colorDepthParameter;

        image.Save(imageStream, info, parameters);

        imageStream.Position = 0;
        return imageStream;
    }
山人契 2024-09-22 22:15:47

Sam Leffler 的 libtiff 附带了各种命令行实用程序。其中之一,tiff2pdf.exe 将 TIFF(包括多页 TIFF)转换为 PDF。

这不是一个选择吗?

Sam Leffler's libtiff ships with various commandline utilities. One of these, tiff2pdf.exe converts TIFFs (including multipage TIFFs) to PDF.

Isn't that an option?

缺⑴份安定 2024-09-22 22:15:47

System.Drawing.Imaging.Bitmap 类将允许您打开多页 TIFF 并提取每个帧。然后,您需要确定每个帧中图像的大小,创建该大小的 PDF 页面,然后在这些页面上绘制位图。

我在一篇标题为“使用 2011 年发布的 PDFOne .NET 将多页 TIFF 转换为 PDF

。.NET 提供图像提取功能。任何 PDF 创建库都可以完成其余的工作。

免责声明:我为诺斯替斯。

The System.Drawing.Imaging.Bitmap class will allow you to open a multipage TIFF and extract each frame. You then need to determine the size of the image in each frame, create PDF pages of that size and then draw the bitmaps on those page.

I have done just the thing for our product Gnostice PDFOne .NET in an article titled "Convert A Multipage TIFF To PDF Using PDFOne .NET published in 2011.

.NET provides the image extraction functionality. Any PDF-creation library will be able to do the rest of the job.

DISCLAIMER: I work for Gnostice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文