26

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

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

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

4

9 回答 9

35

使用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();
        }
    }
}
于 2009-10-29T08:33:21.060 回答
19

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

于 2009-10-29T08:30:06.183 回答
8

另一个工作代码,试试看

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());
        }
}
于 2015-10-10T09:26:28.913 回答
5

我们非常幸运的一个是 PDFSharp(我们将它用于 TIFF 和文本到 PDF 的转换,每天有数百个医疗索赔)。

http://pdfsharp.com/PDFsharp/

于 2009-10-29T09:46:20.007 回答
3

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

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

于 2012-04-29T18:09:25.180 回答
1

您需要安装 Acrobat。在 Acrobat DC 上测试。这是一个 VB.net 代码。由于这些对象是 COM 对象,您应该执行“释放对象”,而不仅仅是“=Nothing”。您可以在此处转换此代码:https ://converter.telerik.com/

Private Function ImageToPDF(ByVal FilePath As String, ByVal DestinationFolder As String) As String
    Const PDSaveCollectGarbage  As Integer = 32
    Const PDSaveLinearized      As Integer = 4
    Const PDSaveFull            As Integer = 1
    Dim PDFAVDoc                As Object = Nothing
    Dim PDFDoc                  As Object = Nothing

    Try
        'Check destination requirements
        If Not DestinationFolder.EndsWith("\") Then DestinationFolder += "\"
        If Not System.IO.Directory.Exists(DestinationFolder) Then Throw New Exception("Destination directory does not exist: " & DestinationFolder)
        Dim CreatedFile As String = DestinationFolder & System.IO.Path.GetFileNameWithoutExtension(FilePath) & ".pdf"
        'Avoid conflicts, therefore previous file there will be deleted
        If File.Exists(CreatedFile) Then File.Delete(CreatedFile)

        'Get PDF document
        PDFAVDoc = GetPDFAVDoc(FilePath)
        PDFDoc = PDFAVDoc.GetPDDoc

        If Not PDFDoc.Save(PDSaveCollectGarbage Or PDSaveLinearized Or PDSaveFull, CreatedFile) Then Throw New Exception("PDF file cannot be saved: " & PDFDoc.GetFileName())
        If Not PDFDoc.Close() Then Throw New Exception("PDF file could not be closed: " & PDFDoc.GetFileName())
        PDFAVDoc.Close(1)
        Return CreatedFile
    Catch Ex As Exception
        Throw Ex
    Finally
        System.Runtime.InteropServices.Marshal.ReleaseComObject(PDFDoc)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(PDFDoc)
        PDFDoc = Nothing
        System.Runtime.InteropServices.Marshal.ReleaseComObject(PDFAVDoc)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(PDFAVDoc)
        PDFAVDoc = Nothing
        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
    End Try
End Function
于 2020-03-31T17:04:48.710 回答
0

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

 http://www.pdfonline.com/

C# 代码如下所示:

 Printer oPrinter = new Printer();

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

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

于 2009-11-02T18:23:34.977 回答
0

我使用 Sautinsoft,它非常简单:

SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();
p.Serial="xxx";
p.HtmlToPdfConvertStringToFile("<html><body><img src=\""+filename+"\"></img></body></html>","output.pdf");
于 2022-01-27T19:00:06.087 回答
-5

那里有许多差异工具。我使用的一个是 PrimoPDF (FREE) http://www.primopdf.com/ 你去打印文件然后你把它打印成 pdf 格式到你的驱动器上。适用于 Windows

于 2009-10-29T08:36:29.260 回答