如何使用C#在所有页面中设置pdf水印

时间:2019-02-13 13:02:58

标签: c# jquery asp.net .net model-view-controller

我正在MVC Web应用程序中工作。我尝试创建PDF水印。仅在第一页中有水印,第一页后我没有水印。但是文件已下载。我的代码如下。

public FileResult Export(string GridHtml, string Header)
{
    using (MemoryStream stream = new System.IO.MemoryStream())
    {
        StringReader sr = new StringReader(GridHtml);
        Document pdfDoc = new Document(iTextSharp.text.PageSize.A4.Rotate(), 10, 10, 10, 10);
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
        pdfDoc.Open();
        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(Server.MapPath("~/UploadFiles/HQ-90.jpg"));
        jpg.ScaleToFit(300, 280);  
        jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
        jpg.SetAbsolutePosition(300, 150);     
        pdfDoc.Add(jpg); 
        PdfContentByte cb = writer.DirectContent;
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1257, BaseFont.NOT_EMBEDDED);
        cb.SetFontAndSize(bf, 14);
        cb.BeginText();
        string text = "Signature with Seal";
        cb.ShowTextAligned(1, text, 700, 90, 0);
        cb.EndText();
        text = Header;
        Paragraph para = new Paragraph(text);
        para.SpacingBefore = 10;
        para.SpacingAfter =2;
        para.Alignment = 1; //0-Left, 1 middle,2 Right
        pdfDoc.Add(para);
        Paragraph date = new Paragraph("Date : " + DateTime.Now.ToString("dd/MM/yyyy"));
        //p.SpacingBefore = 20;
        date.SpacingAfter = 0;
        date.Alignment = 2; //0-Left, 1 middle,2 Right
        date.Font.Size = 09;
        pdfDoc.Add(date);

        Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)));
        //p.SpacingBefore = 20;
        p.SpacingAfter = 10;
        p.Alignment = 0; //0-Left, 1 middle,2 Right
        pdfDoc.Add(p);

        XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
        pdfDoc.Close();
        return File(stream.ToArray(), "application/pdf", "TNP_"+Header+"_"+DateTime.Now.ToString("dd/MM/yyyy")+".pdf");
    }
} 

0 个答案:

没有答案