C# 利用iTextSharp画PDF

利用iTextSharp画PDF

友情提示:以下画PDF的代码是先创建PDF文件,然后往PDF里填充数据,如果不符合业务需求的,可以滑过

1、首先,添加iTextSharp包
C# 利用iTextSharp画PDF_第1张图片
2、添加PDF基类,供添加页脚、页眉、水印等

 /// 
    /// Pdf处理基类
    /// 
    public class PDFBase : PdfPageEventHelper
    {
        #region 属性
        private String _fontFilePathForHeaderFooter = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "SIMHEI.TTF");
        /// 
        /// 页眉/页脚所用的字体
        /// 
        public String FontFilePathForHeaderFooter
        {
            get
            {
                return _fontFilePathForHeaderFooter;
            }

            set
            {
                _fontFilePathForHeaderFooter = value;
            }
        }

        private String _fontFilePathForBody = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "SIMSUN.TTC,1");
        /// 
        /// 正文内容所用的字体
        /// 
        public String FontFilePathForBody
        {
            get { return _fontFilePathForBody; }
            set { _fontFilePathForBody = value; }
        }


        private PdfPTable _header;
        /// 
        /// 页眉
        /// 
        public PdfPTable Header
        {
            get { return _header; }
            private set { _header = value; }
        }

        private PdfPTable _footer;
        /// 
        /// 页脚
        /// 
        public PdfPTable Footer
        {
            get { return _footer; }
            private set { _footer = value; }
        }


        private BaseFont _baseFontForHeaderFooter;
        /// 
        /// 页眉页脚所用的字体
        /// 
        public BaseFont BaseFontForHeaderFooter
        {
            get { return _baseFontForHeaderFooter; }
            set { _baseFontForHeaderFooter = value; }
        }

        private BaseFont _baseFontForBody;
        /// 
        /// 正文所用的字体
        /// 
        public BaseFont BaseFontForBody
        {
            get { return _baseFontForBody; }
            set { _baseFontForBody = value; }
        }

        private Document _document;
        /// 
        /// PDF的Document
        /// 
        public Document Document
        {
            get { return _document; }
            private set { _document = value; }
        }

        #endregion


        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            try
            {
                BaseFontForHeaderFooter = BaseFont.CreateFont(FontFilePathForHeaderFooter, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                BaseFontForBody = BaseFont.CreateFont(FontFilePathForBody, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                Document = document;
            }
            catch (DocumentException de)
            {

            }
            catch (System.IO.IOException ioe)
            {

            }
        }

        #region GenerateHeader
        /// 
        /// 生成页眉
        /// 
        /// 
        /// 
        public virtual PdfPTable GenerateHeader(iTextSharp.text.pdf.PdfWriter writer)
        {
            return null;
        }
        #endregion

        #region GenerateFooter
        /// 
        /// 生成页脚
        /// 
        /// 
        /// 
        public virtual PdfPTable GenerateFooter(iTextSharp.text.pdf.PdfWriter writer)
        {
            BaseFont baseFont = BaseFontForHeaderFooter;
            iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 10, iTextSharp.text.Font.NORMAL);

            PdfPTable footer = new PdfPTable(3);
            AddFooterCell(footer, "甲方项目经理:", font);
            AddFooterCell(footer, "乙方代表::", font);
            AddFooterCell(footer, "日期:", font);
            return footer;
        }

        private void AddFooterCell(PdfPTable foot, String text, iTextSharp.text.Font font)
        {
            PdfPCell cell = new PdfPCell();
            cell.BorderWidthTop = 2;
            cell.BorderWidthRight = 0;
            cell.BorderWidthBottom = 0;
            cell.BorderWidthLeft = 0;
            cell.Phrase = new Phrase(text, font);
            cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
            foot.AddCell(cell);
        }
        #endregion

        public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
        {
            base.OnEndPage(writer, document);

            输出页眉
            //Header = GenerateHeader(writer);
            //Header.TotalWidth = document.PageSize.Width - 20f;
            /调用PdfTable的WriteSelectedRows方法。该方法以第一个参数作为开始行写入。
            /第二个参数-1表示没有结束行,并且包含所写的所有行。
            /第三个参数和第四个参数是开始写入的坐标x和y.
            //Header.WriteSelectedRows(0, -1, 10, document.PageSize.Height - 20, writer.DirectContent);

            //输出页脚
            Footer = GenerateFooter(writer);
            Footer.TotalWidth = document.PageSize.Width - 20f;
            Footer.WriteSelectedRows(0, -1, 10, document.PageSize.GetBottom(60), writer.DirectContent);
        }
    }

3、根据后台查出的数据画出PDF

 ///友情提示:在代码画PDF之前,最好自己先手动做一个范本(Excel),然后根据对应的行和列,代码画出
 [AllowAnonymous]
        public string BuildPdf(int srqrID)
        {
            //获取数据
            var srqr = SRQR.getInfoByID(srqrID);
            var errMSG = "";
            try
            {
                if (srqr != null && srqr.ID > 0)
                {
					//文件保存路径为当前程序的路径+日期+文件名称
                    var urlHelper = new UrlHelper(this.Request.RequestContext);
                    string hostPath = urlHelper.Content("~/");
                    var dt = DateTime.Now;
                    string fileHost = Server.MapPath("/") + "doc\\SRQRAttachments\\" + dt.ToString("yyyyMMdd") + "\\";
                    if (!Directory.Exists(fileHost))
                    {
                        Directory.CreateDirectory(fileHost);
                    }
                    string fileName = string.Format("{0}-{1}.pdf", "收入确认单审批表", srqr.DJBH);//文件名称
                    string filePath = fileHost + fileName;//文件路径
                    string title = string.Format("收入确认单审批表");

                    BaseFont bf = BaseFont.CreateFont(@"C:\Windows\Fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                    BaseFont fs = BaseFont.CreateFont(@"C:\Windows\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                    Font fonta = new Font(bf, 20, Font.BOLD, BaseColor.BLACK);
                    Font fontb = new Font(bf, 12, Font.NORMAL, BaseColor.BLACK);
                    Font fontc = new Font(fs, 20, Font.BOLD, BaseColor.BLACK);
                    Rectangle pageSize = new Rectangle(1600, 1000);
                    Document document = new Document(pageSize, 20, 20, 50, 80);
                    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
                    writer.PageEvent = new PDFBase();
                    document.Open();
                    Paragraph p1 = new Paragraph(title, fontc);
                    p1.Alignment = Element.ALIGN_CENTER;
                    document.Add(p1);
                    Paragraph p2 = new Paragraph(" ", fontc);
                    p2.Alignment = Element.ALIGN_CENTER;
                    document.Add(p2);
                    document.AddTitle(title);
					//设置列的宽度,要画的PDF有多少列,就写多少个
                    PdfPTable table = new PdfPTable(new float[] { 20, 10, 10, 10, 10, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 10 });
                    table.TotalWidth = 1200;
                    table.LockedWidth = true;
                    #region 第一行
                    PdfPCell row1_Cell1 = new PdfPCell(new Paragraph("单据内码", fontc));
                    row1_Cell1.HorizontalAlignment = Element.ALIGN_CENTER;
                    row1_Cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row1_Cell1);
                    PdfPCell row1_Cell1_value = new PdfPCell(new Paragraph(srqr.DJNM, fontb));
                    row1_Cell1_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row1_Cell1_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row1_Cell1_value.Colspan = 6;
                    table.AddCell(row1_Cell1_value);

                    PdfPCell row1_Cell2 = new PdfPCell(new Paragraph("单据编号", fontc));
                    row1_Cell2.HorizontalAlignment = Element.ALIGN_CENTER;
                    row1_Cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row1_Cell2);
                    PdfPCell row1_Cell2_value = new PdfPCell(new Paragraph(srqr.DJBH, fontb));
                    row1_Cell2_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row1_Cell2_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row1_Cell2_value.Colspan = 3;
                    table.AddCell(row1_Cell2_value);

                    PdfPCell row1_Cell3 = new PdfPCell(new Paragraph("行政组织", fontc));
                    row1_Cell3.HorizontalAlignment = Element.ALIGN_CENTER;
                    row1_Cell3.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row1_Cell3);
                    PdfPCell row1_Cell3_value = new PdfPCell(new Paragraph(srqr.XZZZName, fontb));
                    row1_Cell3_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row1_Cell3_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row1_Cell3_value.Colspan = 4;
                    table.AddCell(row1_Cell3_value);
                    #endregion
                    #region 第二行
                    PdfPCell row2_Cell1 = new PdfPCell(new Paragraph("行政部门", fontc));
                    row2_Cell1.HorizontalAlignment = Element.ALIGN_CENTER;
                    row2_Cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row2_Cell1);
                    PdfPCell row2_Cell1_value = new PdfPCell(new Paragraph(srqr.XZBMName, fontb));
                    row2_Cell1_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row2_Cell1_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row2_Cell1_value.Colspan = 6;
                    table.AddCell(row2_Cell1_value);

                    PdfPCell row2_Cell2 = new PdfPCell(new Paragraph("所属单位", fontc));
                    row2_Cell2.HorizontalAlignment = Element.ALIGN_CENTER;
                    row2_Cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row2_Cell2);
                    PdfPCell row2_Cell2_value = new PdfPCell(new Paragraph(srqr.OwnerDeptName, fontb));
                    row2_Cell2_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row2_Cell2_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row2_Cell2_value.Colspan = 3;
                    table.AddCell(row2_Cell2_value);

                    PdfPCell row2_Cell3 = new PdfPCell(new Paragraph("所属部门", fontc));
                    row2_Cell3.HorizontalAlignment = Element.ALIGN_CENTER;
                    row2_Cell3.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row2_Cell3);
                    PdfPCell row2_Cell3_value = new PdfPCell(new Paragraph(srqr.OwnerProName, fontb));
                    row2_Cell3_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row2_Cell3_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row2_Cell3_value.Colspan = 4;
                    table.AddCell(row2_Cell3_value);
                    #endregion
                    #region 第三行
                    PdfPCell row3_Cell1 = new PdfPCell(new Paragraph("收入类型", fontc));
                    row3_Cell1.HorizontalAlignment = Element.ALIGN_CENTER;
                    row3_Cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row3_Cell1);
                    PdfPCell row3_Cell1_value = new PdfPCell(new Paragraph(srqr.SRLXName, fontb));
                    row3_Cell1_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row3_Cell1_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row3_Cell1_value.Colspan = 6;
                    table.AddCell(row3_Cell1_value);

                    PdfPCell row3_Cell2 = new PdfPCell(new Paragraph("纳税人类型", fontc));
                    row3_Cell2.HorizontalAlignment = Element.ALIGN_CENTER;
                    row3_Cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row3_Cell2);
                    PdfPCell row3_Cell2_value = new PdfPCell(new Paragraph(srqr.NSRLXName, fontb));
                    row3_Cell2_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row3_Cell2_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row3_Cell2_value.Colspan = 3;
                    table.AddCell(row3_Cell2_value);

                    PdfPCell row3_Cell3 = new PdfPCell(new Paragraph("是否主营", fontc));
                    row3_Cell3.HorizontalAlignment = Element.ALIGN_CENTER;
                    row3_Cell3.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row3_Cell3);
                    PdfPCell row3_Cell3_value = new PdfPCell(new Paragraph(srqr.SFZYName, fontb));
                    row3_Cell3_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row3_Cell3_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row3_Cell3_value.Colspan = 4;
                    table.AddCell(row3_Cell3_value);
                    #endregion
                    #region 第四行
                    PdfPCell row4_Cell1 = new PdfPCell(new Paragraph("制单人", fontc));
                    row4_Cell1.HorizontalAlignment = Element.ALIGN_CENTER;
                    row4_Cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row4_Cell1);
                    PdfPCell row4_Cell1_value = new PdfPCell(new Paragraph(srqr.ZDRName, fontb));
                    row4_Cell1_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row4_Cell1_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row4_Cell1_value.Colspan = 6;
                    table.AddCell(row4_Cell1_value);

                    PdfPCell row4_Cell2 = new PdfPCell(new Paragraph("单据日期", fontc));
                    row4_Cell2.HorizontalAlignment = Element.ALIGN_CENTER;
                    row4_Cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row4_Cell2);
                    PdfPCell row4_Cell2_value = new PdfPCell(new Paragraph((srqr.DJRQ != null ? srqr.DJRQ.Value.ToString("yyyy-MM-dd") : ""), fontb));
                    row4_Cell2_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row4_Cell2_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row4_Cell2_value.Colspan = 3;
                    table.AddCell(row4_Cell2_value);

                    PdfPCell row4_Cell3 = new PdfPCell(new Paragraph("单据状态", fontc));
                    row4_Cell3.HorizontalAlignment = Element.ALIGN_CENTER;
                    row4_Cell3.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row4_Cell3);
                    PdfPCell row4_Cell3_value = new PdfPCell(new Paragraph(srqr.DJZTName, fontb));
                    row4_Cell3_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row4_Cell3_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row4_Cell3_value.Colspan = 4;
                    table.AddCell(row4_Cell3_value);
                    #endregion
                    #region 第五行
                    PdfPCell row5_Cell1 = new PdfPCell(new Paragraph("本位币币种", fontc));
                    row5_Cell1.HorizontalAlignment = Element.ALIGN_CENTER;
                    row5_Cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row5_Cell1);
                    PdfPCell row5_Cell1_value = new PdfPCell(new Paragraph(srqr.BWBName, fontb));
                    row5_Cell1_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row5_Cell1_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row5_Cell1_value.Colspan = 6;
                    table.AddCell(row5_Cell1_value);

                    PdfPCell row5_Cell2 = new PdfPCell(new Paragraph("计税方式", fontc));
                    row5_Cell2.HorizontalAlignment = Element.ALIGN_CENTER;
                    row5_Cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row5_Cell2);
                    PdfPCell row5_Cell2_value = new PdfPCell(new Paragraph(srqr.JSFSName, fontb));
                    row5_Cell2_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row5_Cell2_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row5_Cell2_value.Colspan = 3;
                    table.AddCell(row5_Cell2_value);

                    PdfPCell row5_Cell3 = new PdfPCell(new Paragraph("收入确认金额", fontc));
                    row5_Cell3.HorizontalAlignment = Element.ALIGN_CENTER;
                    row5_Cell3.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row5_Cell3);
                    PdfPCell row5_Cell3_value = new PdfPCell(new Paragraph((srqr.SRQRJEBB ?? 0) == 0 ? "" : srqr.SRQRJEBB.Value.ToString("F2"), fontb));
                    row5_Cell3_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row5_Cell3_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row5_Cell3_value.Colspan = 4;
                    table.AddCell(row5_Cell3_value);
                    #endregion
                    #region 第六行
                    PdfPCell row6_Cell1 = new PdfPCell(new Paragraph("附件张数", fontc));
                    row6_Cell1.HorizontalAlignment = Element.ALIGN_CENTER;
                    row6_Cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(row6_Cell1);
                    PdfPCell row6_Cell1_value = new PdfPCell(new Paragraph((srqr.DJFJZS ?? 0).ToString(), fontb));
                    row6_Cell1_value.HorizontalAlignment = Element.ALIGN_CENTER;
                    row6_Cell1_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row6_Cell1_value.Colspan = 6;
                    table.AddCell(row6_Cell1_value);
                    #endregion

                    #region 第七行
                    PdfPCell row7_Cell1 = new PdfPCell(new Paragraph("单据摘要", fontc));
                    row7_Cell1.HorizontalAlignment = Element.ALIGN_CENTER;
                    row7_Cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row7_Cell1.Rowspan = 2;
                    table.AddCell(row7_Cell1);
                    PdfPCell row7_Cell1_value = new PdfPCell(new Paragraph(srqr.DJZY, fontb));
                    row7_Cell1_value.HorizontalAlignment = Element.ALIGN_LEFT;
                    row7_Cell1_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row7_Cell1_value.Colspan = 15;
                    row7_Cell1_value.Rowspan = 2;
                    table.AddCell(row7_Cell1_value);
                    #endregion
                    #region 第八行
                    PdfPCell row8_Cell1 = new PdfPCell(new Paragraph("报账事由", fontc));
                    row8_Cell1.HorizontalAlignment = Element.ALIGN_CENTER;
                    row8_Cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row8_Cell1.Rowspan = 2;
                    table.AddCell(row8_Cell1);
                    PdfPCell row8_Cell1_value = new PdfPCell(new Paragraph(srqr.BZSY, fontb));
                    row8_Cell1_value.HorizontalAlignment = Element.ALIGN_LEFT;
                    row8_Cell1_value.VerticalAlignment = Element.ALIGN_MIDDLE;
                    row8_Cell1_value.Colspan = 15;
                    row8_Cell1_value.Rowspan = 2;
                    table.AddCell(row8_Cell1_value);
                    #endregion
                  
                    var workerList = Wf_QueryDAL.Wf_GetEffectiveTrackList(srqr.WorkID ?? 0);
                    if (workerList != null && workerList.Count > 0)
                    {
                        #region 第九行
                        PdfPCell row9_Cell1 = new PdfPCell(new Paragraph("审批记录", fontc));
                        row9_Cell1.HorizontalAlignment = Element.ALIGN_CENTER;
                        row9_Cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
                        row9_Cell1.Colspan = 16;
                        table.AddCell(row9_Cell1);
                        #endregion
                        foreach (var item in workerList)
                        {
                            #region 审批处理人相关
                            PdfPCell spRow_Cell1 = new PdfPCell(new Paragraph(item.NDFromT, fontc));
                            spRow_Cell1.HorizontalAlignment = Element.ALIGN_CENTER;
                            spRow_Cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
                            table.AddCell(spRow_Cell1);

                            PdfPCell spRow_Cell2 = new PdfPCell(new Paragraph(item.EmpFromT, fontb));
                            spRow_Cell2.HorizontalAlignment = Element.ALIGN_CENTER;
                            spRow_Cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
                            spRow_Cell2.Colspan = 6;
                            table.AddCell(spRow_Cell2);


                            PdfPCell spRow_Cell3 = new PdfPCell(new Paragraph(item.NodeData, fontb));
                            spRow_Cell3.HorizontalAlignment = Element.ALIGN_CENTER;
                            spRow_Cell3.VerticalAlignment = Element.ALIGN_MIDDLE;
                            spRow_Cell3.Colspan = 4;
                            table.AddCell(spRow_Cell3);

                            PdfPCell spRow_Cell4 = new PdfPCell(new Paragraph(item.RDT.Substring(0, 10), fontb));
                            spRow_Cell4.HorizontalAlignment = Element.ALIGN_CENTER;
                            spRow_Cell4.VerticalAlignment = Element.ALIGN_MIDDLE;
                            spRow_Cell4.Colspan = 5;
                            table.AddCell(spRow_Cell4);
                            #endregion
                        }
                    }


                    document.Add(table);
                    document.Close();
                    writer.Close();
                    //给PDF加水印
                    // SetWatermark(filePath, "这是水印");
					//如果需要单独保存附件的一些信息,比如大小等,可利用FileInfo
                    //FileInfo fileInfo = new FileInfo(filePath);
                }
            }
            catch (Exception ex)
            {
               //对异常进行处理
            }
            return errMSG;
        }

4、加水印

 /// 
        /// 给PDF加水印
        /// 
        /// PDF路径
        /// 需要加的水印
        /// 
private void SetWatermark(string outputfilepath, string waterMarkName)
        {
            PdfReader pdfReader = null;
            PdfStamper pdfStamper = null;

            try
            {
                pdfReader = new PdfReader(outputfilepath);
                pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
                // 设置密码  
                //pdfStamper.SetEncryption(false,userPassWord, ownerPassWord, permission);

                int total = pdfReader.NumberOfPages + 1;
                PdfContentByte content;
                BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                PdfGState gs = new PdfGState();
                gs.FillOpacity = 0.3f;//透明度

                int waterMarkNameLenth = waterMarkName.Length;
                char c;
                int rise = 0;
                int span = 15;
                int fontSize = 20;
                int height = 500;
                int width = 1800;
                string spanString = " ";//水平位移
                for (int i = 1; i < total; i++)
                {
                    rise = waterMarkNameLenth * span;
                    //content = pdfStamper.GetOverContent(i);//在内容上方加水印
                    content = pdfStamper.GetUnderContent(i);//在内容下方加水印
                    content.SetGState(gs);
                    content.BeginText();
                    content.SetColorFill(BaseColor.DARK_GRAY);
                    content.SetFontAndSize(font, fontSize);
                    int heightNumbert = (int)Math.Ceiling((decimal)height / (decimal)rise);//垂直重复的次数,进一发
                    int panleWith = (fontSize + span) * waterMarkNameLenth;
                    int widthNumber = (int)Math.Ceiling((decimal)width / (decimal)panleWith);//水平重复次数

                    // 设置水印文字字体倾斜 开始 
                    for (int w = 0; w < widthNumber; w++)
                    {
                        for (int h = 1; h <= heightNumbert; h++)
                        {
                            int yleng = rise * h;
                            content.SetTextMatrix(w * panleWith, yleng);//x,y设置水印开始的绝对左边,以左下角为x,y轴的起点
                            for (int k = 0; k < waterMarkNameLenth; k++)
                            {
                                content.SetTextRise(yleng);//指定的y轴值处添加
                                c = waterMarkName[k];
                                content.ShowText(c + spanString);
                                yleng -= span;
                            }
                        }
                    }
                    content.EndText();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (pdfStamper != null)
                    pdfStamper.Close();

                if (pdfReader != null)
                    pdfReader.Close();
            }
        }

5、效果
C# 利用iTextSharp画PDF_第2张图片

你可能感兴趣的:(c#,后端,c#)