JPG to PDF Convertor in C#

后端 未结 8 984
鱼传尺愫
鱼传尺愫 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: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.

提交回复
热议问题