我想将图片(如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/

另一个有效的代码,试试吧

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());
        }
}

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

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

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);
    }
}

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

不确定您是在寻找免费/开源解决方案还是考虑商业解决方案。但是,如果您要包含商业解决方案,那么有一个名为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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top