C#中的JPG到PDF转换器

我想从图像(如jpg或png)转换为PDF。

我已经查看了ImageMagickNET ,但它对我的需求来说太复杂了。

还有哪些其他.NET解决方案或代码可用于将图像转换为PDF?

iTextSharp非常干净,是开源的。 此外,它还有作者非常好的随书,如果你最终做了更多有趣的事情,比如管理表格,我推荐这本书 。 对于正常使用,邮件列表和新闻组中有大量资源可用于处理常见事项的示例。

编辑:正如在@ Chirag的评论中提到的那样 , @ Darin的答案代码肯定会编译当前版本。

用法示例:

public static void ImagesToPdf(string[] imagepaths, string pdfpath) { using(var doc = new iTextSharp.text.Document()) { iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(pdfpath, FileMode.Create)); doc.Open(); foreach (var item in imagepaths) { iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(item); doc.Add(image); } } } 

使用iTextSharp轻松:

 class Program { static void Main(string[] args) { Document document = new Document(); using (var stream = new FileStream("test.pdf", FileMode.Create, FileAccess.Write, FileShare.None)) { PdfWriter.GetInstance(document, stream); document.Open(); using (var imageStream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var image = Image.GetInstance(imageStream); document.Add(image); } document.Close(); } } } 

我们非常幸运的是PDFSharp(我们每天用它来进行TIFF和文本到PDF的转换,以获得数百项医疗索赔)。

http://pdfsharp.com/PDFsharp/

借助Docotic.Pdf库可以轻松完成此任务。

这是一个从给定图像创建PDF的示例(实际上不仅仅是JPG):

 public static void imagesToPdf(string[] images, string pdfName) { using (PdfDocument pdf = new PdfDocument()) { for (int i = 0; i < images.Length; i++) { if (i > 0) pdf.AddPage(); PdfPage page = pdf.Pages[i]; string imagePath = images[i]; PdfImage pdfImage = pdf.AddImage(imagePath); page.Width = pdfImage.Width; page.Height = pdfImage.Height; page.Canvas.DrawImage(pdfImage, 0, 0); } pdf.Save(pdfName); } } 

免责声明:我为图书馆的供应商工作。

另一个工作代码,试试吧

 public void ImagesToPdf(string[] imagepaths, string pdfpath) { iTextSharp.text.Rectangle pageSize = null; using (var srcImage = new Bitmap(imagepaths[0].ToString())) { pageSize = new iTextSharp.text.Rectangle(0, 0, srcImage.Width, srcImage.Height); } using (var ms = new MemoryStream()) { var document = new iTextSharp.text.Document(pageSize, 0, 0, 0, 0); iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms).SetFullCompression(); document.Open(); var image = iTextSharp.text.Image.GetInstance(imagepaths[0].ToString()); document.Add(image); document.Close(); File.WriteAllBytes(pdfpath+"cheque.pdf", ms.ToArray()); } } 

不确定您是否正在寻找免费/开源解决方案或考虑商业解决方案。 但是,如果您要包含商业解决方案,那么有一个名为EasyPDF SDK的工具包,它提供了一个API,用于将图像(以及许多其他文件类型)转换为PDF。 它支持C#,可以在这里找到:

  http://www.pdfonline.com/ 

C#代码如下所示:

  Printer oPrinter = new Printer(); ImagePrintJob oPrintJob = oPrinter.ImagePrintJob; oPrintJob.PrintOut(imageFile, pdfFile); 

为了完全透明,我应该声称我为EasyPDF SDK的制造商(因此我的手柄)工作,所以这个建议并非没有个人偏见:)但如果你有兴趣,请随时查看eval版本。 干杯!

许多差异工具在那里。 我使用的是PrimoPDF(免费) http://www.primopdf.com/你打印文件然后将其打印成pdf格式到你的驱动器上。 适用于Windows