FCI-Helwan blog

Just another FCI-H weblog

Stamping text file in PDF pages

I wrote C# function that Stamping the content of text file (text and hyper links) at the front of the select pages (selected by the user) from the PDF .

This C# function stamp the text with aligning the text at the center of the selected pages ,the function based on the latest library called ItextCharp ( iTextSharp 4.0.8 (2008-01-25) .
Download the library from here


# (iTextSharp) is a port of the iText open source java library written entirely in C# for the .NET platform.
iText is a library that allows you to generate PDF files on the fly. It is implemented as an assembly.

Parameter Example :

string sourcefile –> “input.pdf”
string SaveFileName–> “output.pdf”
float Y_space –> 405
float lead–> 25
float FontSize –> 12
string TxtFileName–> “textostamp.txt”
string _Font –> “HELVETICA”
string pagesRange –> “3,6,8” or “3-6,9-15”

you must to import this namespaces 🙂
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text;

/* StamperTxt is a class which stamps the content of text file (text and hyper links)

* at the front of the select pages (selected by the user)from the PDF .

* */

public StamperTxt(string sourcefile, string SaveFileName, float Y_space, float lead, float FontSize, string TxtFileName, string _Font , string pagesRange)


PdfReader pdfreader = new PdfReader(sourcefile);

float pagewidth = pdfreader.GetPageSize(1).Width;

int j = pdfreader.NumberOfPages;

bool[] flag ;

flag = new bool[j];

for (int i = 0; i

flag[i] = false;

if ((pagesRange.Trim() == “all” )&&(pagesRange.IndexOf(“-“) == -1 )&&(pagesRange.IndexOf(“,”) == -1 ) )


for (int ii = 0; ii

flag[ii] = true;


else if ((pagesRange.IndexOf(“-“) != -1))


string[] tempArr = pagesRange.Trim().Split(‘,’);

foreach (string str in tempArr)


string[] tempArrhyphen = str.Trim().Split(‘-‘);

int startIndex = 0, endIndex = 0;

startIndex =int.Parse(tempArrhyphen[0].Trim());

endIndex = int.Parse(tempArrhyphen[1].Trim());

for (int s = startIndex; s <= endIndex; s++)


flag[s-1] = true;




else if ((pagesRange.IndexOf(‘,’) > -1))


string[] tempArr = pagesRange.Trim().Split(‘,’);

foreach (string str in tempArr)


int index = int.Parse(str);

flag[index-1] = true;



PdfGState pdfgstate = new PdfGState();

pdfgstate.FillOpacity = 1.0f;

Stream output_stream = new FileStream(SaveFileName, FileMode.OpenOrCreate);

PdfStamper pdfstamper = new PdfStamper(pdfreader, output_stream);

for (int k = 0; k


if (flag[k] != true)


PdfContentByte pdfcontentbyte = pdfstamper.GetOverContent(k+1);

float v_space = Y_space;



BaseFont PF = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, false) ;

pdfcontentbyte. SetFontAndSize(PF , FontSize);

StreamReader SR;

string S;

SR = File.OpenText(TxtFileName);

ArrayList chunklist = new ArrayList();

S = SR.ReadLine();

while (S != null)


Chunk chk = new Chunk(S + “\r\n”, FontFactory.GetFont(_Font, FontSize, Font.NORMAL));

if (S.IndexOf(“http:/”) > -1)


chk = new Chunk(S + “\r\n”, FontFactory.GetFont(_Font, FontSize, Font.UNDERLINE));




S = SR.ReadLine();




Phrase PH = new Phrase();

PH.Font = FontFactory.GetFont(_Font, FontSize, Font.NORMAL);


PH.Leading = lead;

PdfPTable table = new PdfPTable(1);

table.DefaultCell.Border = Rectangle.NO_BORDER;

table.DefaultCell.SetLeading(lead, 0);

table.TotalWidth = pagewidth;

table.HorizontalAlignment = Rectangle.ALIGN_MIDDLE;

table.DefaultCell.HorizontalAlignment = Rectangle.ALIGN_MIDDLE;

table.HorizontalAlignment = Rectangle.ALIGN_CENTER;

table.DefaultCell.HorizontalAlignment = Rectangle.ALIGN_CENTER;


table.WriteSelectedRows(0, -1,0, v_space, pdfcontentbyte);


byte[] arr = pdfstamper.Writer.XmpMetadata;



Console.WriteLine(SaveFileName + “Was Created in Success…”);

}////end Function


February 14, 2008 - Posted by | C#, Tips


  1. Ahmed,

    We are seeking a consultant to assist us with developing a method for using iTextSharp to flatten annotations inside a PDF. I was wondering if you would have interest in helping us with this, or if you could recommend others who we could contact.

    We need to be able to flatten all annotations inside a PDF. We have found that the setFreeTextFlattening method in iTextSharp will flatten text-based elements, but it does not apply to graphical annotations such as lines, shapes, revisions clouds, and stamps.

    We will pay a consulting fee for assistance with this project. Please let me know if you could help or if you could recommend others who we could contact. Thank you.

    Email: ostkm@yahoo.com

    Comment by matt | June 18, 2008 | Reply

  2. Dear Matt,

    Thanks you for comment and this trust,I am really extremely busy this days, I can recommend other for you .I think Eng.Tarek can support you too, he have a strong experience with manipulate the PDF format and iTextShap .
    Eng.Tarek’s Mail: eltareksoft@gmail.com

    Comment by Ahmed Essawy | June 18, 2008 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: