欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.Net 對(duì)于PDF生成以及各種轉(zhuǎn)換的操作

 更新時(shí)間:2020年06月16日 14:15:38   作者:沉_默  
這篇文章主要介紹了.Net 對(duì)于PDF生成以及各種轉(zhuǎn)換的操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前段時(shí)間公司的產(chǎn)品,要做一個(gè)新功能,簽章(就是把需要的數(shù)據(jù)整理成PDF很標(biāo)準(zhǔn)的文件,然后在蓋上我們?cè)诜?wù)器上面的章)

然后我就在百度上找了找,發(fā)現(xiàn)搞PDF的類庫(kù)很少,要么就要錢,要么就有水印,破解版的沒(méi)找到。

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

1、生成PDF

  這里用到了Spire.Pdf 這個(gè)類庫(kù)可以在NuGet里面搜索到,上面帶個(gè)小紅標(biāo)的就是免費(fèi)版本?! ?/p>

  當(dāng)然也可以去他們的官網(wǎng),上面還有文檔(https://www.e-iceblue.cn/Introduce/Spire-PDF-NET.html)。

  代碼(這是我自己寫(xiě)的一個(gè)測(cè)試的表格)

 public static void abc()
  {
   //創(chuàng)建PDF文檔
   Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();

   //添加一頁(yè)
   PdfPageBase page = doc.Pages.Add();
    //設(shè)置字體
   PdfTrueTypeFont font = new PdfTrueTypeFont(new System.Drawing.Font("Microsoft Yahei", 20f), true);
   PdfTrueTypeFont font1 = new PdfTrueTypeFont(new System.Drawing.Font("Microsoft Yahei", 11f), true);

   //創(chuàng)建一個(gè)PdfGrid對(duì)象
   PdfGrid grid = new PdfGrid();

        //這一段的內(nèi)容是在表格只玩顯示一些數(shù)據(jù) 根據(jù)坐標(biāo)定位 第一個(gè)是內(nèi)容,第二個(gè)是字體,第三個(gè)是顏色,第四第五是坐標(biāo)
   page.Canvas.DrawString("XXXXXXXX管理中心回單",
     font,
     new PdfSolidBrush(System.Drawing.Color.Black), 130, 10);
   page.Canvas.DrawString("編號(hào):31231",
     font1,
     new PdfSolidBrush(System.Drawing.Color.Black), 380, 60);
   page.Canvas.DrawString("經(jīng)辦人:XXXX",
     font1,
     new PdfSolidBrush(System.Drawing.Color.Black), 60, 250);
   page.Canvas.DrawString("打印日期:2020/06/15",
     font1,
     new PdfSolidBrush(System.Drawing.Color.Black), 380, 250);
   //設(shè)置單元格邊距
   grid.Style.CellPadding = new PdfPaddings(1, 1, 4, 4);

   //設(shè)置表格默認(rèn)字體
   grid.Style.Font = new PdfTrueTypeFont(new System.Drawing.Font("Microsoft Yahei", 12f), true);

   //添加4行4列
   PdfGridRow row1 = grid.Rows.Add();
   PdfGridRow row2 = grid.Rows.Add();
   PdfGridRow row3 = grid.Rows.Add();
   PdfGridRow row4 = grid.Rows.Add();
   PdfGridRow row5 = grid.Rows.Add();
   PdfGridRow row6 = grid.Rows.Add();
   grid.Columns.Add(4);

   //設(shè)置列寬
   foreach (PdfGridColumn col in grid.Columns)
   {
    col.Width = 120f;
   }

   //寫(xiě)入數(shù)據(jù) 第一行第一個(gè)格式的值,第一行第二個(gè)格子的值 
   row1.Cells[0].Value = "收款單位";
   row1.Cells[1].Value = "{DW}";
   row2.Cells[0].Value = "收款單位";
   row2.Cells[1].Value = "{DW}";
   row3.Cells[0].Value = "匯款時(shí)間";
   row3.Cells[1].Value = "2016/06/02";
   row3.Cells[2].Value = "金額小寫(xiě)";
   row3.Cells[3].Value = "¥231";
   row4.Cells[0].Value = "金額合計(jì)大寫(xiě)";
   row4.Cells[1].Value = "大蘇打?qū)嵈驅(qū)?;
   row5.Cells[0].Value = "用途:" +
    "付XXXX2020年04月至2020年04月";
   row6.Cells[0].Value = "提示:回單可重復(fù)打印,請(qǐng)勿重復(fù)XXX";

   //row5.Cells[0].Height = (float)20;
   //水平和垂直合并單元格 從那個(gè)格子開(kāi)始合并幾個(gè)(包含當(dāng)前格子)
   row1.Cells[1].ColumnSpan = 3;
   row2.Cells[1].ColumnSpan = 3;
   row4.Cells[1].ColumnSpan = 3;
   row5.Cells[0].ColumnSpan = 2;
   row5.Cells[2].ColumnSpan = 2;
   row6.Cells[0].ColumnSpan = 2;
   row6.Cells[2].ColumnSpan = 2;
        //這個(gè)是垂直合并,但是我之前合并的沒(méi)有效果
   row5.Cells[1].RowSpan = 2;

   //設(shè)置單元格內(nèi)文字對(duì)齊方式
   row1.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row2.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row2.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row3.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row3.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row3.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row3.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row4.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row4.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row5.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   row6.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   //設(shè)置單元格背景顏色
   //row1.Cells[0].Style.BackgroundBrush = PdfBrushes.Gray;
   //row3.Cells[3].Style.BackgroundBrush = PdfBrushes.Green;
   //row4.Cells[3].Style.BackgroundBrush = PdfBrushes.MediumVioletRed;

   //設(shè)置邊框顏色、粗細(xì)
   PdfBorders borders = new PdfBorders();
   borders.All = new PdfPen(System.Drawing.Color.Black, 0.1f);
   foreach (PdfGridRow pgr in grid.Rows)
   {
    foreach (PdfGridCell pgc in pgr.Cells)
    {
     pgc.Style.Borders = borders;
    }
   }
   //保存到文檔
   //在指定為繪入表格
   grid.Draw(page, new PointF(30, 80));
   doc.SaveToFile(@"路徑");
       //這句是在瀏覽器重打開(kāi)這個(gè)PDF  
   System.Diagnostics.Process.Start(@"路徑");
  }

  保存我們看一下

  

2、之后就是關(guān)于PDF一些轉(zhuǎn)換的操作了(PDF轉(zhuǎn)base64,轉(zhuǎn)圖片之類的,我把代碼貼到下面)

這個(gè)用到了iTextSharp類庫(kù),也可以在Nuget下載到

/// <summary>
  /// 圖片轉(zhuǎn)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;
   }
  }

這個(gè)和上面用的也是同一個(gè)(我覺(jué)得這個(gè)比較好用一些)

/// <summary>
  /// pdf轉(zhuǎn)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);
   //實(shí)例化一個(gè)PdfDocument類對(duì)象,并加載PDF文檔
   using (Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument())
   {
    doc.LoadFromFile(path);
    //調(diào)用方法SaveAsImage()將PDF第一頁(yè)保存為Bmp格式
    System.Drawing.Image bmp = doc.SaveAsImage(0);
    // //調(diào)用另一個(gè)SaveAsImage()方法,并將指定頁(yè)面保存保存為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();
   }  
  }

我主要也就用到這些,之后在發(fā)現(xiàn),我在往上加  

http://www.dbjr.com.cn/dll/O2S.Components.PDFRender4NET.dll.html

  這個(gè)是O2S.Components.PDFRender4NET的文件,我看評(píng)論里有問(wèn)的,之前忘記發(fā)了,網(wǎng)上有的是收費(fèi)的,Nuget里面帶后綴的我沒(méi)用過(guò)。

到此這篇關(guān)于.Net 對(duì)于PDF生成以及各種轉(zhuǎn)換的操作的文章就介紹到這了,更多相關(guān).Net PDF生成內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

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

相關(guān)文章

最新評(píng)論