Separator examples

This is a code example of iText PDF, discover more.

25th October 2015
admin-marketing

Switch code for this example

UnderlineWithDottedLine.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/22382717/write-two-itext-paragraphs-on-the-same-position
  4.  *
  5.  * We create a Chunk and add a background color.
  6.  */
  7. package sandbox.objects;
  8.  
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13. import com.itextpdf.text.pdf.draw.DottedLineSeparator;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18.  
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class UnderlineWithDottedLine {
  23.     public static final String DEST = "results/objects/underline_dotted.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new UnderlineWithDottedLine().createPdf(DEST);
  29.     }
  30.    
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Document document = new Document();
  33.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  34.         document.open();
  35.         Paragraph p = new Paragraph("This line will be underlined with a dotted line.");
  36.         DottedLineSeparator dottedline = new DottedLineSeparator();
  37.         dottedline.setOffset(-2);
  38.         dottedline.setGap(2f);
  39.         p.add(dottedline);
  40.         document.add(p);
  41.         document.close();
  42.     }
  43. }
FullDottedLine.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/20233630/itextsharp-how-to-add-a-full-line-break
  4.  *
  5.  * We create a Chunk and add a background color.
  6.  */
  7. package sandbox.objects;
  8.  
  9. import com.itextpdf.text.Chunk;
  10. import com.itextpdf.text.Document;
  11. import com.itextpdf.text.DocumentException;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import com.itextpdf.text.pdf.draw.DottedLineSeparator;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class FullDottedLine {
  24.     public static final String DEST = "results/objects/full_dotted_line.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new FullDottedLine().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         document.add(new Paragraph("Before dotted line"));
  37.         DottedLineSeparator separator = new DottedLineSeparator();
  38.         separator.setPercentage(59500f / 523f);
  39.         Chunk linebreak = new Chunk(separator);
  40.         document.add(linebreak);
  41.         document.add(new Paragraph("After dotted line"));
  42.         document.close();
  43.     }
  44. }
CustomDashedLine.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27752409/using-itext-to-draw-separator-line-as-continuous-hypen-in-a-table-row
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13. import com.itextpdf.text.pdf.draw.DottedLineSeparator;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class CustomDashedLine {
  22.    
  23.     class CustomDashedLineSeparator extends DottedLineSeparator {
  24.         protected float dash = 5;
  25.         protected float phase = 2.5f;
  26.  
  27.         public float getDash() {
  28.             return dash;
  29.         }
  30.  
  31.         public float getPhase() {
  32.             return phase;
  33.         }
  34.  
  35.         public void setDash(float dash) {
  36.             this.dash = dash;
  37.         }
  38.  
  39.         public void setPhase(float phase) {
  40.             this.phase = phase;
  41.         }
  42.        
  43.         public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {
  44.             canvas.saveState();
  45.             canvas.setLineWidth(lineWidth);
  46.             canvas.setLineDash(dash, gap, phase);
  47.             drawLine(canvas, llx, urx, y);
  48.             canvas.restoreState();
  49.         }
  50.     }
  51.    
  52.     public static final String DEST = "results/objects/custom_dashed_line.pdf";
  53.    
  54.     public static void main(String[] args) throws IOException, DocumentException {
  55.         File file = new File(DEST);
  56.         file.getParentFile().mkdirs();
  57.         new CustomDashedLine().createPdf(DEST);
  58.     }
  59.    
  60.     public void createPdf(String dest) throws IOException, DocumentException {
  61.         Document document = new Document();
  62.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  63.         document.open();
  64.         document.add(new Paragraph("Before dashed line"));
  65.         CustomDashedLineSeparator separator = new CustomDashedLineSeparator();
  66.         separator.setDash(10);
  67.         separator.setGap(7);
  68.         separator.setLineWidth(3);
  69.         Chunk linebreak = new Chunk(separator);
  70.         document.add(linebreak);
  71.         document.add(new Paragraph("After dashed line"));
  72.         document.close();
  73.     }
  74. }
UnderlineParagraphWithTwoParts.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27591313/how-to-restrict-number-of-characters-in-a-phrase-in-pdf
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.BaseFont;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import com.itextpdf.text.pdf.draw.LineSeparator;
  15. import com.itextpdf.text.pdf.draw.VerticalPositionMark;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class UnderlineParagraphWithTwoParts {
  24.     public static final String DEST = "results/objects/underline_paragraph_with_two_parts.pdf";
  25.     public static final Chunk GLUE = new Chunk(new VerticalPositionMark());
  26.    
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new UnderlineParagraphWithTwoParts().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document();
  35.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.         BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
  38.         float charWidth = bf.getWidth(" ");
  39.         int charactersPerLine = 101;
  40.         float pageWidth = document.right() - document.left();
  41.         float fontSize = (1000 * (pageWidth / (charWidth * charactersPerLine)));
  42.         fontSize = ((int)(fontSize * 100)) / 100f;
  43.         Font font = new Font(bf, fontSize);
  44.         String string2 = "0123456789";
  45.         for (int i = 0; i < 5; i++)
  46.             string2 = string2 + string2;
  47.         addParagraphWithTwoParts1(document, font, "0123", string2);
  48.         document.add(new Paragraph("Test 1"));
  49.         addParagraphWithTwoParts2(document, font, "0123", string2);
  50.         document.add(new Paragraph("Test 1"));
  51.         document.add(new Paragraph("Test 1"));
  52.         addParagraphWithTwoParts1(document, font, "012345", string2);
  53.         document.add(new Paragraph("Test 2"));
  54.         addParagraphWithTwoParts2(document, font, "012345", string2);
  55.         document.add(new Paragraph("Test 2"));
  56.         addParagraphWithTwoParts1(document, font, "0123456789012345", string2);
  57.         document.add(new Paragraph("Test 3"));
  58.         document.add(new Paragraph("Test 3"));
  59.         addParagraphWithTwoParts2(document, font, "0123456789012345", string2);
  60.         document.add(new Paragraph("Test 3"));
  61.         document.add(new Paragraph("Test 3"));
  62.         addParagraphWithTwoParts1(document, font, "012", "0123456789");
  63.         document.add(new Paragraph("Test 4"));
  64.         addParagraphWithTwoParts2(document, font, "012", "0123456789");
  65.         document.add(new Paragraph("Test 4"));
  66.         addParagraphWithTwoParts1(document, font, "012345", "01234567890123456789");
  67.         document.add(new Paragraph("Test 5"));
  68.         addParagraphWithTwoParts2(document, font, "012345", "01234567890123456789");
  69.         document.add(new Paragraph("Test 5"));
  70.         addParagraphWithTwoParts1(document, font, "0", "01234567890123456789012345678901234567890123456789");
  71.         document.add(new Paragraph("Test 6"));
  72.         addParagraphWithTwoParts2(document, font, "0", "01234567890123456789012345678901234567890123456789");
  73.         document.close();
  74.     }
  75.    
  76.     public void addParagraphWithTwoParts1(Document document, Font font, String string1, String string2)
  77.             throws DocumentException {
  78.         if (string1 == null) string1 = "";
  79.         if (string1.length() > 10)
  80.             string1 = string1.substring(0, 10);
  81.         Chunk chunk1 = new Chunk(string1, font);
  82.         if (string2 == null) string2 = "";
  83.         if (string1.length() + string2.length() > 100)
  84.             string2 = string2.substring(0, 100 - string1.length());
  85.         Chunk chunk2 = new Chunk(string2, font);
  86.         Paragraph p = new Paragraph();
  87.         p.add(chunk1);
  88.         p.add(GLUE);
  89.         p.add(chunk2);
  90.         LineSeparator line = new LineSeparator();
  91.         line.setOffset(-2);
  92.         p.add(line);
  93.         document.add(p);
  94.     }
  95.    
  96.     public void addParagraphWithTwoParts2(Document document, Font font, String string1, String string2)
  97.             throws DocumentException {
  98.         if (string1 == null) string1 = "";
  99.         if (string1.length() > 10)
  100.             string1 = string1.substring(0, 10);
  101.         if (string2 == null) string2 = "";
  102.         if (string1.length() + string2.length() > 100)
  103.             string2 = string2.substring(0, 100 - string1.length());
  104.         Paragraph p = new Paragraph(string1 + " " + string2, font);
  105.         LineSeparator line = new LineSeparator();
  106.         line.setOffset(-2);
  107.         p.add(line);
  108.         document.add(p);
  109.     }
  110. }
CellWithGlue.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question on StackOverflow:
  3.  * http://stackoverflow.com/questions/31315441/how-to-add-inline-spacing-in-pdfpcell
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Element;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import com.itextpdf.text.pdf.draw.VerticalPositionMark;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. /**
  23.  *
  24.  * @author Bruno Lowagie (iText Software)
  25.  */
  26. @WrapToTest
  27. public class CellWithGlue {
  28.  
  29.     public static final String DEST = "results/tables/cell_with_glue.pdf";
  30.    
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new CellWithGlue().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         Document document = new Document();
  39.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         PdfPTable table;
  42.         PdfPCell cell;
  43.         table = new PdfPTable(2);
  44.         table.setHorizontalAlignment(Element.ALIGN_LEFT);
  45.         table.setWidthPercentage(60);
  46.         table.setSpacingAfter(20);
  47.         cell = new PdfPCell(new Phrase("Received Rs (in Words):"));
  48.         cell.setBorder(PdfPCell.LEFT | PdfPCell.TOP | PdfPCell.BOTTOM);
  49.         table.addCell(cell);
  50.         cell = new PdfPCell(new Phrase("Priceless"));
  51.         cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
  52.         cell.setBorder(PdfPCell.RIGHT | PdfPCell.TOP | PdfPCell.BOTTOM);
  53.         table.addCell(cell);
  54.         document.add(table);
  55.         table.setWidthPercentage(50);
  56.         document.add(table);
  57.         table = new PdfPTable(1);
  58.         table.setHorizontalAlignment(Element.ALIGN_LEFT);
  59.         table.setWidthPercentage(50);
  60.         Phrase p = new Phrase();
  61.         p.add(new Chunk("Received Rs (In Words):"));
  62.         p.add(new Chunk(new VerticalPositionMark()));
  63.         p.add(new Chunk("Priceless"));
  64.         table.addCell(p);
  65.         document.add(table);
  66.         document.close();
  67.     }
  68.        
  69. }
LeftRight.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/29575142/how-to-align-two-paragraphs-or-text-in-left-and-right-in-a-same-line-in-pdf
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import com.itextpdf.text.pdf.draw.VerticalPositionMark;
  16.  
  17. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class LeftRight {
  25.  
  26.     public static final String DEST = "results/objects/left_right.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new LeftRight().createPdf(DEST);
  32.     }
  33.  
  34.     public void createPdf(String dest) throws FileNotFoundException, DocumentException {
  35.         Document document = new Document();
  36.  
  37.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.  
  39.         document.open();
  40.  
  41.         Chunk glue = new Chunk(new VerticalPositionMark());
  42.         Paragraph p = new Paragraph("Text to the left");
  43.         p.add(new Chunk(glue));
  44.         p.add("Text to the right");
  45.         document.add(p);
  46.         PdfPTable table = new PdfPTable(3);
  47.         table.setWidthPercentage(100);
  48.         table.addCell(getCell("Text to the left", PdfPCell.ALIGN_LEFT));
  49.         table.addCell(getCell("Text in the middle", PdfPCell.ALIGN_CENTER));
  50.         table.addCell(getCell("Text to the right", PdfPCell.ALIGN_RIGHT));
  51.         document.add(table);
  52.         document.close();
  53.     }
  54.  
  55.     public PdfPCell getCell(String text, int alignment) {
  56.         PdfPCell cell = new PdfPCell(new Phrase(text));
  57.         cell.setPadding(0);
  58.         cell.setHorizontalAlignment(alignment);
  59.         cell.setBorder(PdfPCell.NO_BORDER);
  60.         return cell;
  61.     }
  62. }
DottedLineLeader.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  *http://stackoverflow.com/questions/27046352/how-to-generate-table-of-figures-dot-leaders-in-a-pdfpcell-for-the-last-line-of
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Element;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import com.itextpdf.text.pdf.draw.DottedLineSeparator;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class DottedLineLeader {
  24.    
  25.     public static final String DEST = "results/tables/dotted_line_leader.pdf";
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new DottedLineLeader().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         PdfPTable table = new PdfPTable(3);
  37.         table.setWidthPercentage(50);
  38.         table.setWidths(new int[]{1, 3, 1});
  39.         Chunk leader = new Chunk(new DottedLineSeparator());
  40.         Paragraph p;
  41.         table.addCell(getCell(new Paragraph("fig 1"), Element.ALIGN_TOP));
  42.         p = new Paragraph("Title text");
  43.         p.add(leader);
  44.         table.addCell(getCell(p, Element.ALIGN_TOP));
  45.         table.addCell(getCell(new Paragraph("2"), Element.ALIGN_BOTTOM));
  46.         table.addCell(getCell(new Paragraph("fig 2"), Element.ALIGN_TOP));
  47.         p = new Paragraph("This is a longer title text that wraps");
  48.         p.add(leader);
  49.         table.addCell(getCell(p, Element.ALIGN_TOP));
  50.         table.addCell(getCell(new Paragraph("55"), Element.ALIGN_BOTTOM));
  51.         table.addCell(getCell(new Paragraph("fig 3"), Element.ALIGN_TOP));
  52.         p = new Paragraph("Another title text");
  53.         p.add(leader);
  54.         table.addCell(getCell(p, Element.ALIGN_TOP));
  55.         table.addCell(getCell(new Paragraph("89"), Element.ALIGN_BOTTOM));
  56.         document.add(table);
  57.         document.close();
  58.     }
  59.    
  60.     public PdfPCell getCell(Paragraph p, int verticalAlignment) {
  61.         PdfPCell cell = new PdfPCell();
  62.         cell.setVerticalAlignment(verticalAlignment);
  63.         cell.setUseAscender(true);
  64.         cell.setUseDescender(true);
  65.         cell.addElement(p);
  66.         return cell;
  67.     }
  68. }
DottedLineEnder.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/38236515
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import com.itextpdf.text.pdf.draw.DottedLineSeparator;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17.  
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class DottedLineEnder {
  22.     public static final String DEST = "results/objects/dotted_line_ender.pdf";
  23.    
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new DottedLineEnder().createPdf(DEST);
  28.     }
  29.    
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document();
  32.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.         DottedLineSeparator separator = new DottedLineSeparator();
  35.         Chunk c = new Chunk(separator);
  36.         Paragraph p = new Paragraph("Ends with dots ");
  37.         p.add(c);
  38.         document.add(p);
  39.         p = new Paragraph("This is a much longer paragraph that spans "
  40.                 + "several lines. The String used to create this paragraph "
  41.                 + "will be split automatically at the end of the line. The "
  42.                 + "final line of this paragraph will end in a dotted line. ");
  43.         p.add(c);
  44.         document.add(p);
  45.         document.close();
  46.     }
  47. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now