Change logo of a pdf documment in every page
private void button2_Click(object sender, EventArgs e)
{
String sourceFileName = "D:\\reader2.pdf";
String imageFileName = "D:\\AxaltaByAnil.jpg";
String newFileName = "E:\\" + DateTime.Now.Millisecond + ".pdf";
OpenFileDialog dlg = new OpenFileDialog();
////string filepath;
dlg.Filter = "PDF Files(*.PDF)|*.PDF|All Files (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
sourceFileName = dlg.FileName.ToString();
InsertImageToPdf(sourceFileName, imageFileName, newFileName);
}
////InsertTextToPdf("asd", "jahlks");
}
private static void InsertImageToPdf(string sourceFileName, string imageFileName, string newFileName)
{
using (Stream pdfStream = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream imageStream = new FileStream(imageFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
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);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageStream);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
////PdfImportedPage pages = pdfStamper.GetImportedPage(pdfReader, page);
////int n = pdfReader.NumberOfPages;
////image.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
//// image.ScalePercent(50f);
if (page == 1)
{
pdfContentByte = pdfStamper.GetOverContent(page);
image.SetAbsolutePosition(48, 645);
}
else
{
pdfContentByte = pdfStamper.GetOverContent(page);
image.SetAbsolutePosition(70, 730);
}
pdfContentByte.AddImage(image);
}
pdfStamper.Close();
}
}
{
String sourceFileName = "D:\\reader2.pdf";
String imageFileName = "D:\\AxaltaByAnil.jpg";
String newFileName = "E:\\" + DateTime.Now.Millisecond + ".pdf";
OpenFileDialog dlg = new OpenFileDialog();
////string filepath;
dlg.Filter = "PDF Files(*.PDF)|*.PDF|All Files (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
sourceFileName = dlg.FileName.ToString();
InsertImageToPdf(sourceFileName, imageFileName, newFileName);
}
////InsertTextToPdf("asd", "jahlks");
}
private static void InsertImageToPdf(string sourceFileName, string imageFileName, string newFileName)
{
using (Stream pdfStream = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream imageStream = new FileStream(imageFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
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);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageStream);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
////PdfImportedPage pages = pdfStamper.GetImportedPage(pdfReader, page);
////int n = pdfReader.NumberOfPages;
////image.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
//// image.ScalePercent(50f);
if (page == 1)
{
pdfContentByte = pdfStamper.GetOverContent(page);
image.SetAbsolutePosition(48, 645);
}
else
{
pdfContentByte = pdfStamper.GetOverContent(page);
image.SetAbsolutePosition(70, 730);
}
pdfContentByte.AddImage(image);
}
pdfStamper.Close();
}
}
Comments