Drawing lines

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

31st May 2016
admin-marketing

Switch code for this example

DrawingLines.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written in answer to:
  10.  * http://stackoverflow.com/questions/34267096/
  11.  */
  12. package com.itextpdf.samples.sandbox.graphics;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.color.DeviceCmyk;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.File;
  25.  
  26. @Category(SampleTest.class)
  27. public class DrawingLines extends GenericTest {
  28.     public static final String DEST = "./target/test/resources/sandbox/graphics/drawing_lines.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new DrawingLines().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  39.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  40.         Color magentaColor = new DeviceCmyk(0.f, 1.f, 0.f, 0.f);
  41.         canvas.setStrokeColor(magentaColor)
  42.                 .moveTo(36, 36)
  43.                 .lineTo(36, 806)
  44.                 .lineTo(559, 36)
  45.                 .lineTo(559, 806)
  46.                 .closePathStroke();
  47.         pdfDoc.close();
  48.     }
  49. }
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