JPG to PDF Convertor in C#

后端 未结 8 788
鱼传尺愫
鱼传尺愫 2020-12-01 14:07

I would like to convert from an image (like jpg or png) to PDF.

I\'ve checked out ImageMagickNET, but it is far too complex for my needs.

What other .NET so

相关标签:
8条回答
  • 2020-12-01 14:38

    iTextSharp does it pretty cleanly and is open source. Also, it has a very good accompanying book by the author which I recommend if you end up doing more interesting things like managing forms. For normal usage, there are plenty resources on mailing lists and newsgroups for samples of how to do common things.

    EDIT: as alluded to in @Chirag's comment, @Darin's answer has code that definitely compiles with current versions.

    Example usage:

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

    0 讨论(0)
  • not sure if you're looking for just free / open source solutions or considering commercial ones as well. But if you're including commercial solutions, there's a toolkit called EasyPDF SDK that offers an API for converting images (plus a number of other file types) to PDF. It supports C# and can be found here:

     http://www.pdfonline.com/
    

    The C# code would look as follows:

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

    To be fully transparent, I should disclaim that I do work for the makers of EasyPDF SDK (hence my handle), so this suggestion is not without some personal bias :) But feel free to check out the eval version if you're interested. Cheers!

    0 讨论(0)
  • 2020-12-01 14:43

    One we've had great luck with is PDFSharp (we use it for TIFF and Text to PDF conversion for hundreds of medical claims every day).

    http://pdfsharp.com/PDFsharp/

    0 讨论(0)
  • 2020-12-01 14:46

    Easy with 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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 14:46

    Many diff tools out there. One I use is PrimoPDF (FREE) http://www.primopdf.com/ you go to print the file and you print it to pdf format onto your drive. works on Windows

    0 讨论(0)
  • 2020-12-01 14:50

    Such task can be easily done with help of Docotic.Pdf library.

    Here is a sample that creates PDF from given images (not only JPGs, actually):

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

    Disclaimer: I work for the vendor of the library.

    0 讨论(0)
提交回复
热议问题