JPG to PDF Convertor in C#

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

    Another working code, try it

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

提交回复
热议问题