打印为 PDF 时字符串格式中的退格键

Backspace in string formatting while printing to PDF

我正在尝试以 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);

例如,如果用户输入 "John Edward Jr." 作为姓名,"Pascal Einstein W. Alfi" 作为姓氏,"St. John" 作为学校,那么预期的输出是这样的:

Name: John Edward Jr.________________AAA:

Surname: Pascal Einstein W. Alfi_______BBB:

School: St. John___________________CCC:

我想每个字符串名称、姓氏和学校中的空格都是问题所在。我该如何处理?

预期输出:

Name: John Edward Jr.__________________AAA:

Surname: Pascal Einstein W. Alfi_______BBB:

School: St. John_______________________CCC:

编辑:

int width = 60 - name.Count(Char.IsWhiteSpace);

问题出在打印为 PDF 时使用的间距键。 只需使用不间断空格(按 Alt+<255>)或者您可以使用以下代码转换您的空格:

string s = " ";
if(s == " ")
{
 s = "&nbsp;"
}

使用"My name is this".Replace(" ", "&nbsp;");

更新 2

因为您使用的是 iText#,您可以 API 插入空格,例如:

iTextSharp.text.Paragraph s = new iTextSharp.text.Paragraph(name, font);
s.SpacingAfter = 20;
s.Add("AAA: ");

如果我的回答支持您的问题,请告诉我。

我已经在下面尝试过了。它对我有用。您正在使用 (60 - name.size) 作为名称宽度,但您没有使用 (name.width) 作为 AAA。如果你使用它,那么总宽度将为 = (60 - name.size) + name.size = 60。[我认为这就是你想要的。]。祝你好运。

string name = "John Edward Jr.";
        string surname = "Pascal Einstein W. Alfi";
        string school = "St. John";
        string s = "";
        int charWidth = 0;
        int width = 60 - name.Count();
        charWidth = name.Count();
        s = s + string.Format("{0,-" + width + "}" + "{1," + charWidth + "}", "Name: " + name, "AAA: ");
        Console.WriteLine(s);
        s = "";
        width = 60 - surname.Count();
        charWidth = surname.Count();
        s = s + string.Format("{0,-" + width + "}" + "{1," + charWidth + "}", "Surname: " + surname, "BBB: ");
        Console.WriteLine(s);
        s = "";
        width = 60 - school.Count();
        charWidth = school.Count();
        s = s + string.Format("{0,-" + width + "}" + "{1," + charWidth + "}", "School: " + school, "CCC: ");
        Console.WriteLine(s);

假设我们要以表格格式呈现此数据:

public static final String[][] DATA = {
    {"John Edward Jr.", "AAA"},
    {"Pascal Einstein W. Alfi", "BBB"},
    {"St. John", "CCC"}
};

使用 iText 可以通过三种方式实现这一点。

解决方案 #1:使用 table

请看一下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();
}

我们创建一个包含 2 列的 table 并添加 6 个单元格。这将导致 table 具有 2 列和 3 行。当我们删除边框并告诉 table 第一列的宽度应为第二列的 5 倍时,结果如下所示:

优点:如果其中一个单元格中的文本太多,内容会自动换行,table 的大小会适应内容。

解决方案 #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;
}

在这个例子中,我们创建了一个段落,我们为其定义了 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;
}

现在我们设置第一个字符串的格式,使其始终为 35 个字符。结果如下所示:

缺点:如果第一栏的文字需要超过35个字符,它会运行通过第二栏,第二栏的文字会粘在上面。您还看到我需要使用不同的字体。我使用 PTMono-Regular. Usually, text in a monospaced font takes more space than text in a proportional font. For more info about monospaced fonts read my answer to: