private static void InsertTextToPdf(string sourceFileName, string newFileName)
{
using (Stream pdfStream = new FileStream(sourceFileName, FileMode.Open))
using (Stream newpdfStream = new FileStream(newFileName, FileMode.Create, FileAccess.ReadWrite))
{
PdfReader pdfReader = new PdfReader(pdfStream);
PdfStamper pdfStamper = new PdfStamper(pdfReader, newpdfStream);
PdfContentByte pdfContentByte = pdfStamper.GetOverContent(1);
BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
pdfContentByte.SetColorFill(BaseColor.BLUE);
pdfContentByte.SetFontAndSize(baseFont, 8);
pdfContentByte.BeginText();
pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Kevin Cheng - A Hong Kong actor", 400, 600, 0);
pdfContentByte.EndText();
pdfStamper.Close();
}
}
This code is working fine to add text in a PDF , but i want to add the same text on below of every image in PDF.
I tried with merging the code of image extraction with text add but not able to see the same text multiple time , while able to see multiple image extraction. I added the text in same loop where extracting the image .
public static Dictionary<string, System.Drawing.Image> ExtractImages(string filename, string sourceFileName, string newFileName)
{
var images = new Dictionary<string, System.Drawing.Image>();
Stream newpdfStream = new FileStream(newFileName, FileMode.Create, FileAccess.ReadWrite);
using (var reader = new PdfReader(filename))
{
//
PdfReader pdfReader = new PdfReader(reader);
PdfStamper pdfStamper = new PdfStamper(pdfReader, newpdfStream);
PdfContentByte pdfContentByte = pdfStamper.GetOverContent(1);
var parser = new PdfReaderContentParser(reader);
ImageRenderListener listener = null;
for (var i = 1; i <= reader.NumberOfPages; i++)
{
parser.ProcessContent(i, (listener = new ImageRenderListener()));
var index = 1;
if (listener.Images.Count > 0)
{
foreach (var pair in listener.Images)
{
images.Add(string.Format("{0}_Page_{1}_Image_{2}{3}",
System.IO.Path.GetFileNameWithoutExtension(filename), i.ToString("D4"), index.ToString("D4"), pair.Value), pair.Key);
index++;
{
BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
pdfContentByte.SetColorFill(BaseColor.BLUE);
pdfContentByte.SetFontAndSize(baseFont, 8);
pdfContentByte.BeginText();
pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Kevin Cheng - A Hong Kong actor", 400 + index * 10, 600, 0);
pdfContentByte.EndText();
}
}
}
}
pdfStamper.Close();
return images;
}
}