C's method of converting jpg to pdf

  • 2020-12-05 17:20:00
  • OfStack

This article shows an example of how C# converts jpg to pdf. Share to everybody for everybody reference. Specific implementation methods are as follows:

C# generates pdf files using an ES8en.dll file. itextsharp.dll is an open source library file used in C# to generate PDF documents. Here's how to generate itextsharp files.

introduce

itextsharp. dll is an open source library used in C# to generate PDF documents. Many C# fans have made PDF document generators with it

The specific code is as follows:

void ConvertJPG2PDF(string jpgfile, string pdf)
{
 var document = new Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
 using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None))
 {
  PdfWriter.GetInstance(document, stream);
  document.Open();
  using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  {
   var image = Image.GetInstance(imageStream);
   if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
   {
    image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
   }
   else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
   {
    image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
   }
   image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
   document.Add(image);
  }
 
  document.Close();
 }
}

iTextSharp sure enough, X, a few lines of code to do.

Hopefully this article has helped you with your C# programming.


Related articles: