首页 > 编程 > C# > 正文

C#代码实现PDF文档操作类

2019-10-29 21:36:51
字体:
来源:转载
供稿:网友
本篇文章给大家介绍使用pdf文档操作C#代码,本文代码非常简单,代码附有注释,需要注意的是:需要添加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.  //------------------------------------调用-------------------------------------------- 
  9.  //PDFOperation pdf = new PDFOperation(); 
  10.  //pdf.Open(new FileStream(path, FileMode.Create)); 
  11.  //pdf.SetBaseFont(@"C:/Windows/Fonts/SIMHEI.TTF"); 
  12.  //pdf.AddParagraph("测试文档(生成时间:" + DateTime.Now + ")", 15, 1, 20, 0, 0); 
  13.  //pdf.Close(); 
  14.  //------------------------------------------------------------------------------------- 
  15.  public class PDFOperation 
  16.  { 
  17.   #region 构造函数 
  18.   /// <summary> 
  19.   /// 构造函数 
  20.   /// </summary> 
  21.   public PDFOperation() 
  22.   { 
  23.    rect = PageSize.A4; 
  24.    document = new Document(rect); 
  25.   } 
  26.   /// <summary> 
  27.   /// 构造函数 
  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.   /// 构造函数 
  37.   /// </summary> 
  38.   /// <param name="type">页面大小(如"A4")</param> 
  39.   /// <param name="marginLeft">内容距左边框距离</param> 
  40.   /// <param name="marginRight">内容距右边框距离</param> 
  41.   /// <param name="marginTop">内容距上边框距离</param> 
  42.   /// <param name="marginBottom">内容距下边框距离</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;//文档对象 
  53.   private BaseFont basefont;//字体 
  54.   #endregion 
  55.   #region 设置字体 
  56.   /// <summary> 
  57.   /// 设置字体 
  58.   /// </summary> 
  59.   public void SetBaseFont(string path) 
  60.   { 
  61.    basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
  62.   } 
  63.   /// <summary> 
  64.   /// 设置字体 
  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 设置页面大小 
  73.   /// <summary> 
  74.   /// 设置页面大小 
  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 实例化文档 
  91.   /// <summary> 
  92.   /// 实例化文档 
  93.   /// </summary> 
  94.   /// <param name="os">文档相关信息(如路径,打开方式等)</param> 
  95.   public void GetInstance(Stream os) 
  96.   { 
  97.    PdfWriter.GetInstance(document, os); 
  98.   } 
  99.   #endregion 
  100.   #region 打开文档对象 
  101.   /// <summary> 
  102.   /// 打开文档对象 
  103.   /// </summary> 
  104.   /// <param name="os">文档相关信息(如路径,打开方式等)</param> 
  105.   public void Open(Stream os) 
  106.   { 
  107.    GetInstance(os); 
  108.    document.Open(); 
  109.   } 
  110.   #endregion 
  111.   #region 关闭打开的文档 
  112.   /// <summary> 
  113.   /// 关闭打开的文档 
  114.   /// </summary> 
  115.   public void Close() 
  116.   { 
  117.    document.Close(); 
  118.   } 
  119.   #endregion 
  120.   #region 添加段落 
  121.   /// <summary> 
  122.   /// 添加段落 
  123.   /// </summary> 
  124.   /// <param name="content">内容</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">内容</param> 
  136.   /// <param name="fontsize">字体大小</param> 
  137.   /// <param name="Alignment">对齐方式(1为居中,0为居左,2为居右)</param> 
  138.   /// <param name="SpacingAfter">段后空行数(0为默认值)</param> 
  139.   /// <param name="SpacingBefore">段前空行数(0为默认值)</param> 
  140.   /// <param name="MultipliedLeading">行间距(0为默认值)</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">对齐方式(1为居中,0为居左,2为居右)</param> 
  167.   /// <param name="newWidth">图片宽(0为默认值,如果宽度大于页宽将按比率缩放)</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 添加链接、点 
  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.   /// 添加链接点 
  203.   /// </summary> 
  204.   /// <param name="Content">链接文字</param> 
  205.   /// <param name="FontSize">字体大小</param> 
  206.   /// <param name="Name">链接点名</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.  } 
?

脚本之家友情提醒需要注意点:需要添加iTextSharp.dll引用才可以正常通过编译。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表