Your code works for me (I don’t have your SavePDF function . of course).
Suggestions:
1. Are you scaling it to a tiny size (you didn’t include the values you use)?
2. Are you positioning it off the page (you didn’t include the values you use)?
3. You’re using GetUnderContent, not GetOverContent. Since the content ot this
file is an opaque image, this causes the image you’re adding to be hidden
under it.
- Eric
From: 王义 [mailto:[email protected]]
Sent: Wednesday, April 11, 2012 10:18 AM
To: [email protected]
Subject: [iText-questions] Could not able to add image to some pdf
Hello veryone,I'm a c# Programmer from china. i want to insert a image to the
existing documents.
I found could not able to add image to some pdf,for example the attached file,
but other files can added.
the flow is my code :
public static bool SignImg(string localFilePath, string imgPath, string
imgFitWidth,string imgFitHeight,string SignX,string SignY)
{
if (!File.Exists(localFilePath) || !File.Exists(imgPath))
{
return false;
}
try
{
Stream pdfStream = new FileStream(localFilePath,
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
PdfReader reader = new PdfReader(pdfStream);
var stamper = new PdfStamper(reader, pdfStream);
int iPageNums = reader.NumberOfPages;
for (int i = 1; i < iPageNums + 1; i++)
{
var pdfConvertByte2 = stamper.GetUnderContent(i);
if (pdfConvertByte2 == null)
continue;
Rectangle rectanle = reader.GetPageSize(i);
float YMax = rectanle.Height;
Image img = Image.GetInstance(imgPath);
img.ScaleToFit(float.Parse(imgFitWidth),
float.Parse(imgFitHeight));
img.SetAbsolutePosition(float.Parse(SignX), YMax -
float.Parse(SignY));
pdfConvertByte2.AddImage(img);
}
stamper.Close();
reader.Close();
pdfStream.Close();
if (!SavePDF(localFilePath))
{ return false;}
return true;
}
catch (Exception e)
{
return false;
}
}
Thank you for your help!
------------------------------------------------------------------------------
Better than sec? Nothing is better than sec when it comes to
monitoring Big Data applications. Try Boundary one-second
resolution app monitoring today. Free.
http://p.sf.net/sfu/Boundary-dev2dev
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php