在打印到PDF时退格为字符串格式 [英] Backspace in string formatting while printing to PDF

查看:245
本文介绍了在打印到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为学校,那么预期的输出是这样的:

pre
姓名:约翰·爱德华小姐_________________ AAA:

姓:Pascal Einstein W. Alfi_______BBB:

学校:St. John___________________CCC:

我想每个字符串的名字,姓氏和学校都是问题。

预期的输出:

 
名称:John爱德华Jr.__________________ AAA:

姓:Pascal Einstein W. Alfi_______BBB:

学校:St. John_______________________CCC:

编辑:

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


解决方案

假设我们想要以表格格式:

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

使用iText有三种方法可以达到这个目的。



解决方案#1:使用表格

请看



好处:如果其中一个单元格中的文字太多,那么内容会自动换行并且表格的大小将适应容纳内容。

解决方案#2:使用选项卡

请查看 TableTab 示例:

  public void createPdf(String dest)throws FileNotFoundException,DocumentException {
Document document = new Document();

PdfWriter.getInstance(document,new FileOutputStream(dest));

document.open();
$ b 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();

$ b $ 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;





$ b

在这个例子中,我们创建了一个段落,我们定义了200个用户单位。现在,当我们添加 Chunk.TABBING 时,内容将跳转到该位置。结果如下所示:



< img src =https://i.stack.imgur.com/QO2Of.pngalt =输入图片描述>



缺点:当第一列中的文本太多时,该文本将进入第二列,并且将在接下来的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);
$ b 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();

$ b $ 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;



$ b现在我们格式化第一个字符串,以便总是测量35个字符。结果如下所示:



< img src =https://i.stack.imgur.com/jpbvQ.pngalt =在这里输入图片描述>



缺点:如果第一列中的文本需要超过35个字符,则它将贯穿第二列,并且第二列的文本将被粘贴到它。你也看到我需要使用不同的字体。我使用 PTMono-Regular 。通常情况下,等宽字体中的文本比比例字体中的文本占用更多的空间。有关等宽字体的更多信息,请阅读我的答案:如何设置等宽字体通过使用iTextSharp?


I'm trying to print in PDF two columns of information that contain some strings that the user inputs. This is my code so far:

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);

If the user enters, for example, "John Edward Jr." for name, "Pascal Einstein W. Alfi" for surname and "St. John" for school, then the expected output is something like this:

Name: John Edward Jr.________________AAA:

Surname: Pascal Einstein W. Alfi_______BBB:

School: St. John___________________CCC:

I suppose the spaces in every string name, surname and school are the problem. How can I deal with this?

EXPECTED OUTPUT:

Name: John Edward Jr.__________________AAA:

Surname: Pascal Einstein W. Alfi_______BBB:

School: St. John_______________________CCC:

EDIT:

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

解决方案

Suppose that we want to render this data in tabular format:

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

There are three ways to achieve this using iText.

Solution #1: use a table

Please take a look at the SimpleTable13 example:

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();
}

We create a table with 2 columns and we add 6 cells. This will result in a table with 2 columns and 3 rows. As we removed the border and as we told the table that the first column should be 5 times as wide as the second column, the result looks like this:

Advantage: should there be too much text in one of the cells, then the content will automatically wrap and the size of the table will adapt to accommodate the content.

Solution #2: use tabs

Please take a look at the TableTab example:

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;
}

In this example, we create a paragraph for which we define tabs of 200 user units. Now when we add a Chunk.TABBING, the content will jump to that position. The result looks like this:

Disadvantage: when there is too much text in the first column, that text will run into the second column, and a tab will be added at the next 200 user units (could be on the same line, could be on the next line).

Solution #3: use spaces and a monospaced font

This is what you're doing, except that you don't use a monospaced font which doesn't give you the result you want.

Please take a look at the TableSpace example:

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;
}

Now we format the first String so that it always measures 35 characters. The result looks like this:

Disadvantages: if the text in the first column needs more than 35 characters, it will run through the second column and the text of the second column will be glued to it. You also see that I needed to use a different font. I use 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: How to set monospaced font by using iTextSharp?

这篇关于在打印到PDF时退格为字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆