Nut*_*ker 3 c# pdf string formatting itextsharp
我正在尝试在PDF中打印两列信息,其中包含用户输入的一些字符串。到目前为止,这是我的代码:
string s = "";
int width = 60 - name.Count(Char.IsWhiteSpace);
s = s + string.Format("{0,-" + width +"}", "Name: " + name);
s = s + string.Format("{0,15}","AAA: ");
p1.Add(s);
document.Add(p1);
string p = "";
int width = 60 - surname.Count(Char.IsWhiteSpace);
p = p + string.Format("{0,-"+ width +"}", "Surname: " + surname);
p = p + string.Format("{0,15}","BBB: ");
p2.Add(p);
document.Add(p2);
string r = "";
int width = 60 - school.Count(Char.IsWhiteSpace);
r = r + string.Format("{0,-"+ width +"}", "School: " + school);
r = r + string.Format("{0,15}","CCC: ");
p3.Add(r);
document.Add(p3);
Run Code Online (Sandbox Code Playgroud)
例如,如果用户输入“ John Edward Jr.”。以“ Pascal Einstein W. Alfi”为姓,以“ St. John”为学校,则预期输出如下所示:
姓名:小约翰·爱德华(John Edward) 姓:Pascal Einstein W.Alfi _______ BBB: 学校:St. John___________________CCC:
我想每个字符串名称,姓氏和学校中的空格都是问题。我该如何处理?
预期的输出:
姓名:小约翰·爱德华(John Edward Jr.) 姓:Pascal Einstein W.Alfi _______ BBB: 学校:St. John_______________________CCC:
编辑:
int width = 60 - name.Count(Char.IsWhiteSpace);
Run Code Online (Sandbox Code Playgroud)
假设我们要以表格格式呈现此数据:
public static final String[][] DATA = {
{"John Edward Jr.", "AAA"},
{"Pascal Einstein W. Alfi", "BBB"},
{"St. John", "CCC"}
};
Run Code Online (Sandbox Code Playgroud)
使用iText可以通过三种方法实现此目的。
解决方案1:使用表格
请看一下SimpleTable13示例:
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();
}
Run Code Online (Sandbox Code Playgroud)
我们创建一个包含2列的表格,并添加6个单元格。这将导致一个具有2列3行的表。当我们移除边框并告诉表格第一列的宽度应为第二列的5倍时,结果如下所示:
优点:如果其中一个单元格中的文本过多,则内容将自动换行,并且表格的大小将适应内容的需要。
解决方案2:使用标签
请看一下TableTab示例:
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;
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,我们创建一个段落,为其定义200个用户单位的标签。现在,当我们添加时Chunk.TABBING,内容将跳到该位置。结果看起来像这样:
缺点:当第一列中的文本过多时,该文本将进入第二列,并在接下来的200个用户单元中添加一个选项卡(可能在同一行,也可能在下一行)。
解决方案#3:使用空格和等宽字体
这是您正在执行的操作,除了您不使用等宽字体不会给您想要的结果。
请看一下TableSpace示例:
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;
}
Run Code Online (Sandbox Code Playgroud)
现在,我们格式化第一个String,以便它始终可以测量35个字符。结果看起来像这样:
缺点:如果第一列中的文本需要超过35个字符,它将贯穿第二列,第二列的文本将被粘贴到该文本上。您还看到我需要使用其他字体。我使用PTMono-Regular。通常,等宽字体的文本比比例字体的文本占用更多的空间。有关等宽字体的更多信息,请阅读我的回答:如何通过使用iTextSharp设置等宽字体?