using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Web;
using iTextSharp.text.pdf;
using iText = iTextSharp.text;
using iTextSharp.text;
using System.IO;
namespace ReportViewerHacking
{
public class WaterMarkModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += context_BeginRequest;
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
string url = app.Context.Request.RawUrl;
var context = app.Context;
if (url.Contains("Reserved.ReportViewerWebControl.axd"))
{
var req = context.Request;
var resp = context.Response;
string opType = req["OpType"];
string name = req["Name"];
string format = req["Format"];
if (opType == "Export" && format == "PDF")
{
resp.BufferOutput = true;
resp.Filter = new ExpFileFilterStream(resp, (buff) =>
{
//輸入PDF內容,加上浮水印
PdfReader pr = new PdfReader(buff);
iText.Rectangle dimension = pr.GetPageSize(1);
MemoryStream ms = new MemoryStream();
PdfStamper stmp = new PdfStamper(pr, ms);
//REF: http://bit.ly/10qirzK
BaseFont bf =
BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iText.Font fnt = new iText.Font(bf, 6, iText.Font.NORMAL, BaseColor.BLACK);
PdfContentByte cb = stmp.GetOverContent(1);
//設定半透明文字
PdfGState gstate = new PdfGState();
gstate.FillOpacity = 0.2f;
gstate.StrokeOpacity = 0.2f;
cb.SetGState(gstate);
cb.BeginText();
cb.SetFontAndSize(bf, 6);
cb.SetColorFill(BaseColor.BLACK);
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT,
string.Format("PDF {0:yyyy-MM-dd HH:mm:ss}", DateTime.Now),
dimension.GetLeft(1), dimension.GetTop(5), 0);
cb.EndText();
stmp.Close();
pr.Close();
return ms.ToArray();
});
}
}
}
#endregion
}
}