Separator examples

Tags: tablesseparatorParagraphjustify textmeasure textfont widthfontsdotted linedashed lineunderlineiText 5
Files: 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/22382717/write-two-itext-paragraphs-on-the-same-position
 * 
 * We create a Chunk and add a background color.
 */
package sandbox.objects;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class UnderlineWithDottedLine {
    public static final String DEST = "results/objects/underline_dotted.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new UnderlineWithDottedLine().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        Paragraph p = new Paragraph("This line will be underlined with a dotted line.");
        DottedLineSeparator dottedline = new DottedLineSeparator();
        dottedline.setOffset(-2);
        dottedline.setGap(2f);
        p.add(dottedline);
        document.add(p);
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/20233630/itextsharp-how-to-add-a-full-line-break
 * 
 * We create a Chunk and add a background color.
 */
package sandbox.objects;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class FullDottedLine {
    public static final String DEST = "results/objects/full_dotted_line.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FullDottedLine().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        document.add(new Paragraph("Before dotted line"));
        DottedLineSeparator separator = new DottedLineSeparator();
        separator.setPercentage(59500f / 523f);
        Chunk linebreak = new Chunk(separator);
        document.add(linebreak);
        document.add(new Paragraph("After dotted line"));
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/27752409/using-itext-to-draw-separator-line-as-continuous-hypen-in-a-table-row
 */
package sandbox.objects;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class CustomDashedLine {
 
    class CustomDashedLineSeparator extends DottedLineSeparator {
        protected float dash = 5;
        protected float phase = 2.5f;
 
        public float getDash() {
            return dash;
        }
 
        public float getPhase() {
            return phase;
        }
 
        public void setDash(float dash) {
            this.dash = dash;
        }
 
        public void setPhase(float phase) {
            this.phase = phase;
        }
 
        public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {
            canvas.saveState();
            canvas.setLineWidth(lineWidth);
            canvas.setLineDash(dash, gap, phase);
            drawLine(canvas, llx, urx, y);
            canvas.restoreState();
        }
    }
 
    public static final String DEST = "results/objects/custom_dashed_line.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CustomDashedLine().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        document.add(new Paragraph("Before dashed line"));
        CustomDashedLineSeparator separator = new CustomDashedLineSeparator();
        separator.setDash(10);
        separator.setGap(7);
        separator.setLineWidth(3);
        Chunk linebreak = new Chunk(separator);
        document.add(linebreak);
        document.add(new Paragraph("After dashed line"));
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/27591313/how-to-restrict-number-of-characters-in-a-phrase-in-pdf
 */
package sandbox.objects;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import com.itextpdf.text.pdf.draw.VerticalPositionMark;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class UnderlineParagraphWithTwoParts {
    public static final String DEST = "results/objects/underline_paragraph_with_two_parts.pdf";
    public static final Chunk GLUE = new Chunk(new VerticalPositionMark());
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new UnderlineParagraphWithTwoParts().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        float charWidth = bf.getWidth(" ");
        int charactersPerLine = 101;
        float pageWidth = document.right() - document.left();
        float fontSize = (1000 * (pageWidth / (charWidth * charactersPerLine)));
        fontSize = ((int)(fontSize * 100)) / 100f;
        Font font = new Font(bf, fontSize);
        String string2 = "0123456789";
        for (int i = 0; i < 5; i++)
            string2 = string2 + string2;
        addParagraphWithTwoParts1(document, font, "0123", string2);
        document.add(new Paragraph("Test 1"));
        addParagraphWithTwoParts2(document, font, "0123", string2);
        document.add(new Paragraph("Test 1"));
        document.add(new Paragraph("Test 1"));
        addParagraphWithTwoParts1(document, font, "012345", string2);
        document.add(new Paragraph("Test 2"));
        addParagraphWithTwoParts2(document, font, "012345", string2);
        document.add(new Paragraph("Test 2"));
        addParagraphWithTwoParts1(document, font, "0123456789012345", string2);
        document.add(new Paragraph("Test 3"));
        document.add(new Paragraph("Test 3"));
        addParagraphWithTwoParts2(document, font, "0123456789012345", string2);
        document.add(new Paragraph("Test 3"));
        document.add(new Paragraph("Test 3"));
        addParagraphWithTwoParts1(document, font, "012", "0123456789");
        document.add(new Paragraph("Test 4"));
        addParagraphWithTwoParts2(document, font, "012", "0123456789");
        document.add(new Paragraph("Test 4"));
        addParagraphWithTwoParts1(document, font, "012345", "01234567890123456789");
        document.add(new Paragraph("Test 5"));
        addParagraphWithTwoParts2(document, font, "012345", "01234567890123456789");
        document.add(new Paragraph("Test 5"));
        addParagraphWithTwoParts1(document, font, "0", "01234567890123456789012345678901234567890123456789");
        document.add(new Paragraph("Test 6"));
        addParagraphWithTwoParts2(document, font, "0", "01234567890123456789012345678901234567890123456789");
        document.close();
    }
 
    public void addParagraphWithTwoParts1(Document document, Font font, String string1, String string2)
            throws DocumentException {
        if (string1 == null) string1 = "";
        if (string1.length() > 10)
            string1 = string1.substring(0, 10);
        Chunk chunk1 = new Chunk(string1, font);
        if (string2 == null) string2 = "";
        if (string1.length() + string2.length() > 100)
            string2 = string2.substring(0, 100 - string1.length());
        Chunk chunk2 = new Chunk(string2, font);
        Paragraph p = new Paragraph();
        p.add(chunk1);
        p.add(GLUE);
        p.add(chunk2);
        LineSeparator line = new LineSeparator();
        line.setOffset(-2);
        p.add(line);
        document.add(p);
    }
 
    public void addParagraphWithTwoParts2(Document document, Font font, String string1, String string2)
            throws DocumentException {
        if (string1 == null) string1 = "";
        if (string1.length() > 10)
            string1 = string1.substring(0, 10);
        if (string2 == null) string2 = "";
        if (string1.length() + string2.length() > 100)
            string2 = string2.substring(0, 100 - string1.length());
        Paragraph p = new Paragraph(string1 + " " + string2, font);
        LineSeparator line = new LineSeparator();
        line.setOffset(-2);
        p.add(line);
        document.add(p);
    }
}
/*
 * Example written by Bruno Lowagie in answer to the following question on StackOverflow:
 * http://stackoverflow.com/questions/31315441/how-to-add-inline-spacing-in-pdfpcell
 */
package sandbox.tables;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.VerticalPositionMark;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class CellWithGlue {
 
    public static final String DEST = "results/tables/cell_with_glue.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CellWithGlue().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table;
        PdfPCell cell;
        table = new PdfPTable(2);
        table.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.setWidthPercentage(60);
        table.setSpacingAfter(20);
        cell = new PdfPCell(new Phrase("Received Rs (in Words):"));
        cell.setBorder(PdfPCell.LEFT | PdfPCell.TOP | PdfPCell.BOTTOM);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Priceless"));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setBorder(PdfPCell.RIGHT | PdfPCell.TOP | PdfPCell.BOTTOM);
        table.addCell(cell);
        document.add(table);
        table.setWidthPercentage(50);
        document.add(table);
        table = new PdfPTable(1);
        table.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.setWidthPercentage(50);
        Phrase p = new Phrase();
        p.add(new Chunk("Received Rs (In Words):"));
        p.add(new Chunk(new VerticalPositionMark()));
        p.add(new Chunk("Priceless"));
        table.addCell(p);
        document.add(table);
        document.close();
    }
 
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/29575142/how-to-align-two-paragraphs-or-text-in-left-and-right-in-a-same-line-in-pdf
 */
package sandbox.objects;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.VerticalPositionMark;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class LeftRight {
 
    public static final String DEST = "results/objects/left_right.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new LeftRight().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws FileNotFoundException, DocumentException {
        Document document = new Document();
 
        PdfWriter.getInstance(document, new FileOutputStream(dest));
 
        document.open();
 
        Chunk glue = new Chunk(new VerticalPositionMark());
        Paragraph p = new Paragraph("Text to the left");
        p.add(new Chunk(glue));
        p.add("Text to the right");
        document.add(p);
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100);
        table.addCell(getCell("Text to the left", PdfPCell.ALIGN_LEFT));
        table.addCell(getCell("Text in the middle", PdfPCell.ALIGN_CENTER));
        table.addCell(getCell("Text to the right", PdfPCell.ALIGN_RIGHT));
        document.add(table);
        document.close();
    }
 
    public PdfPCell getCell(String text, int alignment) {
        PdfPCell cell = new PdfPCell(new Phrase(text));
        cell.setPadding(0);
        cell.setHorizontalAlignment(alignment);
        cell.setBorder(PdfPCell.NO_BORDER);
        return cell;
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 *http://stackoverflow.com/questions/27046352/how-to-generate-table-of-figures-dot-leaders-in-a-pdfpcell-for-the-last-line-of
 */
package sandbox.tables;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class DottedLineLeader {
 
    public static final String DEST = "results/tables/dotted_line_leader.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DottedLineLeader().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(50);
        table.setWidths(new int[]{1, 3, 1});
        Chunk leader = new Chunk(new DottedLineSeparator());
        Paragraph p;
        table.addCell(getCell(new Paragraph("fig 1"), Element.ALIGN_TOP));
        p = new Paragraph("Title text");
        p.add(leader);
        table.addCell(getCell(p, Element.ALIGN_TOP));
        table.addCell(getCell(new Paragraph("2"), Element.ALIGN_BOTTOM));
        table.addCell(getCell(new Paragraph("fig 2"), Element.ALIGN_TOP));
        p = new Paragraph("This is a longer title text that wraps");
        p.add(leader);
        table.addCell(getCell(p, Element.ALIGN_TOP));
        table.addCell(getCell(new Paragraph("55"), Element.ALIGN_BOTTOM));
        table.addCell(getCell(new Paragraph("fig 3"), Element.ALIGN_TOP));
        p = new Paragraph("Another title text");
        p.add(leader);
        table.addCell(getCell(p, Element.ALIGN_TOP));
        table.addCell(getCell(new Paragraph("89"), Element.ALIGN_BOTTOM));
        document.add(table);
        document.close();
    }
 
    public PdfPCell getCell(Paragraph p, int verticalAlignment) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(verticalAlignment);
        cell.setUseAscender(true);
        cell.setUseDescender(true);
        cell.addElement(p);
        return cell;
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/38236515
 */
package sandbox.objects;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class DottedLineEnder {
    public static final String DEST = "results/objects/dotted_line_ender.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DottedLineEnder().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        DottedLineSeparator separator = new DottedLineSeparator();
        Chunk c = new Chunk(separator);
        Paragraph p = new Paragraph("Ends with dots ");
        p.add(c);
        document.add(p);
        p = new Paragraph("This is a much longer paragraph that spans "
                + "several lines. The String used to create this paragraph "
                + "will be split automatically at the end of the line. The "
                + "final line of this paragraph will end in a dotted line. ");
        p.add(c);
        document.add(p);
        document.close();
    }
}