I dont have experience with annotations - but often I need to position something absolutely on a page and I use a 'fake' table. If you can add your annotation to a 'cell', and I think you can, try this
PdfContentByte cb = writer.DirectContent;
PdfPTable table = new PdfPTable(1);
table.TotalWidth = 400f;
table.AddCell();
table.WriteSelectedRows(0, -1, 200, 50, cb);
You can set the table borders to whatever you wish, I make them invisible
Ok, this is 'the base' of my process :-
PdfContentByte cb;
PdfReader reader = new PdfReader(
using (System.IO.FileStream fs = new System.IO.FileStream(
{
PdfStamper pdfStamper = new (PdfStamper, fs);
cb = pdfStamper.GetOverContent(1);
cb.BeginText();
PdfPTable table = new PdfPTable(1);
table.TotalWidth = 400f;
table.WriteSelectedRows(0, -1, 200, 50, cb);
cb.EndText();
pdfStamper.FormFlattening = true;
pdfStamper.Close();
}
Then either of these two options
var webAddress = new Chunk("CodeProject");
webAddress.SetAnchor("http://www.codeproject.com");
var para = new Paragraph(webAddress);
para.Alignment = Element.ALIGN_CENTER;
PdfPCell anchorCell = new PdfPCell();
anchorCell.AddElement(para);
anchorCell.Padding = 30f;
anchorCell.Border = Rectangle.NO_BORDER;
table.AddCell(anchorCell);
or
var webAddress = new Chunk("CodeProject");
webAddress.SetAnchor("http://www.codeproject.com");
PdfPCell anchorCell = new PdfPCell();
anchorCell.AddElement(webAddress);
anchorCell.Border = Rectangle.NO_BORDER;
table.AddCell(anchorCell);
You'll need to set
//Source PDF File Name
//Target PDF Output File Name
To values for a PDF file in (I use this as an 'underlay') and a PDF file out
btw - this is typed up from handwritten notes, Im on my mac and cant get to my dev machine at the moment, so it may need a bit of editing here and there
You can copy Option 1 or 2 in here (or make a static procedure and pass in cb / ContentBytes
Quote:
// Option 1 or Option 2 Here - I usually use 'builder' procedures for Tables/Paragraphs/Cells
.. as it says, I usually have a set of 'builders' that allow me to assembly fonts, paragraphs from chunks, tables from cells etc
Complete working example (Create PDF From scratch)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace CPPDF001
{
class Program
{
static void Main(string[] args)
{
BaseFont bfTimesRoman = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font fTimesRomanItalic10 = new Font(bfTimesRoman, 10, Font.ITALIC, BaseColor.BLACK);
String testPDFName = "CPPFF001.pdf";
if (File.Exists(testPDFName))
{
File.Delete(testPDFName);
}
using (FileStream fs = new FileStream("CPPFF001.pdf", FileMode.Create))
{
Document doc = new Document(PageSize.A4, 25,25,30,30);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
PdfContentByte cb = writer.DirectContent;
cb.BeginText();
writeText(cb, "Garth Was Here !!!", 30, 718, bfTimesRoman, 14);
cb.SetFontAndSize(bfTimesRoman, 10);
PdfPTable table = new PdfPTable(1);
table.TotalWidth = 400f;
var chunk = new Chunk("CodeProject");
chunk.SetAnchor("http://www.codeproject.com");
chunk.SetUnderline(0.5f, -1.5f);
PdfPCell cell = new PdfPCell();
cell.AddElement(chunk);
table.AddCell(cell);
table.WriteSelectedRows(0, -1, 30, 646, cb);
PdfPTable table1 = new PdfPTable(1);
table1.TotalWidth = 100f;
PdfPCell cell1 = new PdfPCell();
cell1.AddElement(chunk);
cell1.Border = Rectangle.NO_BORDER;
table1.AddCell(cell1);
table1.WriteSelectedRows(0, -1, 30, 574, cb);
PdfPTable table2 = new PdfPTable(1);
table2.TotalWidth = 100f;
PdfPCell cell2 = new PdfPCell();
var chunk1 = new Chunk("CodeProject", fTimesRomanItalic10);
chunk1.SetAnchor("http://www.codeproject.com");
cell2.AddElement(chunk1);
cell2.Border = Rectangle.NO_BORDER;
table2.AddCell(cell2);
table2.WriteSelectedRows(0, -1, 30, 502, cb);
cb.EndText();
doc.Close();
}
}
static void writeText(PdfContentByte cb, string text, int x, int y, BaseFont font, int size)
{
cb.SetFontAndSize(font, size);
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0);
}
}
}
}
Complete working example (annotating existing PDF)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace CPPDF001
{
class Program
{
static void Main(string[] args)
{
BaseFont bfTimesRoman = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font fTimesRomanItalic10 = new Font(bfTimesRoman, 10, Font.ITALIC, BaseColor.BLACK);
String testPDFName = "CPPDF001.pdf";
if (File.Exists(testPDFName))
{
File.Delete(testPDFName);
}
using (FileStream fs = new FileStream(testPDFName, FileMode.Create))
{
Document doc = new Document(PageSize.A4, 25,25,30,30);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
PdfContentByte cb = writer.DirectContent;
cb.SetFontAndSize(bfTimesRoman, 10);
cb.BeginText();
writeText(cb, "CodeProject Is Neat !!!", 30, 718, bfTimesRoman, 14);
cb.EndText();
doc.Close();
}
String testPDFNameStamped = "CPPDF001-Stamped.pdf";
if (File.Exists(testPDFNameStamped))
{
File.Delete(testPDFNameStamped);
}
PdfReader reader = new PdfReader(testPDFName);
using (System.IO.FileStream fs = new System.IO.FileStream(testPDFNameStamped, System.IO.FileMode.Create))
{
PdfStamper pdfStamper = new PdfStamper(reader, fs);
PdfContentByte cb = pdfStamper.GetOverContent(1);
cb.BeginText();
PdfPTable table1 = new PdfPTable(1);
table1.TotalWidth = 100f;
PdfPCell cell1 = new PdfPCell();
var chunk1 = new Chunk("CodeProject", fTimesRomanItalic10);
chunk1.SetAnchor("http://www.codeproject.com");
cell1.AddElement(chunk1);
table1.AddCell(cell1);
table1.WriteSelectedRows(0, -1, 30, 646, cb);
cb.EndText();
pdfStamper.FormFlattening = true;
pdfStamper.Close();
}
}
static void writeText(PdfContentByte cb, string text, int x, int y, BaseFont font, int size)
{
cb.SetFontAndSize(font, size);
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0);
}
}
}