1

我已经使用 ITextSharp 成功地将我的 aspx 页面呈现为 pdf。现在我想在 PDF 文件的背景中添加水印标志,请帮帮我,我已经陷入困境。

以下是我导出为 pdf 的代码

private void ShowPdf(string s)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + s);
        Response.ContentType = "application/pdf";
        Response.WriteFile(s);
        Response.Flush();
        Response.Clear();
    }
    public void PrepareControlForPDF()
    {
        MemoryStream mem = new MemoryStream();
        StreamWriter twr = new StreamWriter(mem);
        HtmlTextWriter myWriter = new HtmlTextWriter(twr);
        divApplicantDetails.RenderControl(myWriter);
        myWriter.Flush();
        //  myWriter.Dispose();
        StreamReader strmRdr = new StreamReader(mem);       
        strmRdr.BaseStream.Position = 0;
        string pageContent = strmRdr.ReadToEnd();
        //CreatePDFDocument(strmRdr);
        //strmRdr.Dispose();
        ///mem.Dispose();
        CreatePDFDocument(pageContent);
        //writer.Write(pageContent);
    
    }
    public void CreatePDFDocument(string strHtml)
    {
        string filename =  ""+System.DateTime.Now.Day+"AppLetter.pdf";
        //          if (System.IO.File.Exists(Server.MapPath("../Pdf") + "/" + filename))
        //          {
        //              System.IO.File.Delete(Server.MapPath("../Pdf") + "/" + filename);
        //          }
        string strFileName = Server.MapPath("../Pdf") + "/" + filename;

        Document document = new Document();
        try
        {
            PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
            StringReader se = new StringReader(strHtml);
            MemoryStream ms = new MemoryStream();
            ms.Write(System.Text.Encoding.ASCII.GetBytes(strHtml), 0, System.Text.Encoding.ASCII.GetBytes(strHtml).Length);
            //ms.Position = 0;
            StreamReader sr = new StreamReader(new MemoryStream(new System.Text.ASCIIEncoding().GetBytes(strHtml)));
            sr.BaseStream.Position = 0;
            HTMLWorker obj = new HTMLWorker(document);
        
            document.Open();

            obj.Parse(se);
        }
        finally
        {
            document.Close();
        }
    }
4

1 回答 1

0

我认为您可以通过创建一个新类并实现IPdfPageEvent interface...参考这里

于 2014-02-26T11:44:11.147 回答