本文純干貨,貼上PDF文檔操作類C#代碼,需要添加iTextSharp.dll引用才可以正常通過編譯。
廢話不多說了,直接給大家貼代碼了。
代碼如下:
- using System.IO;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- namespace DotNet.Utilities
- {
- /// <summary>
- /// PDF文檔操作類
- /// </summary>
- //------------------------------------調(diào)用--------------------------------------------
- //PDFOperation pdf = new PDFOperation();
- //pdf.Open(new FileStream(path, FileMode.Create));
- //pdf.SetBaseFont(@"C:/Windows/Fonts/SIMHEI.TTF");
- //pdf.AddParagraph("測(cè)試文檔(生成時(shí)間:" + DateTime.Now + ")", 15, 1, 20, 0, 0);
- //pdf.Close();
- //-------------------------------------------------------------------------------------
- public class PDFOperation
- {
- #region 構(gòu)造函數(shù)
- /// <summary>
- /// 構(gòu)造函數(shù)
- /// </summary>
- public PDFOperation()
- {
- rect = PageSize.A4;
- document = new Document(rect);
- }
- /// <summary>
- /// 構(gòu)造函數(shù)
- /// </summary>
- /// <param name="type">頁面大小(如"A4")</param>
- public PDFOperation(string type)
- {
- SetPageSize(type);
- document = new Document(rect);
- }
- /// <summary>
- /// 構(gòu)造函數(shù)
- /// </summary>
- /// <param name="type">頁面大小(如"A4")</param>
- /// <param name="marginLeft">內(nèi)容距左邊框距離</param>
- /// <param name="marginRight">內(nèi)容距右邊框距離</param>
- /// <param name="marginTop">內(nèi)容距上邊框距離</param>
- /// <param name="marginBottom">內(nèi)容距下邊框距離</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;//文檔對(duì)象
- private BaseFont basefont;//字體
- #endregion
- #region 設(shè)置字體
- /// <summary>
- /// 設(shè)置字體
- /// </summary>
- public void SetBaseFont(string path)
- {
- basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
- }
- /// <summary>
- /// 設(shè)置字體
- /// </summary>
- /// <param name="size">字體大小</param>
- public void SetFont(float size)
- {
- font = new Font(basefont, size);
- }
- #endregion
- #region 設(shè)置頁面大小
- /// <summary>
- /// 設(shè)置頁面大小
- /// </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 實(shí)例化文檔
- /// <summary>
- /// 實(shí)例化文檔
- /// </summary>
- /// <param name="os">文檔相關(guān)信息(如路徑,打開方式等)</param>
- public void GetInstance(Stream os)
- {
- PdfWriter.GetInstance(document, os);
- }
- #endregion
- #region 打開文檔對(duì)象
- /// <summary>
- /// 打開文檔對(duì)象
- /// </summary>
- /// <param name="os">文檔相關(guān)信息(如路徑,打開方式等)</param>
- public void Open(Stream os)
- {
- GetInstance(os);
- document.Open();
- }
- #endregion
- #region 關(guān)閉打開的文檔
- /// <summary>
- /// 關(guān)閉打開的文檔
- /// </summary>
- public void Close()
- {
- document.Close();
- }
- #endregion
- #region 添加段落
- /// <summary>
- /// 添加段落
- /// </summary>
- /// <param name="content">內(nèi)容</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">內(nèi)容</param>
- /// <param name="fontsize">字體大小</param>
- /// <param name="Alignment">對(duì)齊方式(1為居中,0為居左,2為居右)</param>
- /// <param name="SpacingAfter">段后空行數(shù)(0為默認(rèn)值)</param>
- /// <param name="SpacingBefore">段前空行數(shù)(0為默認(rèn)值)</param>
- /// <param name="MultipliedLeading">行間距(0為默認(rèn)值)</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">對(duì)齊方式(1為居中,0為居左,2為居右)</param>
- /// <param name="newWidth">圖片寬(0為默認(rèn)值,如果寬度大于頁寬將按比率縮放)</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 添加鏈接、點(diǎn)
- /// <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>
- /// 添加鏈接點(diǎn)
- /// </summary>
- /// <param name="Content">鏈接文字</param>
- /// <param name="FontSize">字體大小</param>
- /// <param name="Name">鏈接點(diǎn)名</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
- }
- }
腳本之家友情提醒需要注意點(diǎn):需要添加iTextSharp.dll引用才可以正常通過編譯。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注