Backspace in string formatting while printing to PDF

前端 未结 3 1527
刺人心
刺人心 2021-01-20 04:54

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 = 6         


        
相关标签:
3条回答
  • 2021-01-20 05:35

    The issue is the spacing key you use when printing to PDF. Simply use non-breaking spaces instead (press Alt+<255>) or you can convert your spaces with the below code:

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

    OR

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

    Update 2

    Since you are using iText# you can you the API to insert spaces by doing for example:

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

    Please let me know if my answer support your question.

    0 讨论(0)
  • 2021-01-20 05:47

    i have tried this below. It worked for me. you are using (60 - name.size) for name width but you are not using (name.width) for AAA. if you use this then total width will be = (60 - name.size) + name.size = 60. [I think this is what you are wanting.]. good luck.

    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);
    
    0 讨论(0)
  • 2021-01-20 05:48

    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?

    0 讨论(0)
提交回复
热议问题