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.
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.
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.
发布评论
评论(5)
这是使用 PDFSharp 的示例
Here is an example using PDFSharp
您可以尝试使用我们的 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.
我使用免费的 iTextSharp 库创建 PDF 并将标准 System.Drawing.Image 对象插入到 PDF 中,每页一个。
CreateCompressedImageStream
只是获取 System.Drawing.Image 并将其保存为黑白 PNG 以减小文件大小。http://www.itextpdf.com/
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/
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?
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.