DJ Quimby
DJ Quimby

Reputation: 3699

Copy Form Fields From One PDF to Another

I have a situation where I need to copy all of the form fields from one PDF to another. The purpose is to automate the overlaying of the fields when small edits are made to the underlying Word pages.

I've been using the trial version of Aspose.Pdf.Kit, and I'm able to copy everything but Radio buttons to a new form. However Aspose doesn't support copying the radio buttons, which completely nullifies it's usefulness, not to mention their customer support has been subpar.

In any event, I'm looking for some sort of library or plug-in that does support copying all types of form fields.

Does anyone have any ideas?

Thanks,

~DJ

Upvotes: 3

Views: 4436

Answers (2)

Mark Storer
Mark Storer

Reputation: 15868

Yes, it is possible. No, setField() won't do the trick... madisonw's code will copy the field values, but not the fields themselves.

OTOH, it really isn't that hard.

Something like:

PdfReader currentReader = new PdfReader( CURRENT_PDF_PATH ); // throws
PdfReader pdfFromWord = new PdfReader( TWEAKED_PDF_FROM_WORD_PATH ); // throws
PdfStamper stamper = new PdfStamper( currentReader , outputFile ); //throws
for( int i = 1; i <= tempalteReader.getNumberOfPages(); ++i) {
  stamper.replacePage( pdfFromWord, i, i );
}

stamper.close(); // throws 

I'm ignoring a bunch of exceptions, and am writing in Java, but C# should look virtually identical.

Also, this code ignores the case where someone ADDS A PAGE... which would get quite thorny. Was it added before or after the pages with fields on them? Did those pages reflow at all, requiring you to move the fields? At that point you really need a manual process with Acrobat Pro.

Upvotes: 4

madisonw
madisonw

Reputation: 836

I agree with Oded, iTextSharp should be able to do the job. I've used code similar the following snippet and never had problems with any field types. I'm sure there must have been a radio button in the mix.

private void CopyFields(PdfStamper targetFile, PdfReader sourceFile){
{
  foreach (DictionaryEntry de in targetFile.AcroFields.Fields)
  {
    string fieldName = de.Key.ToString();
    target.AcroFields.SetField(fieldName, sourceFile.AcroFields.GetField(fieldName));
  }
}

Upvotes: 0

Related Questions