sigil
sigil

Reputation: 9546

Trying to add a PDF stamp using iTextSharp, "The byte array is not a recognized imageformat"

I want to add a stamp to a PDF, where the file for the stamp is itself a PDF. Here's my code:

void addImage(string inputPath,string imagePath,string outputPath,int pageNumber)
{ 
    Stream inputImageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
    PdfReader pdfReader = new PdfReader(inputPath);
    PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(outputPath, FileMode.Create));
    PdfContentByte pdfContentByte = pdfStamper.GetOverContent(pageNumber);
    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);
    image.SetAbsolutePosition(100, 100);
    pdfContentByte.AddImage(image);
}

void addImageTest()
{
    string sourceFile = @"C:\somefolder\source.pdf";
    string stampFile = @"C:\somefolder\stamp.pdf";
    string destFile = @"C:\somefolder\destination.pdf";
    addImage(sourceFile, stampFile, destFile, 1);
    return;
}

On the call to GetInstance(), I get this exception:

The byte array is not a recognized imageformat.

Update: looking at the source code for GetInstance(), I see that it works with GIF, TIFF, JPEG, PNG, WMF, and BMP, but not PDF. So I need to figure out another way to use a PDF as an image, I think. Any ideas?

Upvotes: 3

Views: 10719

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

A PDF is a document format, not an image format, hence Image.GetInstance() doesn't accept PDF bytes. That doesn't mean there is no workaround.

A PDF document can be read by the PdfReader object:

PdfReader reader = new PdfReader(src);

You can reuse pages from the PDF that is being read by using the PdfImportedPage object:

PdfImportedPage page = writer.GetImportedPage(reader, 1);

In the line above, writer is the PdfWriter instance of the document you're creating. The GetImportedPage method copies resources needed for page to the new file. Resources could be fonts, images,...

PdfImportedPage extends PdfTemplate. PdfTemplate is the class used for Form XObject. Raster images are stored as external objects called Image XObjects. Vector data (e.g. WMF, an imported PDF page, a stream of PDF syntax) is stored as Form XObjects.

You now have two different options to add that PdfTemplate (or PdfImportedPage) named page to the document.

  1. you can use the AddTemplate() method: writer.DirectContent.AddTemplate(page, x, y);
  2. you can wrap the PdfTemplate inside an Image object: iTextSharp.text.Image Img = iTextSharp.text.Image.GetInstance(page);

Note that, whichever option you choose, the PDF will never be converted to a raster image. It will be added as vector data.

Update:

In your case, you have to replace writer like this:

  • pdfStamper.GetImportedPage(reader, 1);
  • pdfStamper.GetOverContent(pageNumber).AddTemplate(page, x, y);

My examples were made in the case you create a PDF from scratch.

Upvotes: 2

Related Questions