首页 文章

当我在文档中添加PdfPCell时,短语在PdfPCell中不起作用

提问于
浏览
0

我从原始PDF创建一个新的PDF . 我尝试在PdfPCell中添加Phrase . 图书馆说,当你把文字放在PdfPCell时,你必须使用短语 .

我需要这样做,因为我需要一个位置和单元格中的文本 . 我有这个代码 .

mPdfFileOutPut = new File(Environment.getExternalStorageDirectory(),
            outPutFileStringBuilder.toString());
    Document document = new Document();

    try {
        PdfWriter.getInstance(document, new FileOutputStream(mPdfFileOutPut));
        document.open();


        FileInputStream is = new FileInputStream(mPdfFile);
        // Create a reader to extract info
        PdfReader pdfReader = new PdfReader(is);

        // Get the fields from the reader (read-only!!!)
        AcroFields form = pdfReader.getAcroFields();



        Set<String> fields = form.getFields().keySet();


        PdfPCell cell = new  PdfPCell( new Phrase("string"));
        for (String key : fields) {
            cell.setBackgroundColor(BaseColor.BLUE);

            cell.addElement(new Paragraph("ddfdelellelle"));
            cell.setLeft(form.getFieldPositions(key).get(0).position.getLeft());
            cell.setRight(form.getFieldPositions(key).get(0).position.getRight());
            cell.setTop(form.getFieldPositions(key).get(0).position.getTop());
            cell.setBottom(form.getFieldPositions(key).get(0).position.getBottom());
            document.add(cell);

        }
        document.close();


    } catch (Exception e) {
        e.printStackTrace();

    }

任何的想法?我希望有一个解决方案 .

1 回答

  • 2

    如果你看一下你链接到的API documentation,你会看到它说:PdfPTable中的一个单元格 . 表格单元格需要成为表格的一部分 .

    但看起来你根本不想使用表格 . 如果我正确理解了代码的意图,您希望创建一个新的PDF文档,其文本内容与输入PDF中的表单字段位于相同的位置 . 请注意,您没有考虑多个页面:如果输入表单的字段超过1页,您将在输出文档的1页上添加所有内容 . 您也没有考虑页面大小 .

    看一下方便方法 ColumnText.showTextAligned() 来添加一个具有绝对定位的 Phrase . 这是一个例子:http://itextpdf.com/examples/iia.php?id=61

    如果您正在尝试制作表单的非交互式版本,请阅读表单展平 .

相关问题