<%@ WebHandler Language="C#" Class="PrintFile" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.Collections;
public class PrintFile : IHttpHandler
{
public string Code1;
public void ProcessRequest(HttpContext context)
{
try
{
String tableName = NewEcryptDecrypt.DecryptString(context.Request.QueryString["URL"]);
byte[] bytes;
string fileName, contentType;
string constr = ConfigurationManager.ConnectionStrings["AppConStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM " + tableName + " WHERE Id=@Id";
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["FileData"];
contentType = sdr["ContentType"].ToString();
fileName = sdr["FileName"].ToString();
int TotalPage = 0;
if (contentType == "Application/pdf" || contentType == "Application/PDF")
{
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
PdfReader reader1 = new PdfReader(bytes);
using (PdfStamper stamper = new PdfStamper(reader1, memoryStream))
{
int pages = reader1.NumberOfPages;
TotalPage = pages;
for (int i = 1; i <= pages; i++)
{
float factor = .92f;
shrink(stamper, i, factor);
Rectangle box = reader1.GetCropBox(i);
box.Top = box.Top - factor * box.Height;
PdfContentByte cb = stamper.GetOverContent(i);
cb.SetColorFill(BaseColor.YELLOW);
cb.SetColorStroke(BaseColor.BLACK);
cb.Rectangle(box.Left, box.Bottom, box.Width, box.Height);
cb.FillStroke();
cb.SetColorFill(BaseColor.BLACK);
ColumnText ct = new ColumnText(cb);
ct.AddElement(new Paragraph(" You can varify this 7/12 from this URL(GSWAN) http://10.110.3.98/web" + " Enter code : " + code, FontFactory.GetFont("Calibri", 11, Font.BOLD)));
ct.AddElement(new Paragraph(" Issued By: " + OfficeName + " " + " Print Date:" + DateTime.Now + " " + "5 Rs per Side", FontFactory.GetFont("Calibri", 11, Font.BOLD)));
ct.SetSimpleColumn(box);
ct.Go();
}
}
bytes = memoryStream.GetBuffer();
memoryStream.Flush();
memoryStream.Dispose();
bytes = memoryStream.ToArray();
}
con.Close();
}
}
context.Response.Buffer = true;
context.Response.Charset = "";
if (context.Request.QueryString["download"] == "1")
{
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
}
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = contentType;
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
}
catch (Exception e)
{
}
}
public bool IsReusable
{
get
{
return false;
}
}
public void shrink(PdfStamper stamper, int page, float factor)
{
Rectangle crop = stamper.Reader.GetCropBox(page);
float diffX = crop.Right * (1 - factor);
float diffY = crop.Top * (1 - factor);
PdfDictionary pageN = stamper.Reader.GetPageN(page);
stamper.MarkUsed(pageN);
PdfArray ar = null;
PdfObject content = PdfReader.GetPdfObject(pageN.Get(PdfName.CONTENTS), pageN);
if (content == null)
return;
if (content.IsArray())
{
ar = new PdfArray((PdfArray)content);
pageN.Put(PdfName.CONTENTS, ar);
}
else if (content.IsStream())
{
ar = new PdfArray();
ar.Add(pageN.Get(PdfName.CONTENTS));
pageN.Put(PdfName.CONTENTS, ar);
}
else
return;
ByteBuffer out_p = new ByteBuffer();
out_p.Append(factor).Append(" 0 0 ").Append(factor).Append(' ').Append(diffX).Append(' ').Append(diffY).Append(" cm ");
PdfStream stream = new PdfStream(out_p.ToByteArray());
ar.AddFirst(stamper.Writer.AddToBody(stream).IndirectReference);
out_p.Reset();
}
}This code snippet is for multiple page have same footer In PDF file (IText Sharp) ASP.NET using handler.