Hi all,
I have developed one pdf httphandler in sharepoint 2010. Its working fine but sometimes while clicking on pdf link empty page is displaying in the browser. Can somebody pls help me out why this is happening. Here is my code:
public class PDFSharepointHttpHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
SPFile file = SPContext.Current.Web.GetFile(context.Request.Url.ToString());
byte[] content = file.OpenBinary();
SPUser currentUser = SPContext.Current.Web.CurrentUser;
string watermark = null;
if (currentUser != null)
watermark = "This download was specially prepared for " + currentUser.Name;
if (watermark != null)
{
PdfReader pdfReader = new PdfReader(content);
using (MemoryStream outputStream = new MemoryStream())
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
{
for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
{
//Rectangle class in iText represent geometric representation...
//in this case, rectangle object would contain page geometry
Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
//PdfContentByte object contains graphics and text content of page returned by PdfStamper
PdfContentByte pdfData = pdfStamper.GetUnderContent(pageIndex);
//create font size for watermark
pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 8);
//create new graphics state and assign opacity
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.4F;
//set graphics state to PdfContentByte
pdfData.SetGState(graphicsState);
//indicates start of writing of text
pdfData.BeginText();
//show text as per position and rotation
pdfData.ShowTextAligned(Element.ALIGN_CENTER, watermark, pageRectangle.Width / 4, pageRectangle.Height / 44, 0);
//call endText to invalid font set
pdfData.EndText();
}
pdfStamper.Close();
content = outputStream.ToArray();
}
}
context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite(content);
context.Response.End();
}
public bool IsReusable
{
get { return false; }
}
}