栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Apache PDFBox Java库-是否有用于创建表的API?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Apache PDFBox Java库-是否有用于创建表的API?

来源 :使用PDFBox创建表

以下方法绘制具有指定表内容的表。这有点骇人听闻,并且适用于小的文本字符串。它不执行自动换行,但是您可以了解它是如何完成的。搏一搏!

public static void drawTable(PDPage page, PDPageContentStream contentStream,       float y, float margin,       String[][] content) throws IOException {    final int rows = content.length;    final int cols = content[0].length;    final float rowHeight = 20f;    final float tableWidth = page.findMediaBox().getWidth() - margin - margin;    final float tableHeight = rowHeight * rows;    final float colWidth = tableWidth/(float)cols;    final float cellMargin=5f;    //draw the rows    float nexty = y ;    for (int i = 0; i <= rows; i++) {        contentStream.drawLine(margin, nexty, margin+tableWidth, nexty);        nexty-= rowHeight;    }    //draw the columns    float nextx = margin;    for (int i = 0; i <= cols; i++) {        contentStream.drawLine(nextx, y, nextx, y-tableHeight);        nextx += colWidth;    }    //now add the text contentStream.setFont( PDType1Font.HELVETICA_BOLD , 12 );    float textx = margin+cellMargin;    float texty = y-15; for(int i = 0; i < content.length; i++){        for(int j = 0 ; j < content[i].length; j++){ String text = content[i][j]; contentStream.beginText(); contentStream.moveTextPositionByAmount(textx,texty); contentStream.drawString(text); contentStream.endText(); textx += colWidth;        }        texty-=rowHeight;        textx = margin+cellMargin;    }}

用法:

PDdocument doc = new PDdocument();PDPage page = new PDPage();doc.addPage( page );PDPageContentStream contentStream = new PDPageContentStream(doc, page);String[][] content = {{"a","b", "1"}, {"c","d", "2"}, {"e","f", "3"}, {"g","h", "4"}, {"i","j", "5"}} ;drawTable(page, contentStream, 700, 100, content);contentStream.close();doc.save("test.pdf" );


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407277.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号