Wednesday 16, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 3 June 2017

iText 5 PDF - How to restrict the number of characters on a single line?

To create PDF file we need iText 5 jar. Download iText Jars from iText Website or Maven Repository

Maven Dependency

2.<dependency>
3. <groupid>com.itextpdf</groupid>
4. <artifactid>itextpdf</artifactid>
5. <version>5.5.11</version>
6.</dependency>

UnderlineParagraphWithTwoParts.java
001.package com.javatutorialcorner.itextpdf;
002. 
003.import com.itextpdf.text.Chunk;
004.import com.itextpdf.text.Document;
005.import com.itextpdf.text.DocumentException;
006.import com.itextpdf.text.Font;
007.import com.itextpdf.text.Paragraph;
008.import com.itextpdf.text.pdf.BaseFont;
009.import com.itextpdf.text.pdf.PdfWriter;
010.import com.itextpdf.text.pdf.draw.LineSeparator;
011.import com.itextpdf.text.pdf.draw.VerticalPositionMark;
012.  
013. 
014.import java.io.File;
015.import java.io.FileOutputStream;
016.import java.io.IOException;
017. 
018.public class UnderlineParagraphWithTwoParts {
019.  public static final String DEST = "C:/JTC/UnderlineParagraphWithTwoParts.pdf";
020.      public static final Chunk GLUE = new Chunk(new VerticalPositionMark());
021.  
022.    public static void main(String[] args) throws IOException, DocumentException {
023.        File file = new File(DEST);
024.        file.getParentFile().mkdirs();
025.        new UnderlineParagraphWithTwoParts().createPdf(DEST);
026.    }
027.  
028.    public void createPdf(String dest) throws IOException, DocumentException {
029.        Document document = new Document();
030.        PdfWriter.getInstance(document, new FileOutputStream(dest));
031.        document.open();
032.        BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
033.        float charWidth = bf.getWidth(" ");
034.        int charactersPerLine = 101;
035.        float pageWidth = document.right() - document.left();
036.        float fontSize = (1000 * (pageWidth / (charWidth * charactersPerLine)));
037.        fontSize = ((int)(fontSize * 100)) / 100f;
038.        Font font = new Font(bf, fontSize);
039.        String string2 = "0123456789";
040.        for (int i = 0; i < 5; i++)
041.            string2 = string2 + string2;
042.        addParagraphWithTwoParts1(document, font, "0123", string2);
043.        document.add(new Paragraph("Test 1"));
044.        addParagraphWithTwoParts2(document, font, "0123", string2);
045.        document.add(new Paragraph("Test 1"));
046.        document.add(new Paragraph("Test 1"));
047.        addParagraphWithTwoParts1(document, font, "012345", string2);
048.        document.add(new Paragraph("Test 2"));
049.        addParagraphWithTwoParts2(document, font, "012345", string2);
050.        document.add(new Paragraph("Test 2"));
051.        addParagraphWithTwoParts1(document, font, "0123456789012345", string2);
052.        document.add(new Paragraph("Test 3"));
053.        document.add(new Paragraph("Test 3"));
054.        addParagraphWithTwoParts2(document, font, "0123456789012345", string2);
055.        document.add(new Paragraph("Test 3"));
056.        document.add(new Paragraph("Test 3"));
057.        addParagraphWithTwoParts1(document, font, "012", "0123456789");
058.        document.add(new Paragraph("Test 4"));
059.        addParagraphWithTwoParts2(document, font, "012", "0123456789");
060.        document.add(new Paragraph("Test 4"));
061.        addParagraphWithTwoParts1(document, font, "012345", "01234567890123456789");
062.        document.add(new Paragraph("Test 5"));
063.        addParagraphWithTwoParts2(document, font, "012345", "01234567890123456789");
064.        document.add(new Paragraph("Test 5"));
065.        addParagraphWithTwoParts1(document, font, "0", "01234567890123456789012345678901234567890123456789");
066.        document.add(new Paragraph("Test 6"));
067.        addParagraphWithTwoParts2(document, font, "0", "01234567890123456789012345678901234567890123456789");
068.        document.close();
069.    }
070.  
071.    public void addParagraphWithTwoParts1(Document document, Font font, String string1, String string2)
072.            throws DocumentException {
073.        if (string1 == null) string1 = "";
074.        if (string1.length() > 10)
075.            string1 = string1.substring(0, 10);
076.        Chunk chunk1 = new Chunk(string1, font);
077.        if (string2 == null) string2 = "";
078.        if (string1.length() + string2.length() > 100)
079.            string2 = string2.substring(0, 100 - string1.length());
080.        Chunk chunk2 = new Chunk(string2, font);
081.        Paragraph p = new Paragraph();
082.        p.add(chunk1);
083.        p.add(GLUE);
084.        p.add(chunk2);
085.        LineSeparator line = new LineSeparator();
086.        line.setOffset(-2);
087.        p.add(line);
088.        document.add(p);
089.    }
090.  
091.    public void addParagraphWithTwoParts2(Document document, Font font, String string1, String string2)
092.            throws DocumentException {
093.        if (string1 == null) string1 = "";
094.        if (string1.length() > 10)
095.            string1 = string1.substring(0, 10);
096.        if (string2 == null) string2 = "";
097.        if (string1.length() + string2.length() > 100)
098.            string2 = string2.substring(0, 100 - string1.length());
099.        Paragraph p = new Paragraph(string1 + " " + string2, font);
100.        LineSeparator line = new LineSeparator();
101.        line.setOffset(-2);
102.        p.add(line);
103.        document.add(p);
104.    }
105.}

Output

Reference : iText Website

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: iText 5 PDF - How to restrict the number of characters on a single line? Rating: 5 Reviewed By: eHowToNow