Drawing lines

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

14th December 2015
admin-marketing

Switch code for this example

DrawingLines.java
  1. /*
  2.  * Example written in answer to:
  3.  * http://stackoverflow.com/questions/34267096/
  4.  */
  5. package sandbox.graphics;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.CMYKColor;
  10. import com.itextpdf.text.pdf.PdfContentByte;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15.  
  16. /**
  17.  * @author iText
  18.  */
  19. public class DrawingLines {
  20.  
  21.     public static final String DEST = "results/graphics/draw_lines.pdf";
  22.    
  23.     public static void main(String[] args) throws IOException, DocumentException {
  24.         File file = new File(DEST);
  25.         file.getParentFile().mkdirs();
  26.         new DrawingLines().createPdf(DEST);
  27.     }
  28.     public void createPdf(String dest) throws IOException, DocumentException {
  29.         Document document = new Document();
  30.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  31.         document.open();
  32.         PdfContentByte canvas = writer.getDirectContent();
  33.         CMYKColor magentaColor = new CMYKColor(0.f, 1.f, 0.f, 0.f);
  34.         canvas.setColorStroke(magentaColor);
  35.         canvas.moveTo(36, 36);
  36.         canvas.lineTo(36, 806);
  37.         canvas.lineTo(559, 36);
  38.         canvas.lineTo(559, 806);
  39.         canvas.closePathStroke();
  40.         document.close();
  41.     }
  42. }
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