国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#代碼實(shí)現(xiàn)PDF文檔操作類

2019-10-29 21:36:51
字體:
供稿:網(wǎng)友
本篇文章給大家介紹使用pdf文檔操作C#代碼,本文代碼非常簡(jiǎn)單,代碼附有注釋,需要注意的是:需要添加itextsharp.dll引用才可以正常通過編譯,感興趣的朋友可以參考下
 

本文純干貨,貼上PDF文檔操作類C#代碼,需要添加iTextSharp.dll引用才可以正常通過編譯。

廢話不多說了,直接給大家貼代碼了。

代碼如下:
 

  1. using System.IO; 
  2. using iTextSharp.text; 
  3. using iTextSharp.text.pdf; 
  4. namespace DotNet.Utilities 
  5.  /// <summary> 
  6.  /// PDF文檔操作類 
  7.  /// </summary> 
  8.  //------------------------------------調(diào)用-------------------------------------------- 
  9.  //PDFOperation pdf = new PDFOperation(); 
  10.  //pdf.Open(new FileStream(path, FileMode.Create)); 
  11.  //pdf.SetBaseFont(@"C:/Windows/Fonts/SIMHEI.TTF"); 
  12.  //pdf.AddParagraph("測(cè)試文檔(生成時(shí)間:" + DateTime.Now + ")", 15, 1, 20, 0, 0); 
  13.  //pdf.Close(); 
  14.  //------------------------------------------------------------------------------------- 
  15.  public class PDFOperation 
  16.  { 
  17.   #region 構(gòu)造函數(shù) 
  18.   /// <summary> 
  19.   /// 構(gòu)造函數(shù) 
  20.   /// </summary> 
  21.   public PDFOperation() 
  22.   { 
  23.    rect = PageSize.A4; 
  24.    document = new Document(rect); 
  25.   } 
  26.   /// <summary> 
  27.   /// 構(gòu)造函數(shù) 
  28.   /// </summary> 
  29.   /// <param name="type">頁面大小(如"A4")</param> 
  30.   public PDFOperation(string type) 
  31.   { 
  32.    SetPageSize(type); 
  33.    document = new Document(rect); 
  34.   } 
  35.   /// <summary> 
  36.   /// 構(gòu)造函數(shù) 
  37.   /// </summary> 
  38.   /// <param name="type">頁面大小(如"A4")</param> 
  39.   /// <param name="marginLeft">內(nèi)容距左邊框距離</param> 
  40.   /// <param name="marginRight">內(nèi)容距右邊框距離</param> 
  41.   /// <param name="marginTop">內(nèi)容距上邊框距離</param> 
  42.   /// <param name="marginBottom">內(nèi)容距下邊框距離</param> 
  43.   public PDFOperation(string type, float marginLeft, float marginRight, float marginTop, float marginBottom) 
  44.   { 
  45.    SetPageSize(type); 
  46.    document = new Document(rect, marginLeft, marginRight, marginTop, marginBottom); 
  47.   } 
  48.   #endregion 
  49.   #region 私有字段 
  50.   private Font font; 
  51.   private Rectangle rect; //文檔大小 
  52.   private Document document;//文檔對(duì)象 
  53.   private BaseFont basefont;//字體 
  54.   #endregion 
  55.   #region 設(shè)置字體 
  56.   /// <summary> 
  57.   /// 設(shè)置字體 
  58.   /// </summary> 
  59.   public void SetBaseFont(string path) 
  60.   { 
  61.    basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
  62.   } 
  63.   /// <summary> 
  64.   /// 設(shè)置字體 
  65.   /// </summary> 
  66.   /// <param name="size">字體大小</param> 
  67.   public void SetFont(float size) 
  68.   { 
  69.    font = new Font(basefont, size); 
  70.   } 
  71.   #endregion 
  72.   #region 設(shè)置頁面大小 
  73.   /// <summary> 
  74.   /// 設(shè)置頁面大小 
  75.   /// </summary> 
  76.   /// <param name="type">頁面大小(如"A4")</param> 
  77.   public void SetPageSize(string type) 
  78.   { 
  79.    switch (type.Trim()) 
  80.    { 
  81.     case "A4"
  82.      rect = PageSize.A4; 
  83.      break
  84.     case "A8"
  85.      rect = PageSize.A8; 
  86.      break
  87.    } 
  88.   } 
  89.   #endregion 
  90.   #region 實(shí)例化文檔 
  91.   /// <summary> 
  92.   /// 實(shí)例化文檔 
  93.   /// </summary> 
  94.   /// <param name="os">文檔相關(guān)信息(如路徑,打開方式等)</param> 
  95.   public void GetInstance(Stream os) 
  96.   { 
  97.    PdfWriter.GetInstance(document, os); 
  98.   } 
  99.   #endregion 
  100.   #region 打開文檔對(duì)象 
  101.   /// <summary> 
  102.   /// 打開文檔對(duì)象 
  103.   /// </summary> 
  104.   /// <param name="os">文檔相關(guān)信息(如路徑,打開方式等)</param> 
  105.   public void Open(Stream os) 
  106.   { 
  107.    GetInstance(os); 
  108.    document.Open(); 
  109.   } 
  110.   #endregion 
  111.   #region 關(guān)閉打開的文檔 
  112.   /// <summary> 
  113.   /// 關(guān)閉打開的文檔 
  114.   /// </summary> 
  115.   public void Close() 
  116.   { 
  117.    document.Close(); 
  118.   } 
  119.   #endregion 
  120.   #region 添加段落 
  121.   /// <summary> 
  122.   /// 添加段落 
  123.   /// </summary> 
  124.   /// <param name="content">內(nèi)容</param> 
  125.   /// <param name="fontsize">字體大小</param> 
  126.   public void AddParagraph(string content, float fontsize) 
  127.   { 
  128.    SetFont(fontsize); 
  129.    Paragraph pra = new Paragraph(content, font); 
  130.    document.Add(pra); 
  131.   } 
  132.   /// <summary> 
  133.   /// 添加段落 
  134.   /// </summary> 
  135.   /// <param name="content">內(nèi)容</param> 
  136.   /// <param name="fontsize">字體大小</param> 
  137.   /// <param name="Alignment">對(duì)齊方式(1為居中,0為居左,2為居右)</param> 
  138.   /// <param name="SpacingAfter">段后空行數(shù)(0為默認(rèn)值)</param> 
  139.   /// <param name="SpacingBefore">段前空行數(shù)(0為默認(rèn)值)</param> 
  140.   /// <param name="MultipliedLeading">行間距(0為默認(rèn)值)</param> 
  141.   public void AddParagraph(string content, float fontsize, int Alignment, float SpacingAfter, float SpacingBefore, float MultipliedLeading) 
  142.   { 
  143.    SetFont(fontsize); 
  144.    Paragraph pra = new Paragraph(content, font); 
  145.    pra.Alignment = Alignment; 
  146.    if (SpacingAfter != 0) 
  147.    { 
  148.     pra.SpacingAfter = SpacingAfter; 
  149.    } 
  150.    if (SpacingBefore != 0) 
  151.    { 
  152.     pra.SpacingBefore = SpacingBefore; 
  153.    } 
  154.    if (MultipliedLeading != 0) 
  155.    { 
  156.     pra.MultipliedLeading = MultipliedLeading; 
  157.    } 
  158.    document.Add(pra); 
  159.   } 
  160.   #endregion 
  161.   #region 添加圖片 
  162.   /// <summary> 
  163.   /// 添加圖片 
  164.   /// </summary> 
  165.   /// <param name="path">圖片路徑</param> 
  166.   /// <param name="Alignment">對(duì)齊方式(1為居中,0為居左,2為居右)</param> 
  167.   /// <param name="newWidth">圖片寬(0為默認(rèn)值,如果寬度大于頁寬將按比率縮放)</param> 
  168.   /// <param name="newHeight">圖片高</param> 
  169.   public void AddImage(string path, int Alignment, float newWidth, float newHeight) 
  170.   { 
  171.    Image img = Image.GetInstance(path); 
  172.    img.Alignment = Alignment; 
  173.    if (newWidth != 0) 
  174.    { 
  175.     img.ScaleAbsolute(newWidth, newHeight); 
  176.    } 
  177.    else 
  178.    { 
  179.     if (img.Width > PageSize.A4.Width) 
  180.     { 
  181.      img.ScaleAbsolute(rect.Width, img.Width * img.Height / rect.Height); 
  182.     } 
  183.    } 
  184.    document.Add(img); 
  185.   } 
  186.   #endregion 
  187.   #region 添加鏈接、點(diǎn) 
  188.   /// <summary> 
  189.   /// 添加鏈接 
  190.   /// </summary> 
  191.   /// <param name="Content">鏈接文字</param> 
  192.   /// <param name="FontSize">字體大小</param> 
  193.   /// <param name="Reference">鏈接地址</param> 
  194.   public void AddAnchorReference(string Content, float FontSize, string Reference) 
  195.   { 
  196.    SetFont(FontSize); 
  197.    Anchor auc = new Anchor(Content, font); 
  198.    auc.Reference = Reference; 
  199.    document.Add(auc); 
  200.   } 
  201.   /// <summary> 
  202.   /// 添加鏈接點(diǎn) 
  203.   /// </summary> 
  204.   /// <param name="Content">鏈接文字</param> 
  205.   /// <param name="FontSize">字體大小</param> 
  206.   /// <param name="Name">鏈接點(diǎn)名</param> 
  207.   public void AddAnchorName(string Content, float FontSize, string Name) 
  208.   { 
  209.    SetFont(FontSize); 
  210.    Anchor auc = new Anchor(Content, font); 
  211.    auc.Name = Name; 
  212.    document.Add(auc); 
  213.   } 
  214.   #endregion 
  215.  } 
?

腳本之家友情提醒需要注意點(diǎn):需要添加iTextSharp.dll引用才可以正常通過編譯。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 寿宁县| 商城县| 涟水县| 北川| 普兰店市| 区。| 南开区| 年辖:市辖区| 海南省| 阜阳市| 金阳县| 巧家县| 六盘水市| 英吉沙县| 红原县| 溆浦县| 阿巴嘎旗| 黄大仙区| 博罗县| 静安区| 麻江县| 昆山市| 重庆市| 会理县| 外汇| 仙桃市| 九寨沟县| 新源县| 门源| 理塘县| 汕尾市| 克什克腾旗| 龙岩市| 杭州市| 铁岭市| 环江| 香港 | 岑溪市| 松溪县| 湄潭县| 海兴县|