Reputation: 335
I have this Code that adds image to a PDF:
string SRC =
@"C:/Saved/Test.pdf";
string DEST = @"C:/Saved/TestComplete.pdf";
string IMG = @"C:Saved//TestImage.JPG";
Document doc = new Document();
try
{
iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(SRC, FileMode.Create));
doc.Open();
//doc.Add(new Paragraph("GIF"));
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(IMG);
image.ScalePercent(200f);
doc.Add(image);
}
catch (Exception ex)
{
//Log error;
string error = ex.Message;
}
finally
{
doc.Close();
}
}
The Problem is here that its not just adding the image its replacing the whole PDF with that image is there a way to add a image in PDF just a signature image just add that to the page Any Idea ? Also I have upgraded the Itextsharp to IText7 but I couldn't find a way to add image to a existing PDF only there are water marks. If you know a example Link or article on it please let me know.
Upvotes: 0
Views: 425
Reputation: 568
If you want to use the old method then use this:
string SRC =
@"C:/Saved/Test.pdf";
string DEST = @"C:/Saved/TestComplete.pdf";
string IMG = @"C:Saved//TestImage.JPG";
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(SRC);
iTextSharp.text.Rectangle Size = reader.GetPageSizeWithRotation(1);
Document document = new Document(Size);
FileStream fs = new FileStream(DEST, FileMode.Create, FileAccess.Write);
iTextSharp.text.pdf.PdfWriter weiter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, fs);
document.Open();
PdfContentByte cb = weiter.DirectContent;
PdfImportedPage page = weiter.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(IMG);
document.Add(jpg);
document.Close();
fs.Close();
weiter.Close();
reader.Close();
For your question to use the iText7 method plesae refer to this link IText7 JumpStart
and to be exactly where you can find an example of dealing with images Please refer to this Chapter7
I also recommend that you read all the chapters
Upvotes: 1