Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

pdf

png

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
}
like image 527
Jim Avatar asked Dec 30 '10 16:12

Jim


1 Answers

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

like image 181
Nicolas Repiquet Avatar answered Sep 18 '22 12:09

Nicolas Repiquet