使 table 的 editable 单元格只读具有带 itext 的滚动条

Making editable cells of a table readonly having Scroll Bar with itext

请找到下面的代码。

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);
    }
}

请运行以上代码并生成文档。我使用了 itext-1.3.jar 但与 itext-5.3.5.jar.

显示的行为相同

在名为 "text_filled.pdf" 的第二个文件中,我在 table 中有四个 pdf 单元格(字段)。我的代码使这些 editable 字段只读,但我也想要滚动条(当内容超过字段限制时),因为它只是第四个,这样用户就可以查看整个内容而无需编辑权限。

我能否为 table.

的每个单元格设置带滚动条的只读模式(如果内容超过文本字段的限制)

我也尝试了下面的代码来使字段只读。

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);

如果无法通过这些代码完成,则可以使用任何其他可能的解决方案。

我试过你的例子,我发现你遇到的行为是由 Adob​​e Acrobat / Reader 中的错误引起的。当不同文本字段的小部件注释的边框重叠时会出现该错误。

一旦我确定不同字段之间没有重叠,滚动条就出现了。

我如何确保没有重叠?只需更改在 cellLayout() 方法中创建 TextField 实例的方式即可:

Rectangle rect = new Rectangle(
    rectangle.getLeft(), rectangle.getTop() - 1,
    rectangle.getRight(), rectangle.getBottom() + 1);
TextField text = new TextField(writer, rect, String.format("text1_%s",tf));

现在,定义文本字段的矩形不再重叠,您也不会再遇到 Adob​​e Acrobat / Reader 错误。