PDF添加水印


using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;
using System.Data;
using System;
using System.Web;

public class CreatePDF
{
    private static CreatePDF instance;
    public static CreatePDF GetInstance()
    {
        if (instance == null)
        {
            instance = new CreatePDF();
        }
        return instance;
    }

    private static Document doc;
    private static BaseFont bf = BaseFont.CreateFont(@"C://Windows/Fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    //四種字體
    private static Font fontBig = new Font(bf, 20, Font.BOLD);
    private static Font fontMiddle = new Font(bf, 15, Font.BOLD);
    private static Font fontSmall = new Font(bf, 13, Font.BOLD);
    private static Font fontSmallNoBold = new Font(bf, 13);
    private static float IndentationLeft = 50;//距左邊距
    //如果要傳參數進來,可自定義
    public void GeneratePDF(string fileName,string filePath)
    {
        doc = new Document(PageSize.A4);
        try
        {
            //MemoryStream ms2 = new MemoryStream();
            //string fileName = string.Format("數據_{0}.pdf", DateTime.Now.ToString("yyyyMMddHHmmss"));
            //string filePath = string.Format("{0}/Resource/{1}", AppDomain.CurrentDomain.BaseDirectory, fileName);
            FileStream fs = new FileStream(filePath, FileMode.Open);//創建臨時文件,到時生成好后刪除
            PdfWriter writer = PdfWriter.GetInstance(doc, fs);
            writer.CloseStream = false;//把doc內容寫入流中
            doc.Open();

            //核心操作
            //CreateEmptyRow(1);//生成一行空行
            //CreateLine();//生成一條下橫線
            //AddHeaderTitleContent("表10 已安裝工程量明細表");//添加表頭
            //CreateEmptyRow(1);//生成一行空行
            //AddPartnerContent("工程名稱", "建設名稱", "施工名稱", "監理名稱");//添加合作單位
            //AddPageNumberContent();//添加頁碼
            //CreateEmptyRow(1);//生成一行空行

            //#region 生成表格數據
            //PdfPTable table = new PdfPTable(6);//6列的table
            ////添加表格列頭               
            //table.SetTotalWidth(new float[] { 50, 200, 60, 60, 100, 100 });
            //table.AddCell(GetPdfCell("序號", fontSmallNoBold, Element.ALIGN_CENTER));
            //table.AddCell(GetPdfCell("工程量名稱", fontSmallNoBold, Element.ALIGN_CENTER));
            //table.AddCell(GetPdfCell("單位", fontSmallNoBold, Element.ALIGN_CENTER));
            //table.AddCell(GetPdfCell("數量", fontSmallNoBold, Element.ALIGN_CENTER));
            //table.AddCell(GetPdfCell("安裝地點、路段", fontSmallNoBold, Element.ALIGN_CENTER));
            //table.AddCell(GetPdfCell("備注", fontSmallNoBold, Element.ALIGN_CENTER));

            //int emptyRow = 20;//如果table的行數小於20行,那么剩余部分顯示空白行
            //#region 構造數據源
            //DataTable tableSource = new DataTable();
            //tableSource.Columns.Add(new DataColumn("aa"));
            //tableSource.Columns.Add(new DataColumn("bb"));
            //tableSource.Columns.Add(new DataColumn("cc"));
            //tableSource.Columns.Add(new DataColumn("dd"));
            //tableSource.Columns.Add(new DataColumn("ee"));
            //for (int i = 0; i < 15; i++)
            //{
            //    DataRow row = tableSource.NewRow();
            //    row["aa"] = "aa";
            //    row["bb"] = "bb";
            //    row["cc"] = "cc";
            //    row["dd"] = "dd";
            //    row["ee"] = "ee";
            //    tableSource.Rows.Add(row);
            //}
            //#endregion
            //if (tableSource.Rows.Count > 0)
            //{
            //    emptyRow = emptyRow - tableSource.Rows.Count;//如果為負數,說明不需要生成空白行
            //    for (int i = 0; i < tableSource.Rows.Count; i++)
            //    {
            //        DataRow row = tableSource.Rows[i];
            //        table.AddCell(GetPdfCell((i + 1).ToString(), fontSmallNoBold, Element.ALIGN_CENTER));
            //        table.AddCell(GetPdfCell(row["aa"].ToString(), fontSmallNoBold, Element.ALIGN_CENTER));
            //        table.AddCell(GetPdfCell(row["bb"].ToString(), fontSmallNoBold, Element.ALIGN_CENTER));
            //        table.AddCell(GetPdfCell(row["cc"].ToString(), fontSmallNoBold, Element.ALIGN_CENTER));
            //        table.AddCell(GetPdfCell(row["dd"].ToString(), fontSmallNoBold, Element.ALIGN_CENTER));
            //        table.AddCell(GetPdfCell(row["ee"].ToString(), fontSmallNoBold, Element.ALIGN_CENTER));
            //    }
            //}
            //if (emptyRow > 0)//說明數據源不足20行
            //{
            //    for (int i = 0; i < emptyRow; i++)
            //    {
            //        table.AddCell(GetPdfCell(((20 - emptyRow) + i + 1).ToString(), fontSmallNoBold, Element.ALIGN_CENTER));
            //        table.AddCell(GetPdfCell("", fontSmallNoBold, Element.ALIGN_CENTER));
            //        table.AddCell(GetPdfCell("", fontSmallNoBold, Element.ALIGN_CENTER));
            //        table.AddCell(GetPdfCell("", fontSmallNoBold, Element.ALIGN_CENTER));
            //        table.AddCell(GetPdfCell("", fontSmallNoBold, Element.ALIGN_CENTER));
            //        table.AddCell(GetPdfCell("", fontSmallNoBold, Element.ALIGN_CENTER));
            //    }
            //}
            //doc.Add(table);
            //#endregion

            //添加N行空行
            CreateEmptyRow(1);//生成一行空行

            #region 添加水印
            string waterMarkName = "經度:無  緯度:無  地址:無  驗收負責人:無";
            string waterMarkAddr = "廣州市天河區";
            if (!string.IsNullOrEmpty(waterMarkAddr))
            {
                waterMarkName = string.Format("經度:{0}  緯度:{1}  地址:{2}  驗收負責人:{3}", "113.211", "23.211", waterMarkAddr, "張三");
            }
            #endregion

            doc.Close();
            MemoryStream ms = new MemoryStream();
            if (fs != null)
            {
                byte[] bytes = new byte[fs.Length];//定義一個長度為fs長度的字節數組
                fs.Read(bytes, 0, (int)fs.Length);//把fs的內容讀到字節數組中
                ms.Write(bytes, 0, bytes.Length);//把字節內容讀到流中
                fs.Flush();
                fs.Close();
            }
            MemoryStream waterMS = SetWaterMark(ms, filePath, waterMarkName);//先生成水印,再刪除臨時文件
            if (File.Exists(filePath))//判斷臨時文件是否存在,如果存在則刪除
            {
                File.Delete(filePath);
                GC.Collect();//回收垃圾
            }
            //SendFile(fileName, waterMS);//把PDF文件發送回瀏覽器
            FileStream savefs = new FileStream(filePath, FileMode.Create);
            BinaryWriter w = new BinaryWriter(savefs);
            w.Write(waterMS.ToArray());
            savefs.Close();
            waterMS.Close();

        }
        catch (DocumentException ex)
        {
            throw new Exception(ex.Message);
        }
    }
    #region 生成一條橫線
    private static void CreateLine()
    {
        PdfPTable table = new PdfPTable(1);//一個單元格的
        table.TotalWidth = 500;
        PdfPCell cell = new PdfPCell();
        cell.BorderWidthBottom = 0.5f;
        table.AddCell(cell);
        doc.Add(table);
    }
    #endregion

    #region 生成N行空白行
    private static void CreateEmptyRow(int emptyRowNum)
    {
        for (int i = 0; i < emptyRowNum; i++)
        {
            doc.Add(new Paragraph(" "));
        }
    }
    #endregion

    #region 生成標題
    private static void AddHeaderTitleContent(string content)
    {
        Paragraph p = new Paragraph(content, fontMiddle);
        p.IndentationLeft = IndentationLeft;//距離左邊距
        doc.Add(p);
    }
    #endregion

    #region 生成合作單位
    private static void AddPartnerContent(string stationName, string constructUnit, string buildUnit, string supervisionUnit)
    {
        fontMiddle.SetStyle(Font.UNDERLINE);//文字下划線
        IndentationLeft = IndentationLeft + 10;

        Paragraph content = new Paragraph();
        content.IndentationLeft = IndentationLeft;
        Chunk chunkName = new Chunk("單項或單位工程名稱:", fontSmallNoBold);
        Chunk chunkText = new Chunk(GetEmptyString(25, stationName), fontMiddle);
        content.Add(0, chunkName);
        content.Add(1, chunkText);
        content.Alignment = 3;
        doc.Add(content);

        content = new Paragraph();
        content.IndentationLeft = IndentationLeft;
        chunkName = new Chunk("建設單位名稱:", fontSmallNoBold);
        chunkText = new Chunk(GetEmptyString(30, constructUnit), fontMiddle);
        content.Add(0, chunkName);
        content.Add(1, chunkText);
        content.Alignment = 3;
        doc.Add(content);

        content = new Paragraph();
        content.IndentationLeft = IndentationLeft;
        chunkName = new Chunk("施工單位名稱:", fontSmallNoBold);
        chunkText = new Chunk(GetEmptyString(30, buildUnit), fontMiddle);
        content.Add(0, chunkName);
        content.Add(1, chunkText);
        content.Alignment = 3;
        doc.Add(content);

        content = new Paragraph();
        content.IndentationLeft = IndentationLeft;
        chunkName = new Chunk("監理單位名稱:", fontSmallNoBold);
        chunkText = new Chunk(GetEmptyString(30, supervisionUnit), fontMiddle);
        content.Add(0, chunkName);
        content.Add(1, chunkText);
        content.Alignment = 3;
        doc.Add(content);
    }
    //居中顯示內容
    private static string GetEmptyString(int maxlength, string text)
    {
        int padding = (maxlength - text.Length * 2) / 2;
        string empty = string.Empty;
        for (int i = 0; i < padding; i++)
        {
            empty += " ";
        }
        return string.Format("{0}{1}{0}", empty, text);
    }
    #endregion

    #region 生成頁碼
    private static void AddPageNumberContent()
    {
        var content = new Paragraph("共   頁  第   頁", fontSmall);
        content.IndentationRight = IndentationLeft + 20;
        content.Alignment = 2;    //居左
        doc.Add(content);
    }
    #endregion

    #region 生成單元格
    private static PdfPCell GetPdfCell(string content, Font font, int horizontalAlignment)
    {
        PdfPCell cell = new PdfPCell(new Paragraph(content, font));
        cell.HorizontalAlignment = horizontalAlignment;//水平位置
        cell.VerticalAlignment = Element.ALIGN_CENTER;//垂直居中
        cell.MinimumHeight = 20;//單元格的最小高度
        return cell;
    }
    #endregion

    #region 生成水印
    private static MemoryStream SetWaterMark(MemoryStream ms, string filePath, string waterMarkName, string waterMarkAddr = null)
    {
        MemoryStream msWater = new MemoryStream();
        PdfReader pdfReader = null;
        PdfStamper pdfStamper = null;
        try
        {
            pdfReader = new PdfReader(filePath);
            pdfStamper = new PdfStamper(pdfReader, msWater);

            int total = pdfReader.NumberOfPages + 1;//獲取PDF的總頁數
            iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);//獲取第一頁
            float width = psize.Width;//PDF頁面的寬度,用於計算水印傾斜
            float height = psize.Height;
            PdfContentByte waterContent;
            BaseFont basefont = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            PdfGState gs = new PdfGState();
            for (int i = 1; i < total; i++)
            {
                waterContent = pdfStamper.GetOverContent(i);//在內容上方加水印
                //透明度
                waterContent.SetGState(gs);
                //開始寫入文本
                waterContent.BeginText();
                waterContent.SetColorFill(BaseColor.RED);
                waterContent.SetFontAndSize(basefont, 18);
                waterContent.SetTextMatrix(0, 0);
                if (waterMarkAddr == null || waterMarkAddr == "")
                {
                    waterContent.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2, height / 2, 55);
                }
                else
                {
                    waterContent.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2, height / 2 + 100, 55);
                    waterContent.ShowTextAligned(Element.ALIGN_CENTER, waterMarkAddr, width / 2, height / 2 - 100, 55);
                }
                waterContent.EndText();
            }
        }
        catch (Exception)
        {
            return ms;
        }
        finally
        {
            if (pdfStamper != null)
                pdfStamper.Close();

            if (pdfReader != null)
                pdfReader.Close();
        }
        return msWater;
    }
    #endregion

    //-----------------------發送PDF文件回瀏覽器端----------------------
    public static void SendFile(string fileName, MemoryStream ms, Encoding encoding = null)
    {
        fileName = (fileName + "").Replace(" ", "");
        encoding = encoding ?? Encoding.UTF8;
        if (ms != null && !string.IsNullOrEmpty(fileName))
        {
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.Clear();
            response.Charset = encoding.BodyName;// "utf-8";
            if (!HttpContext.Current.Request.UserAgent.Contains("Firefox") && !HttpContext.Current.Request.UserAgent.Contains("Chrome"))
            {
                fileName = HttpUtility.UrlEncode(fileName, encoding);
            }
            response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            //為了解決打開,導出NPOI生成的xlsx文件時,提示發現不可讀取內容。
            if (!(fileName + "").ToLower().EndsWith(".xlsx"))
            {
                response.AddHeader("Content-Type", "application/octet-stream");
                response.BinaryWrite(ms.GetBuffer());
            }
            else
            {
                response.BinaryWrite(ms.ToArray());
            }
            ms.Close();
            ms = null;
            response.Flush();
            response.End();
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM