Java 如何使用 Apache PDFBox 创建表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28059563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Table using Apache PDFBox
提问by Anil
We are planning to migrate our pdf generation utilities from iText to PDFBox (Due to licensing issues in iText). With some effort, I was able to write and position text, draw lines etc. But creating Tables with text embedded in Table cells is a challenge, I went through the documentation, examples, Google, Stackoverflow couldn't find a thing. Was wondering if PDFBox provides native support for creating Tables with embedded text. My last resort would be to use this link https://github.com/eduardohl/Paginated-PDFBox-Table-Sample
我们计划将我们的 pdf 生成实用程序从 iText 迁移到 PDFBox(由于 iText 中的许可问题)。通过一些努力,我能够编写和定位文本,绘制线条等。但是创建带有嵌入在表格单元格中的文本的表格是一个挑战,我浏览了文档、示例、Google、Stackoverflow 找不到任何东西。想知道 PDFBox 是否为创建带有嵌入文本的表格提供本机支持。我最后的手段是使用此链接https://github.com/eduardohl/Paginated-PDFBox-Table-Sample
采纳答案by Anil
Thanks to the links provided by Tilman. Using the boxable API (https://github.com/dhorions/boxable) I was able to create the table I wanted to. Just an FYI I wanted to create the table with variable number of cells. For example row 1 would have 2 cells, row 2 could have 5 cells and row 3 could have just 3 cells. I was able to do with ease. I followed Example1.java in the link mentioned above.
感谢蒂尔曼提供的链接。使用 boxable API ( https://github.com/dhorions/boxable) 我能够创建我想要的表。仅供参考,我想创建具有可变数量单元格的表格。例如,第 1 行可能有 2 个单元格,第 2 行可能有 5 个单元格,第 3 行可能只有 3 个单元格。我能够轻松做到。我在上面提到的链接中关注了 Example1.java 。
回答by philonous
Since I also needed table drawing functionality for a side project, I implemented a small "table drawer" library myself, which I uploaded to github.
由于我还需要一个辅助项目的表格绘制功能,因此我自己实现了一个小型的“表格抽屉”库,并将其上传到github。
In order to produce such a table – for instance – ...
... you would need this code. In the same file you find the code for that table as well:
...您将需要此代码。在同一个文件中,您还可以找到该表的代码:
The current "feature list" includes:
当前的“功能列表”包括:
- set font and font size on table level as well as on cell level
- define single cells with bottom-, top-, left- and right-border width separately
- define the background color on row or cell level
- define padding (top, bottom, left, right) on cell level
- define border color (on table, row or cell level)
- specify text alignment (vertical and horizontal)
- cell spanning and row spanning
- text wrapping and line spacing
- 在表格级别以及单元格级别设置字体和字体大小
- 分别定义具有底部、顶部、左侧和右侧边框宽度的单个单元格
- 在行或单元格级别定义背景颜色
- 在单元格级别定义填充(顶部、底部、左侧、右侧)
- 定义边框颜色(在表格、行或单元格级别)
- 指定文本对齐方式(垂直和水平)
- 单元格跨越和行跨越
- 文本换行和行距
Also it should not be too hard to add missing stuff like having different border colors for borders on top, bottom, left and right-borders, if needed.
此外,如果需要,添加缺失的东西应该不会太难,例如为顶部、底部、左侧和右侧边框的边框设置不同的边框颜色。