C#中PDF文件操作類

風靈使發表於2018-09-07

PDFOperation.cs

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
    }
}

相關文章