2016-11-21 1 views
0

J'essaie d'ajouter un formulaire à un pdf en utilisant iText 7.iText7 setValue méthode ne fonctionne pas

Je continue d'obtenir une erreur lorsque j'essaie de définir la valeur du champ. Je n'ai pas pu trouver d'informations à partir du documentation de la méthode addKid(). Est-ce que quelqu'un sait comment contourner cette erreur?

Voici un échantillon du code J'utilise:

PdfTextFormField confField = PdfFormField.createText(pdf); 
confField.setFieldName(fieldName); 

PdfWidgetAnnotation confCoverAnnot = new PdfWidgetAnnotation(new Rectangle(x, y, width, height)); 
PdfWidgetAnnotation confAnnot = new PdfWidgetAnnotation(new Rectangle(x2, y2, width2, height2)); 

for (int i = 1; i<= numPages; i++) { 
    switch(i) { 
     case 1: 
      pdf.getPage(i).addAnnotation(confCoverAnnot); 
      break; 
     default: 
      pdf.getPage(i).addAnnotation(confAnnot); 
      break; 
    } 
} 


/* 
    Trying to have two different annotations reference the same field value. 

    Upon using the `setValue()` method, I get: object.must.be.indirect.to.work.with.this.wrapper 
    Any way to get this to work properly? 
*/ 
form.addField(confField); 
confField.addKid(confCoverAnnot); 
confField.addKid(confAnnot); 
if (value.equals("") != true) { 
    confField.setValue(value); //error here 
} 

Répondre

2

Je présume que l'erreur que vous obtenez est ce PdfException: Exception dans le thread « principal » com.itextpdf.kernel.PdfException: L'objet doit être indirect pour travailler avec ce wrapper`?

La solution est de marquer des annotations comme indirectes après leur création:

PdfWidgetAnnotation confCoverAnnot = new PdfWidgetAnnotation(new Rectangle(x, y, width, height)); 
confCoverAnnot.makeIndirect(pdf); 
PdfWidgetAnnotation confAnnot = new PdfWidgetAnnotation(new Rectangle(x, y, width, height)); 
confAnnot.makeIndirect(pdf); 

Explication: Lors du réglage des valeurs de champs de formulaire dans iText7, il attend les annotations d'être des objets indirects et déclenche une exception quand ils ne le sont pas. Depuis PdfWidgetAnnotation est créé indépendamment du PdfDocument, le lien doit être spécifié explicitement en appelant makeIndirect()

+0

Merci pour la clarification. Fait sens maintenant :) – Elliot