如何将图片和文字放在下方/上方?

我尝试了以下代码,但只能看到“测试1”和“测试3”

private void createTable(Paragraph paragraph, Document document){
    PdfPTable table = new PdfPTable(2);

    PdfPCell c1 = new PdfPCell(new Phrase("Question"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Answer"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    table.setHeaderRows(1);

    HierarchyElement formElement;
    FormController formController = Collect.getInstance().getFormController();
    Image imageAnswer = null;

    for(int i = 0; i < mFormList.size(); i++){
        formElement = mFormList.get(i);

        table.addCell(formElement.getPrimaryText());

        String imageName = getImageNameOfSelectedAnswer(formController, formElement);
        if(imageName != null){
            imageAnswer = Image.getInstance(imageName);
            imageAnswer.scalePercent((float) 0.1);
        }

        PdfPCell cell = new PdfPCell();
        cell.addElement(new Paragraph("Test 1"));
        cell.addElement(new Chunk(imageAnswer, 0, 0, true));
        cell.addElement(new Paragraph("Test 3"));
        table.addCell(cell);
    }

    paragraph.add(table);
    document.add(paragraph);
}

最佳答案

您应该尝试这样:

PdfPTable table = new PdfPTable(2);
PdfPCell cell = new PdfPCell();
Paragraph paragraph1 = new Paragraph();
paragraph1.add(textAnswer);
cell.addElement(paragraph1);
cell.addElement(new Paragraph("Test 1"));
cell.addElement(imageAnswer);
cell.addElement(new Paragraph("Test 3"));
table.addCell(cell);


如果那行不通,请提供一个SSCCE来重现Java中的问题。

关于android - 如何将图像和文本放在iText Android表的1个单元格中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18715101/

10-12 21:09