itextsharp stamping form pdf onto other pdf

495 Views Asked by At

I'm using itextsharp in vb.net to stamp some backgrounds (ie other single page pdfs) onto another pdf.

So I have a 5 page 'blank' pdf, on which I stamp page 1 with the first page from a file called page1.pdf, then I put the first page of page2.pdf as a background to page 2 etc etc.

It's worked fine so far, but I've come across a problem with stamping a particular pdf onto my 'blank' - the issue appears to be with a file I'll call 'page4.pdf' and it seems likely it's because page4.pdf has been designed as a fillable form.

When I stamp page4 on and open up the blank file in Adobe reader, I get the message:

There was an error processing a page. There was a problem reading this document (18)

Could anyone suggest a way I can stamp a pdf with a form pdf as the source without this issue?

Thanks!


Here's an extract from the code I'm using for stamping (it does other stuff and involves a loop to go through the pages of the pdf, but I've just put the actual stamp bit below to keep things simple):

Dim background As PdfContentByte
Dim page As PdfImportedPage = Nothing
Dim reader As PdfReader = New PdfReader(sourcepdf)
Dim stamper As New itextsharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputpdf, System.IO.FileMode.Create))
Dim s_reader As New PdfReader(backfile)
    
    
page = stamper.GetImportedPage(s_reader, 1)
    
background.AddTemplate(page, 0, 0)
    
stamper.Close()
reader.Close()
s_reader.Close()

Here's the code I've been trying out to convert the form pdf to a 'non-form', which I was hoping would eliminate the stamping problem (no success so far):

Dim pdfReader As PdfReader = New PdfReader(inputpdf)

Dim pdfStamper As itextsharp.text.pdf.PdfStamper = New PdfStamper(pdfReader, New FileStream(outputpdf, FileMode.Create))

pdfStamper.AnnotationFlattening = True
pdfStamper.FreeTextFlattening = True
pdfStamper.FormFlattening = True

pdfStamper.Close()
1

There are 1 best solutions below

0
On

Issue was nothing to do with the pdf being a form, but a problem that seems to crop up when I repeatedly stamp the same output file. Solution is just to explicity delete the former output file before re-running process.