[C#] 一个C#通过iTextSharp封装的PDF文件操作类代码 →→→→→进入此内容的聊天室

来自 , 2020-11-17, 写在 C#, 查看 111 次.
URL http://www.code666.cn/view/2cfa8f9e
  1. using System.IO;
  2. using iTextSharp.text;
  3. using iTextSharp.text.pdf;
  4.  
  5. namespace DotNet.Utilities
  6. {
  7.     /// <summary>
  8.     /// PDF文档操作类
  9.     /// </summary>
  10.     //------------------------------------调用--------------------------------------------
  11.     //PDFOperation pdf = new PDFOperation();
  12.     //pdf.Open(new FileStream(path, FileMode.Create));
  13.     //pdf.SetBaseFont(@"C:\Windows\Fonts\SIMHEI.TTF");
  14.     //pdf.AddParagraph("测试文档(生成时间:" + DateTime.Now + ")", 15, 1, 20, 0, 0);
  15.     //pdf.Close();
  16.     //-------------------------------------------------------------------------------------
  17.     public class PDFOperation
  18.     {
  19.         #region 构造函数
  20.         /// <summary>
  21.         /// 构造函数
  22.         /// </summary>
  23.         public PDFOperation()
  24.         {
  25.             rect = PageSize.A4;
  26.             document = new Document(rect);
  27.         }
  28.  
  29.         /// <summary>
  30.         /// 构造函数
  31.         /// </summary>
  32.         /// <param name="type">页面大小(如"A4")</param>
  33.         public PDFOperation(string type)
  34.         {
  35.             SetPageSize(type);
  36.             document = new Document(rect);
  37.         }
  38.  
  39.         /// <summary>
  40.         /// 构造函数
  41.         /// </summary>
  42.         /// <param name="type">页面大小(如"A4")</param>
  43.         /// <param name="marginLeft">内容距左边框距离</param>
  44.         /// <param name="marginRight">内容距右边框距离</param>
  45.         /// <param name="marginTop">内容距上边框距离</param>
  46.         /// <param name="marginBottom">内容距下边框距离</param>
  47.         public PDFOperation(string type, float marginLeft, float marginRight, float marginTop, float marginBottom)
  48.         {
  49.             SetPageSize(type);
  50.             document = new Document(rect, marginLeft, marginRight, marginTop, marginBottom);
  51.         }
  52.         #endregion
  53.  
  54.         #region 私有字段
  55.         private Font font;
  56.         private Rectangle rect;   //文档大小
  57.         private Document document;//文档对象
  58.         private BaseFont basefont;//字体
  59.         #endregion
  60.  
  61.         #region 设置字体
  62.         /// <summary>
  63.         /// 设置字体
  64.         /// </summary>
  65.         public void SetBaseFont(string path)
  66.         {
  67.             basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  68.         }
  69.  
  70.         /// <summary>
  71.         /// 设置字体
  72.         /// </summary>
  73.         /// <param name="size">字体大小</param>
  74.         public void SetFont(float size)
  75.         {
  76.             font = new Font(basefont, size);
  77.         }
  78.         #endregion
  79.  
  80.         #region 设置页面大小
  81.         /// <summary>
  82.         /// 设置页面大小
  83.         /// </summary>
  84.         /// <param name="type">页面大小(如"A4")</param>
  85.         public void SetPageSize(string type)
  86.         {
  87.             switch (type.Trim())
  88.             {
  89.                 case "A4":
  90.                     rect = PageSize.A4;
  91.                     break;
  92.                 case "A8":
  93.                     rect = PageSize.A8;
  94.                     break;
  95.             }
  96.         }
  97.         #endregion
  98.  
  99.         #region 实例化文档
  100.         /// <summary>
  101.         /// 实例化文档
  102.         /// </summary>
  103.         /// <param name="os">文档相关信息(如路径,打开方式等)</param>
  104.         public void GetInstance(Stream os)
  105.         {
  106.             PdfWriter.GetInstance(document, os);
  107.         }
  108.         #endregion
  109.  
  110.         #region 打开文档对象
  111.         /// <summary>
  112.         /// 打开文档对象
  113.         /// </summary>
  114.         /// <param name="os">文档相关信息(如路径,打开方式等)</param>
  115.         public void Open(Stream os)
  116.         {
  117.             GetInstance(os);
  118.             document.Open();
  119.         }
  120.         #endregion
  121.  
  122.         #region 关闭打开的文档
  123.         /// <summary>
  124.         /// 关闭打开的文档
  125.         /// </summary>
  126.         public void Close()
  127.         {
  128.             document.Close();
  129.         }
  130.         #endregion
  131.  
  132.         #region 添加段落
  133.         /// <summary>
  134.         /// 添加段落
  135.         /// </summary>
  136.         /// <param name="content">内容</param>
  137.         /// <param name="fontsize">字体大小</param>
  138.         public void AddParagraph(string content, float fontsize)
  139.         {
  140.             SetFont(fontsize);
  141.             Paragraph pra = new Paragraph(content, font);
  142.             document.Add(pra);
  143.         }
  144.  
  145.         /// <summary>
  146.         /// 添加段落
  147.         /// </summary>
  148.         /// <param name="content">内容</param>
  149.         /// <param name="fontsize">字体大小</param>
  150.         /// <param name="Alignment">对齐方式(1为居中,0为居左,2为居右)</param>
  151.         /// <param name="SpacingAfter">段后空行数(0为默认值)</param>
  152.         /// <param name="SpacingBefore">段前空行数(0为默认值)</param>
  153.         /// <param name="MultipliedLeading">行间距(0为默认值)</param>
  154.         public void AddParagraph(string content, float fontsize, int Alignment, float SpacingAfter, float SpacingBefore, float MultipliedLeading)
  155.         {
  156.             SetFont(fontsize);
  157.             Paragraph pra = new Paragraph(content, font);
  158.             pra.Alignment = Alignment;
  159.             if (SpacingAfter != 0)
  160.             {
  161.                 pra.SpacingAfter = SpacingAfter;
  162.             }
  163.             if (SpacingBefore != 0)
  164.             {
  165.                 pra.SpacingBefore = SpacingBefore;
  166.             }
  167.             if (MultipliedLeading != 0)
  168.             {
  169.                 pra.MultipliedLeading = MultipliedLeading;
  170.             }
  171.             document.Add(pra);
  172.         }
  173.         #endregion
  174.  
  175.         #region 添加图片
  176.         /// <summary>
  177.         /// 添加图片
  178.         /// </summary>
  179.         /// <param name="path">图片路径</param>
  180.         /// <param name="Alignment">对齐方式(1为居中,0为居左,2为居右)</param>
  181.         /// <param name="newWidth">图片宽(0为默认值,如果宽度大于页宽将按比率缩放)</param>
  182.         /// <param name="newHeight">图片高</param>
  183.         public void AddImage(string path, int Alignment, float newWidth, float newHeight)
  184.         {
  185.             Image img = Image.GetInstance(path);
  186.             img.Alignment = Alignment;
  187.             if (newWidth != 0)
  188.             {
  189.                 img.ScaleAbsolute(newWidth, newHeight);
  190.             }
  191.             else
  192.             {
  193.                 if (img.Width > PageSize.A4.Width)
  194.                 {
  195.                     img.ScaleAbsolute(rect.Width, img.Width * img.Height / rect.Height);
  196.                 }
  197.             }
  198.             document.Add(img);
  199.         }
  200.         #endregion
  201.  
  202.         #region 添加链接、点
  203.         /// <summary>
  204.         /// 添加链接
  205.         /// </summary>
  206.         /// <param name="Content">链接文字</param>
  207.         /// <param name="FontSize">字体大小</param>
  208.         /// <param name="Reference">链接地址</param>
  209.         public void AddAnchorReference(string Content, float FontSize, string Reference)
  210.         {
  211.             SetFont(FontSize);
  212.             Anchor auc = new Anchor(Content, font);
  213.             auc.Reference = Reference;
  214.             document.Add(auc);
  215.         }
  216.  
  217.         /// <summary>
  218.         /// 添加链接点
  219.         /// </summary>
  220.         /// <param name="Content">链接文字</param>
  221.         /// <param name="FontSize">字体大小</param>
  222.         /// <param name="Name">链接点名</param>
  223.         public void AddAnchorName(string Content, float FontSize, string Name)
  224.         {
  225.             SetFont(FontSize);
  226.             Anchor auc = new Anchor(Content, font);
  227.             auc.Name = Name;
  228.             document.Add(auc);
  229.         }
  230.         #endregion
  231.     }
  232. }
  233. //csharp/8619

回复 "一个C#通过iTextSharp封装的PDF文件操作类代码"

这儿你可以回复上面这条便签

captcha