Is there a .NET library that can convert PNG files to PDF?

送分小仙女□ 提交于 2019-12-23 10:10:56

问题


I have an application where I need to convert PNG files PDF on the fly. Is there an existing library that will do this? I would like the PDF to look exactly like the PNG--no extra margins, no borders, etc.

I'm using .NET 4.0.

EDIT: I tried iTextSharp and it worked great. Here's the basic code to get what I needed.

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

public class ITextPDFHelper
{
    public static void Main(string[] args)
    {
        ITextPDFHelper.CreatePDF("C:\\temp\test.pdf", "C:\\temp\test.png");
    }

    public static void CreatePDF(string fileToCreate, pngFileName)
    {
        Document doc = new Document();
        PdfWriter.GetInstance(doc, new FileStream(fileToCreate, FileMode.Create));
        doc.Open();
        Image png = Image.GetInstance(pngFileName);
        png.SetAbsolutePosition(0, 0);
        doc.Add(png);
        doc.Close();
    }//CreatePDF
}

回答1:


You mean, a pdf document containing a single page with your picture in it? Take a look at ITextSharp




回答2:


ImageMagick is how I would usually do something like this...so check out ImageMagick.NET.

I'm not familiar with ImageMagick.NET's syntax, but with ImageMagick, this is the command you would use:

convert source.png dest.pdf

As you can see, it's relatively simple.




回答3:


For various PDF operations, I am using Aspose.Pdf and related products like Aspose.Pdf.Kit.

I think it is not the most direct solution but it should be usable to achieve your desired result.



来源:https://stackoverflow.com/questions/4564260/is-there-a-net-library-that-can-convert-png-files-to-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!