.Net 对于PDF生成以及各种转换的操作

前段时间公司的产品,要做一个新功能,签章(就是把需要的数据整理成PDF很标准的文件,然后在盖上我们在服务器上面的章)

然后我就在百度上找了找,发现搞PDF的类库很少,要么就要钱,要么就有水印,破解版的没找到。

先讲一讲我是怎么生成PDF的

1、生成PDF

  这里用到了Spire.Pdf 这个类库可以在NuGet里面搜索到

2、之后就是关于PDF一些转换的操作了(PDF转base64,转图片之类的,我把代码贴到下面)

这个用到了iTextSharp类库,也可以在Nuget下载到

/// <summary>
  /// 图片转pdf
  /// </summary>
  /// <param name="jpgfile"></param>
  /// <param name="pdf"></param>
  public static bool ConvertJPG2PDF(string jpgfile, string pdf)
  {
   try
   {
    var document = new iTextSharp.text.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 = iTextSharp.text.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.NewPage();
      document.Add(image);
     }
     document.Close();
    }

    return true;

   }
   catch (Exception ex)
   {
    throw ex;
   }
  }

这个和上面用的也是同一个(我觉得这个比较好用一些)

/// <summary>
  /// pdf转img
  /// </summary>
  /// <param name="path">pdf位置</param>
  /// <param name="path2">img位置</param>
  public static void Pdf2Img(string path, string path2)
  {
   PDFFile pdfFile = PDFFile.Open(path);
   //实例化一个PdfDocument类对象,并加载PDF文档
   using (Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument())
   {
    doc.LoadFromFile(path);
    //调用方法SaveAsImage()将PDF第一页保存为Bmp格式
    System.Drawing.Image bmp = doc.SaveAsImage(0);
    // //调用另一个SaveAsImage()方法,并将指定页面保存保存为Emf、Png   
    System.Drawing.Image emf = doc.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Bitmap);
    System.Drawing.Image zoomImg = new Bitmap((int)(emf.Size.Width * 2), (int)(emf.Size.Height * 2));
    using (Graphics g = Graphics.FromImage(zoomImg))
    {
     g.ScaleTransform(2.0f, 2.0f);
     g.DrawImage(emf, new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), emf.Size), new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), emf.Size), GraphicsUnit.Pixel);
     zoomImg.Save(path2, ImageFormat.Jpeg);
     
     zoomImg.Dispose();
     emf.Dispose();
     bmp.Dispose();
    }
    doc.Close();
    doc.Dispose();
   }  
  }

我主要也就用到这些,之后在发现,我在往上加  

https://www.laike.net/dll/O2S.Components.PDFRender4NET.dll.html

  这个是O2S.Components.PDFRender4NET的文件,我看评论里有问的,之前忘记发了,网上有的是收费的,Nuget里面带后缀的我没用过。

到此这篇关于.Net 对于PDF生成以及各种转换的操作的文章就介绍到这了,更多相关.Net PDF生成内容请搜索来客网以前的文章或继续浏览下面的相关文章希望大家以后多多支持来客网!

本文作者:沉_默
本文链接:https://www.cnblogs.com/silentCM/p/13130333.html