Please find the below code.
public class MakingFieldReadOnly implements PdfPCellEvent {
/** The resulting PDF. */
public static final String RESULT1 = "text_fields.pdf";
/** The resulting PDF. */
public static final String RESULT2 = "text_filled.pdf";
/** The text field index of a TextField that needs to be added to a cell. */
protected int tf;
public static final String CONTENT = "Write any thing so that it exceeds the content limit of the textfield and scroll bar comes. asdadasdasdasdasdasdasdasdasddlfjklfjljdflkjasdfjasdfjsldfjlsdjflsjdfljdflkjsdfljsldfjlsdjflskdfjlskdfjlsdjflskdjflksdjflksdjflkjsdflkjsdfljsdfkljsdlfjlsdjkfasdadasdasdasdasdasdasdasdasddlfjklfjljdflkjasdfjasdfjsldfjlsdjflsjdfljdflkjsdfljsldfjlsdjflskdfjlskdfjlsdjflskdjflksdjflksdjflkjsdflkjsdfljsdfkljsdlfjlsdjkfasdadasdasdasdasdasdasdasdasddlfjklfjljdflkjasdfjasdfjsldfjlsdjflsjdfljdflkjsdfljsldfjlsdjflskdfjlskdfjlsdjflskdjflksdjflksdjflkjsdflkjsdfljsdfkljsdlfjljkf";
/**
* Creates a cell event that will add a text field to a cell.
* @param tf a text field index.
*/
public MakingFieldReadOnly(int tf) {
this.tf = tf;
}
/**
* Manipulates a PDF file src with the file dest as result
* @param src the original PDF
* @param dest the resulting PDF
* @throws IOException
* @throws DocumentException */
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.setField("text1_1", CONTENT);
form.setField("text1_2", CONTENT);
form.setField("text1_3", CONTENT);
form.setField("text1_4", CONTENT);
form.setFieldProperty("text1_1","setfflags",TextField.READ_ONLY , null);
form.setFieldProperty("text1_2","setfflags",TextField.READ_ONLY , null);
form.setFieldProperty("text1_3","setfflags",TextField.READ_ONLY , null);
form.setFieldProperty("text1_4","setfflags",TextField.READ_ONLY , null);
stamper.close();
//reader.close();
}
/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename) throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
PdfPCell cell;
PdfPTable table = new PdfPTable(2);
table.setWidths(new int[]{ 1, 2 });
table.addCell("Name:");
cell = new PdfPCell();
cell.setCellEvent(new MakingFieldReadOnly(1));
cell.setFixedHeight(60);
table.addCell(cell);
table.addCell("Loginname:");
cell = new PdfPCell();
cell.setCellEvent(new MakingFieldReadOnly(2));
cell.setFixedHeight(60);
table.addCell(cell);
table.addCell("Password:");
cell = new PdfPCell();
cell.setCellEvent(new MakingFieldReadOnly(3));
cell.setFixedHeight(60);
table.addCell(cell);
table.addCell("Reason:");
cell = new PdfPCell();
cell.setCellEvent(new MakingFieldReadOnly(4));
cell.setFixedHeight(60);
table.addCell(cell);
document.add(table);
// step 5
document.close();
}
/**
* Creates and adds a text field that will be added to a cell.
* @see com.itextpdf.text.pdf.PdfPCellEvent#cellLayout(com.itextpdf.text.pdf.PdfPCell,
* com.itextpdf.text.Rectangle, com.itextpdf.text.pdf.PdfContentByte[])
*/
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
PdfWriter writer = canvases[0].getPdfWriter();
TextField text = new TextField(writer, rectangle, String.format("text1_%s",tf));
text.setBackgroundColor(new GrayColor(0.75f));
text.setOptions(TextField.MULTILINE | TextField.REQUIRED);
text.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
text.setFontSize(8);
try {
PdfFormField field = text.getTextField();
writer.addAnnotation(field);
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
catch(DocumentException de) {
throw new ExceptionConverter(de);
}
}
/**
* Main method
* @param args no arguments needed
* @throws IOException
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException, IOException {
MakingFieldReadOnly example = new MakingFieldReadOnly(0);
example.createPdf(RESULT1);
example.manipulatePdf(RESULT1, RESULT2);
}
}
Please run the above code and generate the document. I have used itext-1.3.jar but same behavior shown with itext-5.3.5.jar.
In the second file named as "text_filled.pdf", I have four pdf cells(fields) in the table. My code is making these editable fields read only but I want scroll bar also(when content exceeds the field limit) as like it is coming for 4th one only, so that user can be able to view whole content without having edit permission.
Could I get read only mode with scroll bar(if content is more than limit of the text field) for each cell of the the table.
I have tried the below code also for making the fields read only.
form.setFieldProperty("text1_1","setfflags",PdfFormField.FF_READ_ONLY , null);
form.setFieldProperty("text1_2","setfflags",PdfFormField.FF_READ_ONLY , null);
form.setFieldProperty("text1_3","setfflags",PdfFormField.FF_READ_ONLY, null);
form.setFieldProperty("text1_4","setfflags",PdfFormField.FF_READ_ONLY , null);
If from these codes can't be done then any other possible solution.