我使用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();
}
}