0

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()
Ash
  • 23
  • 4
  • Your code should work just fine. Thus, this likely is an issue of that pdf. Can you share that pdf for analysis? – mkl Feb 23 '21 at 05:36
  • Really? - well that's good news I guess. Here's a link to the file: https://drive.google.com/file/d/1FiQlv9XzWdfqruzyRUG8hy_pfKifFEiz/view?usp=sharing – Ash Feb 23 '21 at 08:48
  • One thing, though: When I said "Your code should work just fine" I assumed you do assign something sensible to `background` in your actual code; the excerpt in your question can never work as `background` is unassigned... – mkl Feb 23 '21 at 08:57
  • Which state of that PDF does the shared file represent? Before flattening or thereafter? I wonder because it does not contain any AcroForm form fields, but it doesn't look like a file flattened by iText either. – mkl Feb 23 '21 at 09:05
  • I think I've got to the bottom of this - I did another test this morning and it all worked OK. The issue was that I was continually running the stamp over and over while I was testing the code and not deleting the stamped pdf between each run. I assumed that the process would overwrite the output file, but it's just kept stamping over the top. As soon as deleted the output file between runs, there was no problem. I don't know why the process didn't like page4 in particular, which led me to think it was form-related, but that was a red herring and I just needed to start from 'fresh' each time. – Ash Feb 23 '21 at 09:44
  • To answer the earlier comment, the pdf was after I tried to flatten with the code I posted yesterday - hopefully this is now no longer needed. Thanks for the feedback on this - sometimes it just really helps to go through things with someone else, even if the answer is a silly mistake I made! – Ash Feb 23 '21 at 09:45
  • Well, great that it works now. But there still something is weird, you use `System.IO.FileMode.Create`, and that should already suffice, no deletion required... – mkl Feb 23 '21 at 11:37
  • Thanks for the confirm - it is odd, but now I come to think of it I have seen this happening before. I don't understand it, but can work with just deleting first if the output file already exists. – Ash Feb 25 '21 at 08:52
  • Is the path probably on some special device? E.g. some network file system? – mkl Feb 25 '21 at 11:30
  • Yes it's on a server accessed via assigned letter - would that make a difference? – Ash Feb 26 '21 at 08:52
  • It shouldn't. But sometimes it does. – mkl Feb 26 '21 at 09:27

1 Answers1

0

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.

Ash
  • 23
  • 4