Hi, I'm trying to add some text to an existing pdf together with a
javascript action.
I managed to do that using PdfStamper. Here's the code:
string LinkText = "Some Text";
string UriStr = "http://www.google.com";
PdfReader reader = new PdfReader("1030D01203.pdf");
FileStream fstream = new FileStream("StamperOut.pdf",
FileMode.Create);
PdfStamper pdfStamper = new PdfStamper(reader, fstream);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
iTextSharp.text.Rectangle pageSize =
reader.GetPageSizeWithRotation(i);
PdfContentByte pdfPageContents =
pdfStamper.GetOverContent(i);//GetUnderContent(i);
pdfPageContents.BeginText(); // Start working with text.
BaseFont baseFont =
BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName,
false);
pdfPageContents.SetFontAndSize(baseFont, 15);
pdfPageContents.SetRGBColorFill(255, 0, 0);
float left = 0; // pageSize.Width / 20;
float bottom = pageSize.Height-100;
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT,
"Some text", left, bottom, 0);
// Add action (javascript link)
string ActionString = "app.launchURL('" + UriStr + "',
true)";
PdfAction action = PdfAction.JavaScript(ActionString,
pdfStamper.Writer);
float width =
pdfPageContents.GetEffectiveStringWidth(LinkText, false);
pdfPageContents.SetAction(action, left, bottom, left +
width, bottom + 20);
pdfPageContents.EndText();
}
pdfStamper.FormFlattening = true;
pdfStamper.Close();
By the way, when I try the same using a *PdfCopy *instead of *PdfStamper*,
on SetFontAndSize I get the following exception: *"Content can not be added
to a PdfImportedPage."*.
Is there a way to obtain the same result with pdfcopy?
for (int i=1; i <= reader.NumberOfPages ;i++)
{
PdfImportedPage page = copy.GetImportedPage(reader, i);
page.BeginText(); // Start working with text.
BaseFont baseFont =
BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName,
false);
page.SetFontAndSize(baseFont, 15);
page.SetRGBColorFill(255, 0, 0);
float left = 0; // pageSize.Width / 20;
float bottom = pageSize.Height - 100;
page.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Some
text", left, bottom, 0);
// Add action (javascript link)
string ActionString = "app.launchURL('" + UriStr + "',
true)";
PdfAction action = PdfAction.JavaScript(ActionString,
copy);
float width = page.GetEffectiveStringWidth(LinkText,
false);
page.SetAction(action, left, bottom, left + width,
bottom + 20);
page.EndText();
copy.AddPage(page);
}
Thanks in advance
Federico
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php