程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#代碼完成PDF文檔操作類

C#代碼完成PDF文檔操作類

編輯:C#入門知識

C#代碼完成PDF文檔操作類。本站提示廣大學習愛好者:(C#代碼完成PDF文檔操作類)文章只能為提供參考,不一定能成為您想要的結果。以下是C#代碼完成PDF文檔操作類正文


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

空話不多說了,直接給年夜家貼代碼了。

代碼以下:

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace DotNet.Utilities
{
 /// <summary>
 /// PDF文檔操作類
 /// </summary>
 //------------------------------------挪用--------------------------------------------
 //PDFOperation pdf = new PDFOperation();
 //pdf.Open(new FileStream(path, FileMode.Create));
 //pdf.SetBaseFont(@"C:\Windows\Fonts\SIMHEI.TTF");
 //pdf.AddParagraph("測試文檔(生成時光:" + DateTime.Now + ")", 15, 1, 20, 0, 0);
 //pdf.Close();
 //-------------------------------------------------------------------------------------
 public class PDFOperation
 {
  #region 結構函數
  /// <summary>
  /// 結構函數
  /// </summary>
  public PDFOperation()
  {
   rect = PageSize.A4;
   document = new Document(rect);
  }
  /// <summary>
  /// 結構函數
  /// </summary>
  /// <param name="type">頁面年夜小(如"A4")</param>
  public PDFOperation(string type)
  {
   SetPageSize(type);
   document = new Document(rect);
  }
  /// <summary>
  /// 結構函數
  /// </summary>
  /// <param name="type">頁面年夜小(如"A4")</param>
  /// <param name="marginLeft">內容距右邊框間隔</param>
  /// <param name="marginRight">內容距左邊框間隔</param>
  /// <param name="marginTop">內容距上邊框間隔</param>
  /// <param name="marginBottom">內容距下邊框間隔</param>
  public PDFOperation(string type, float marginLeft, float marginRight, float marginTop, float marginBottom)
  {
   SetPageSize(type);
   document = new Document(rect, marginLeft, marginRight, marginTop, marginBottom);
  }
  #endregion
  #region 公有字段
  private Font font;
  private Rectangle rect; //文檔年夜小
  private Document document;//文檔對象
  private BaseFont basefont;//字體
  #endregion
  #region 設置字體
  /// <summary>
  /// 設置字體
  /// </summary>
  public void SetBaseFont(string path)
  {
   basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  }
  /// <summary>
  /// 設置字體
  /// </summary>
  /// <param name="size">字體年夜小</param>
  public void SetFont(float size)
  {
   font = new Font(basefont, size);
  }
  #endregion
  #region 設置頁面年夜小
  /// <summary>
  /// 設置頁面年夜小
  /// </summary>
  /// <param name="type">頁面年夜小(如"A4")</param>
  public void SetPageSize(string type)
  {
   switch (type.Trim())
   {
    case "A4":
     rect = PageSize.A4;
     break;
    case "A8":
     rect = PageSize.A8;
     break;
   }
  }
  #endregion
  #region 實例化文檔
  /// <summary>
  /// 實例化文檔
  /// </summary>
  /// <param name="os">文檔相干信息(如途徑,翻開方法等)</param>
  public void GetInstance(Stream os)
  {
   PdfWriter.GetInstance(document, os);
  }
  #endregion
  #region 翻開文檔對象
  /// <summary>
  /// 翻開文檔對象
  /// </summary>
  /// <param name="os">文檔相干信息(如途徑,翻開方法等)</param>
  public void Open(Stream os)
  {
   GetInstance(os);
   document.Open();
  }
  #endregion
  #region 封閉翻開的文檔
  /// <summary>
  /// 封閉翻開的文檔
  /// </summary>
  public void Close()
  {
   document.Close();
  }
  #endregion
  #region 添加段落
  /// <summary>
  /// 添加段落
  /// </summary>
  /// <param name="content">內容</param>
  /// <param name="fontsize">字體年夜小</param>
  public void AddParagraph(string content, float fontsize)
  {
   SetFont(fontsize);
   Paragraph pra = new Paragraph(content, font);
   document.Add(pra);
  }
  /// <summary>
  /// 添加段落
  /// </summary>
  /// <param name="content">內容</param>
  /// <param name="fontsize">字體年夜小</param>
  /// <param name="Alignment">對齊方法(1為居中,0為居左,2為居右)</param>
  /// <param name="SpacingAfter">段後空行數(0為默許值)</param>
  /// <param name="SpacingBefore">段前空行數(0為默許值)</param>
  /// <param name="MultipliedLeading">行間距(0為默許值)</param>
  public void AddParagraph(string content, float fontsize, int Alignment, float SpacingAfter, float SpacingBefore, float MultipliedLeading)
  {
   SetFont(fontsize);
   Paragraph pra = new Paragraph(content, font);
   pra.Alignment = Alignment;
   if (SpacingAfter != 0)
   {
    pra.SpacingAfter = SpacingAfter;
   }
   if (SpacingBefore != 0)
   {
    pra.SpacingBefore = SpacingBefore;
   }
   if (MultipliedLeading != 0)
   {
    pra.MultipliedLeading = MultipliedLeading;
   }
   document.Add(pra);
  }
  #endregion
  #region 添加圖片
  /// <summary>
  /// 添加圖片
  /// </summary>
  /// <param name="path">圖片途徑</param>
  /// <param name="Alignment">對齊方法(1為居中,0為居左,2為居右)</param>
  /// <param name="newWidth">圖片寬(0為默許值,假如寬度年夜於頁寬將按比率縮放)</param>
  /// <param name="newHeight">圖片高</param>
  public void AddImage(string path, int Alignment, float newWidth, float newHeight)
  {
   Image img = Image.GetInstance(path);
   img.Alignment = Alignment;
   if (newWidth != 0)
   {
    img.ScaleAbsolute(newWidth, newHeight);
   }
   else
   {
    if (img.Width > PageSize.A4.Width)
    {
     img.ScaleAbsolute(rect.Width, img.Width * img.Height / rect.Height);
    }
   }
   document.Add(img);
  }
  #endregion
  #region 添加鏈接、點
  /// <summary>
  /// 添加鏈接
  /// </summary>
  /// <param name="Content">鏈接文字</param>
  /// <param name="FontSize">字體年夜小</param>
  /// <param name="Reference">鏈接地址</param>
  public void AddAnchorReference(string Content, float FontSize, string Reference)
  {
   SetFont(FontSize);
   Anchor auc = new Anchor(Content, font);
   auc.Reference = Reference;
   document.Add(auc);
  }
  /// <summary>
  /// 添加鏈接點
  /// </summary>
  /// <param name="Content">鏈接文字</param>
  /// <param name="FontSize">字體年夜小</param>
  /// <param name="Name">鏈接點名</param>
  public void AddAnchorName(string Content, float FontSize, string Name)
  {
   SetFont(FontSize);
   Anchor auc = new Anchor(Content, font);
   auc.Name = Name;
   document.Add(auc);
  }
  #endregion
 }
}

友誼提示須要留意點:須要添加iTextSharp.dll援用才可以正常經由過程編譯。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved