java - 如何将 iText LIST 放在具有特定字体和对齐方式的绝对位置上?
问题描述
public void printTextOnAbsolutePosition(String teks, PdfWriter writer, Rectangle rectangle, boolean useAscender) throws Exception {
Font fontParagraf = new Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL);
rectangle.setBorder(Rectangle.BOX);
rectangle.setBorderColor(BaseColor.RED);
rectangle.setBorderWidth(0.0f);
PdfContentByte cb = writer.getDirectContent();
cb.rectangle(rectangle);
Paragraph para = new Paragraph(teks, fontParagraf);
ColumnText columnText = new ColumnText(cb);
columnText.setSimpleColumn(rectangle);
columnText.setUseAscender(useAscender);
columnText.addText(para);
columnText.setAlignment(3);
columnText.setLeading(10);
columnText.go();
}
我们可以使用上面的代码在绝对位置上打印带有 iText 的文本。但是我们怎样才能用 List 实现同样的效果呢?另外,如何格式化列表的文本,使其使用特定的字体和文本对齐方式?
解决方案
public void putBulletOnAbsolutePosition(String yourText, PdfWriter writer, Float koorX, Float koorY, Float lebarX, Float lebarY) throws Exception {
List listToBeShown = createListWithBulletImageAndFormatedFont(yourText);
// ... (the same) ...
columnText.addElement(listToBeShown);
columnText.go();
}
方法基本相同。通过查看文档,我们发现不是.addtext
在 ColumnText 上使用,而是使用.addElement
接受 Paragraph、List、PdfPTable 和 Image。
至于格式化列表文本,我们只需要将 paragraf 作为列表的输入(而不是columnText.setAlignment()
用于设置对齐方式)。
public List createListWithBulletImageAndFormatedFont(String yourText) throws Exception {
Image bulletImage = Image.getInstance("src/main/resources/japoimages/bullet_blue_japo.gif");
bulletImage.scaleAbsolute(10, 8);
bulletImage.setScaleToFitHeight(false);
List myList = new List();
myList.setListSymbol(new Chunk(Image.getInstance(bulletImage), 0, 0));
String[] yourListContent = yourText.split("__");
Font fontList = new Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL);
for (int i=0; i < yourListContent.length; i++) {
Paragraph para = new Paragraph(yourListContent[i], fontList);
para.setAlignment(Element.ALIGN_JUSTIFIED);
myList.add(new ListItem(para));
}
return myList;
}
上面的代码将在我们想要的任何位置打印项目符号列表(使用图像)。
public void putBulletnAbsolutePosition (String dest) throws Exception {
Document document = new Document(PageSize.A5, 30,30, 60, 40);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
String myText = "ONE__TWO__THREE__FOUR";
putBulletOnAbsolutePosition(myText, writer, 140f, 350f, 300f, 200f);
document.close();
}
推荐阅读
- python - 查找二进制向量的“阶段”(“完成”的百分比,或块中的相对位置)的大多数 Pythonic 方法
- php - 如何从准备好的国家名称、数据库列表中搜索文本文件,然后用 id 替换这些国家?
- python - 错误:ResultSet 对象没有属性“endswith”
- javascript - 用 () 反应渲染返回语句与什么都不返回
- angular - 如何从 API 正确使用 ng2 图表和 Angular 显示数据
- javascript - 将 Javascript Promise 放入函数中
- arrays - String incrementer codewars (Ruby) 问题
- c++ - 通过网络传递 std::variant 是否安全,跨平台
- c - 警告:取消引用 'void *' 指针 [默认启用] 和错误:没有忽略 void 值,因为它应该是帮助 c
- python - 1:1 替换已弃用的 time.clock()