This blog is subject the DISCLAIMER below.

Thursday, February 14, 2008

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 .

NOTE :
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

iText

# (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)

continue;

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

float v_space = Y_space;

pdfcontentbyte.SaveState();

pdfcontentbyte.SetGState(pdfgstate);

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));

chk.SetAnchor(S);

}

chunklist.Add(chk);

S = SR.ReadLine();

}

//Console.WriteLine(S);

SR.Close();

Phrase PH = new Phrase();

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

PH.AddRange(chunklist);

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.AddCell(PH);

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

}

byte[] arr = pdfstamper.Writer.XmpMetadata;

pdfstamper.Close();

pdfreader.Close();

Console.WriteLine(SaveFileName + "Was Created in Success...");

}////end Function

3 comments:

Matt Ostanik said...

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.

Matt
Email: ostkm@yahoo.com

Ahmed Essawy said...

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

tarjeta nintendo r4 said...

It's a very useful information, I am glad to see it. Thanks for sharing...