com.justformspdf.pdf
Class PDF

java.lang.Object
  |
  +--com.justformspdf.pdf.PDF

public class PDF
extends java.lang.Object

The PDF class is responsible for accessing PDF forms, rendering form fields and writing generated PDF to an output stream. In a typical program following is the sequence of operations:

To make the API calls simple and efficient there are only a few public methods available to the users of this class.

Here is an example showing how PDF object should be constructed.

 
 FileInputStream fis = new FileInputStream("C:\\JustFormsPDF\\examples\\AddressForm32A2.pdf");                          
 PDF pdf = new PDF(new PDFReader(fis));


 FormText text = (FormText) form.getElement("Name");
 text.setValue("John K. Baker");


 pdf.render();
 pdf.writeTo(new FileOutputStream("C:\\JustFormsPDF\\examples\\AddressForm32A2_out.pdf"));
  
 


Constructor Summary
PDF(com.justformspdf.pdf.PDFReader reader)
          Create PDF object from the specified PDFReader object.
 
Method Summary
 com.justformspdf.pdf.Form getForm()
          Return the parsed PDF AcroForm or Form object containing all the form elements.
 int getSize()
          Get the size of modified/updated PDF file in bytes
 boolean isSaveProtected()
          Get the "Save Protected" flag.
 void render()
          This method renders the PDF document with new or modified form elements.
 void setSaveProtected(boolean b)
          Set the "Save Protected" flag.
 void writeTo(java.io.OutputStream os)
          This method writes the updated/modified PDF contents to an OutputStream The output stream can be a File stream or Servlet (to browser) stream.
  

Constructor Detail

PDF

public PDF(com.justformspdf.pdf.PDFReader reader)
Create PDF object from the specified PDFReader object.

Here is an example showing how PDF object should be constructed.

 
 FileInputStream fis = new FileInputStream("C:\\JustFormsPDF\\examples\\AddressForm32A2.pdf");                          
 PDF pdf = new PDF(new PDFReader(fis));
 
 

Parameters:
reader - the PDFReader object
Method Detail

render

public void render()
            throws java.lang.Exception
This method renders the PDF document with new or modified form elements. During rendering the appearance characteristics, back-ground color, read-only attribute etc are also applied to elements. The rendered bytes are then sent to output stream.

Here is an example showing the user of render() method.

//... //... FormText text = (FormText) form.getElement("Name"); text.setValue("John K. Baker"); pdf.render(); pdf.writeTo(new FileOutputStream("C:\\JustFormsPDF\\examples\\AddressForm32A2_out.pdf")); //... //...

Throws:
java.lang.Exception

writeTo

public void writeTo(java.io.OutputStream os)
             throws java.lang.Exception
This method writes the updated/modified PDF contents to an OutputStream The output stream can be a File stream or Servlet (to browser) stream.

Here is an example showing how a PDF should be streamed to a file.

 
 pdf.render();          
 pdf.writeTo(new FileOutputStream("C:\\JustFormsPDF\\examples\\AddressForm32A2_out.pdf"));
 

Here is an example showing how a PDF should be streamed to a browser (using Servlet's HttpResponse object)

 pdf.render();
 resp.setContentType("application/pdf");        
 pdf.writeTo(resp.getOutputStream());                   
 

Parameters:
os - the output stream
Throws:
java.lang.Exception

getForm

public com.justformspdf.pdf.Form getForm()
Return the parsed PDF AcroForm or Form object containing all the form elements. These form elements are later modified by applying setValue (or similar) operations on individual elements as desired.

Here is an example showing how PDF Form object should be accessed and how its elements should be filled or modified.


 FileInputStream fis = new FileInputStream("C:\\JustFormsPDF\\examples\\AddressForm32A2.pdf");                          
 PDF pdf = new PDF(new PDFReader(fis));
 Form form = pdf.getForm();
                
 FormText name = (FormText) form.getElement("Name");
 name.setValue("John K. Baker");
        
 FormCheckbox addressType = (FormCheckbox) form.getElement("AddressTypePerm");
 addressType.check(true);
        
 FormComboBox suffix = (FormComboBox) form.getElement("Suffix");
 //suffix.setValue("5");
 suffix.setValueByIndex(5);
 
 FormRadioGroup ethnic = (FormRadioGroup) form.getElement("ethnic");
 //ethnic.setValue("ETH02");
 ethnic.setValueByIndex(2);
 
 

Returns:
Form

getSize

public int getSize()
Get the size of modified/updated PDF file in bytes

Returns:
number of bytes

isSaveProtected

public boolean isSaveProtected()
Get the "Save Protected" flag.

This save-protected feature can be used in a browser based application to restrict users from saving the generated PDFs.

Returns:
save-proteced flag (boolean)
See Also:
setSaveProtected(boolean)

setSaveProtected

public void setSaveProtected(boolean b)
Set the "Save Protected" flag.

This feature can be used in a browser based application to make generated PDFs save-protected. If you want your application user just view the PDF and do not save it you may try this feature. Some advanced user may still 'hack' this feature by altering the contents of generated PDF by opening it into a text or hex editor but for most of the users this feature will work fine.

Here is an example showing the user of render() method.

 
 //...
 FileInputStream fis = new FileInputStream("C:\\JustFormsPDF\\examples\\AddressForm32A2.pdf");                          
 PDF pdf = new PDF(new PDFReader(fis));
 //...
 //...
 pdf.setSaveProtected(false);
 //...
 //...