我试图通过在两列中排列数据来创建一个字符串。
String s = "";
List selectedItems = listView.getSelectionModel().getSelectedItems();
s = s + (String) selectedItems.get(0);
String t;
for (int i = 1 ; i<selectedItems.size();i++){
t = (String) selectedItems.get(i);
if (i%2!=0){
s = s + "\t\t\t";
}
else{
s = s + "\n";
}
s = s + (String) t;
}
我的想法是使用这样的代码来完成任务。但根据字符串的长度,我不能让第二列变成一行。
我想有一种方法可以计算出覆盖一个字符串所需的标签数,改变我添加的额外标签数,而不是每行添加3个标签,但我想不出如何计算。
我怎样才能解决这个问题?
目前的结果。
one two
three four
five six
预期结果:
one two
three four
five six
这是在用""填充总长度为30的字符串后的结果。
我只是用jfx来获取数据,itextPdf我们用来将字符串写入pdf
根据 这个 你有3个选择。
假设你有这样的数据:
public static final String[][] DATA = {
{"John Edward Jr.", "AAA"},
{"Pascal Einstein W. Alfi", "BBB"},
};
1: 使用一个表格
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(50);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setWidths(new int[]{5, 1});
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.addCell("Name: " + DATA[0][0]);
table.addCell(DATA[0][1]);
table.addCell("Surname: " + DATA[1][0]);
table.addCell(DATA[1][1]);
table.addCell("School: " + DATA[2][0]);
table.addCell(DATA[1][1]);
document.add(table);
document.close();
}
2:使用标签
public void createPdf(String dest) throws FileNotFoundException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(createParagraphWithTab("Name: ", DATA[0][0], DATA[0][1]));
document.add(createParagraphWithTab("Surname: ", DATA[1][0], DATA[1][1]));
document.add(createParagraphWithTab("School: ", DATA[2][0], DATA[2][1]));
document.close();
}
public Paragraph createParagraphWithTab(String key, String value1, String value2) {
Paragraph p = new Paragraph();
p.setTabSettings(new TabSettings(200f));
p.add(key);
p.add(value1);
p.add(Chunk.TABBING);
p.add(value2);
return p;
}
3:使用空格和单倍行距的字体。
public void createPdf(String dest) throws DocumentException, IOException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.CP1250, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
document.add(createParagraphWithSpaces(font, String.format("%s: %s", "Name", DATA[0][0]), DATA[0][1]));
document.add(createParagraphWithSpaces(font, String.format("%s: %s", "Surname", DATA[1][0]), DATA[1][1]));
document.add(createParagraphWithSpaces(font, String.format("%s: %s", "School", DATA[2][0]), DATA[2][1]));
document.close();
}
public Paragraph createParagraphWithSpaces(Font font, String value1, String value2) {
Paragraph p = new Paragraph();
p.setFont(font);
p.add(String.format("%-35s", value1));
p.add(value2);
return p;
}
为了达到预期的输出,你不需要计算任何东西--你只需要使用 System.out.printf
:
public class Main {
public static void main(String[] args) {
System.out.printf("%-10s %-10s\n", "test", "test1");
System.out.printf("%-10s %-10s\n", "longertest", "test2");
}
}
结果。
test test1
longertest test2
正如评论中提到的那样,如果你使用的字体不是单倍行距的,这将无法解决问题,因此我建议也改变字体。
BaseFont bf = BaseFont.createFont(FONT, BaseFont.CP1250, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);