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

iText 5 PDF - How to create a custom dashed line separator?

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

Maven Dependency



 com.itextpdf
 itextpdf
 5.5.11

CustomDashedLine.java
01.package com.javatutorialcorner.itextpdf;
02. 
03.import com.itextpdf.text.Chunk;
04.import com.itextpdf.text.Document;
05.import com.itextpdf.text.DocumentException;
06.import com.itextpdf.text.Paragraph;
07.import com.itextpdf.text.pdf.PdfContentByte;
08.import com.itextpdf.text.pdf.PdfWriter;
09.import com.itextpdf.text.pdf.draw.DottedLineSeparator;
10.  
11. 
12.import java.io.File;
13.import java.io.FileOutputStream;
14.import java.io.IOException;
15. 
16.public class CustomDashedLine {
17.  
18.    class CustomDashedLineSeparator extends DottedLineSeparator {
19.        protected float dash = 5;
20.        protected float phase = 2.5f;
21.  
22.        public float getDash() {
23.            return dash;
24.        }
25.  
26.        public float getPhase() {
27.            return phase;
28.        }
29.  
30.        public void setDash(float dash) {
31.            this.dash = dash;
32.        }
33.  
34.        public void setPhase(float phase) {
35.            this.phase = phase;
36.        }
37.  
38.        public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {
39.            canvas.saveState();
40.            canvas.setLineWidth(lineWidth);
41.            canvas.setLineDash(dash, gap, phase);
42.            drawLine(canvas, llx, urx, y);
43.            canvas.restoreState();
44.        }
45.    }
46.  
47.    public static final String DEST = "C:/JTC/CustomDashedLine.pdf";
48.      
49.    public static void main(String[] args) throws IOException, DocumentException {
50.        File file = new File(DEST);
51.        file.getParentFile().mkdirs();
52.        new CustomDashedLine().createPdf(DEST);
53.    }
54.  
55.    public void createPdf(String dest) throws IOException, DocumentException {
56.        Document document = new Document();
57.        PdfWriter.getInstance(document, new FileOutputStream(dest));
58.        document.open();
59.        document.add(new Paragraph("Before dashed line"));
60.        CustomDashedLineSeparator separator = new CustomDashedLineSeparator();
61.        separator.setDash(10);
62.        separator.setGap(7);
63.        separator.setLineWidth(3);
64.        Chunk linebreak = new Chunk(separator);
65.        document.add(linebreak);
66.        document.add(new Paragraph("After dashed line"));
67.        document.close();
68.    }
69.}

Output

Reference : iText Website

Shop and help us

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

Related Posts:

  • Java Best Practice - DeclarationsNumber Per Lineint level; // indentation levelint size; // size of tableis prepared overint level, size;Do not put different type on the same line. Example:int foo, fooarray[]; //WRONGNote: The example above use one space be… Read More
  • Java Best Practice - File OrganizationThe Java file consist of section that should be separated by blank lines and optional comments identifying each sections. Java Source file longer than 2000 lines are cumbersome and should be avoided. Java program must be prop… Read More
  • JSON Tree Model in JacksonIn this Tutorials we are going to see how to read and write JSON Tree model using Jackson JsonNode. The JsonNode is used to read and write tree structured date.It is similar to XML Dom tree. 1. Create project called JSONE… Read More
  • Convert InputStream to String using Java IOIn this tutorials we are going to see how to convert InputStream to String using methods available in Java IO. Create project called JavaMisc. Create package called com.javatutorialscorner.io Create java class called InputSt… Read More
  • Sending HTML Email using Java Mail APIIn this tutorials we are going to see how to send the HTML email message using Java program.I used gmail SMTP server for sending email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package use… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: iText 5 PDF - How to create a custom dashed line separator? Rating: 5 Reviewed By: eHowToNow