You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Venkatesh Prasad <ve...@trv.envestnet.com> on 2009/05/01 18:26:18 UTC

Setting Font Size and Type ...

Following is the code I have developed to set a value to a Form Field in a PDF document. What I was not able to do is to set the Font Size and Type before writing the value in the PDF document. Can someone help with an example?
 
 public static void main(String args[])
 {
  String PDFFileName = "Project2.PDF";
  String fieldName = "Text1";
        try {
         PDDocument document = PDDocument.load(PDFFileName);
         List list = document.getDocumentCatalog().getAcroForm().getFields();
         PDField pdfield;
   try {
    if (document.isEncrypted()) {
                 document.decrypt("");
    }
   } catch (Exception e) {
    e.printStackTrace();
    throw new IOException("Failed to decrypt document #");
   }
   for (int i = 0; i < list.size(); i++) {
    pdfield = (PDField) list.get(i);
    String fldName = pdfield.getPartialName();
    if (fldName.equals(fieldName)) {
     PDDocumentCatalog docCatalog = document.getDocumentCatalog();
     PDAcroForm acroForm = docCatalog.getAcroForm();
     PDField field = acroForm.getField(fldName);
     if (field != null) {
      // I want to Set the Font Type and Size of the text "04/29/2009"
      field.setValue("04/29/2009");
     } else {
      // Debug
      System.err.println("No field found with name:" + fldName);
     }
    }
   }
         document.save("test1.PDF");
   document.close();
  } catch (Exception e) {
   System.out.println("Error"+e);
  }
}
 
Thanks & Regards
 
Venkatesh Prasad.B.K.