In the code below, I am attempting to place a layer of text over existing text (the original existing text is "hidden" by writing it in white font; it is serving as a placeholder for this replacement process). The code below is very heavily reliant on code provided by Chris Haas in this post:
However, the "left" position for my text placement is fine, but the "bottom" is lower than the text I'm trying to overwrite (even though the font is the same). How can I alter this to get the correct coordinates of the text in the original document? The original document was created using tables for layout, so does that impact this "overwrite" process?
byte[] content;
string tmppath = @"C:\Junk\MSE_1.pdf";
using (MemoryStream output = new MemoryStream())
{
PdfReader pdf_rdr = new PdfReader(tmppath);
PdfStamper stamper = new PdfStamper(pdf_rdr, output);
for (int i = 1; i < pdf_rdr.NumberOfPages; i++)
{
//Create an instance of our strategy
var t = new MyLocationTextExtractionStrategy("stuff");
if (t != null)
{
//Parse the page of the document above
//(if the text was found)
var ex = PdfTextExtractor.GetTextFromPage(pdf_rdr, i, t);
//Loop through each chunk found
foreach (var p in t.myPoints)
{
Console.WriteLine(string.Format("Found text {0} at {1} x {2} on page {3}", p.Text, p.Rect.Left, p.Rect.Bottom, i.ToString()));
PdfContentByte pcb = stamper.GetOverContent(i);
pcb.BeginText();
try
{
BaseFont bf = BaseFont.CreateFont(@"C:\Junk\FontFiles\georgia.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
pcb.SetFontAndSize(bf, 10);
pcb.SetTextMatrix(p.Rect.Left, p.Rect.Bottom);
//pcb.SetTextRise(3);
pcb.ShowText("STUFF");
}
finally
{
pcb.EndText();
}
}
}
}
// Set the flattening flag to true, as the editing is done
stamper.FormFlattening = true;
// close the pdf stamper
stamper.Close();
//close the PDF reader
pdf_rdr.Close();
//put the output into the byte array
content = output.ToArray();
}
//write the content to a PDF file
using (FileStream fs = File.Create(@"C:\Junk\MSE_REPLACED.pdf"))
{
fs.Write(content, 0, (int)content.Length);
fs.Flush();
}