inset a text into an existing pdf
private void button4_Click(object sender, EventArgs e)
{
String sourceFileName = "D:\\reader2.pdf";
String newFileName = "E:\\" + DateTime.Now.Millisecond + ".pdf";
InsertTextToPdf(sourceFileName, newFileName);
}
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, "Anil", 400, 600, 0);
pdfContentByte.EndText();
pdfStamper.Close();
}
}
{
String sourceFileName = "D:\\reader2.pdf";
String newFileName = "E:\\" + DateTime.Now.Millisecond + ".pdf";
InsertTextToPdf(sourceFileName, newFileName);
}
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, "Anil", 400, 600, 0);
pdfContentByte.EndText();
pdfStamper.Close();
}
}
Comments