When a barcode image is placed on the pdf using stamper in this manner:
PdfContentByte page = stamper.GetOverContent(i);
image.SetAbsolutePosition(x, y);
page.AddImage(image);
it displays properly when the PDF is rendered in a viewer, but it is not being found by the code below (adapted from here). The code simply doesn't recognize it as existing. The code finds an image that was placed in the Pdf by Acrobat Pro XI, but not the one added in the above manner.
What is the proper way to place a barcode image on a pdf in iTextSharp such that the image will be included in the PdfDictionary? What needs to be changed, the code above, or the code below?
for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
{
PdfDictionary pg = pdf.GetPageN(pageNumber);
PdfObject obj = FindImageInPDFDictionary(pg);
if (obj != null)
{
int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
PdfStream pdfStrem = (PdfStream)pdfObj;
byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem);
if ((bytes != null))
{
using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes))
{
memStream.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);
// now we have an image and can examine it
// to see if it is a barcode
}
}
}
}