加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何在彼此旁边显示图像和文本

发布时间:2020-12-15 05:12:05 所属栏目:Java 来源:网络整理
导读:嗨,我正在尝试创建一个发票,其中公司徽标和地址彼此相邻显示.公司徽标显示在左侧,文本显示在其下方.我试图将徽标旁边的文字显示在FAR RIGHT但它没有来.请帮忙. public class Test { /** Path to the resulting PDF */ public static final String RESULT = "
嗨,我正在尝试创建一个发票,其中公司徽标和地址彼此相邻显示.公司徽标显示在左侧,文本显示在其下方.我试图将徽标旁边的文字显示在FAR RIGHT但它没有来.请帮忙.

public class Test {
    /** Path to the resulting PDF */
    public static final String RESULT = "C:/ex/test.pdf";

    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException,DocumentException {
        new ComCrunchifyTutorials().createPdf(RESULT);
    }

    /**
     * Creates a PDF with information about the movies
     * @param    filename the name of the PDF file that will be created.
     * @throws    DocumentException 
     * @throws    IOException
     */
    public void createPdf(String filename)
        throws IOException,DocumentException {

        Document document = new Document();

        PdfWriter.getInstance(document,new FileOutputStream(filename));

        document.open();

       Image image = Image.getInstance("C:/Users/user/Desktop/New folder (3)/shoes/shoes/web/images/abc.jpg");
                                                        image.scaleAbsolute(150f,50f);//image width,height   


Paragraph p = new Paragraph();
Phrase pp = new Phrase(200);
p.add(new Chunk(image,0));
pp.add(" a text after the image.");

p.add(pp);
document.add(p);

        document.close();
    }

}

解决方法

人们通常使用PdfPTable来实现这一目标.例如,参见 ImageNextToText示例:

这是创建PDF的代码和包含两列的表:

public void createPdf(String dest) throws IOException,DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document,new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    table.setWidths(new int[]{1,2});
    table.addCell(createImageCell(IMG1));
    table.addCell(createTextCell("This picture was taken at Java One.nIt shows the iText crew at Java One in 2013."));
    document.add(table);
    document.close();
}

这是使用图像创建单元格的代码.请注意,true参数将导致图像缩放.由于我们已经定义第一列占第二列宽度的一半,因此图像的宽度将占整个表宽度的三分之一.

public static PdfPCell createImageCell(String path) throws DocumentException,IOException {
    Image img = Image.getInstance(path);
    PdfPCell cell = new PdfPCell(img,true);
    return cell;
}

这是使用文本创建单元格的一种方法.有许多不同的方法可以实现相同的结果.只需尝试文本模式和复合模式.

public static PdfPCell createTextCell(String text) throws DocumentException,IOException {
    PdfPCell cell = new PdfPCell();
    Paragraph p = new Paragraph(text);
    p.setAlignment(Element.ALIGN_RIGHT);
    cell.addElement(p);
    cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
    cell.setBorder(Rectangle.NO_BORDER);
    return cell;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读