Agregar formas de línea a PDF en Java

Agregar una línea discontinua

  1. Cree un nuevo PDF Document.
  2. Agregue un Page al documento.
  3. Cree un Graph contenedor y añádalo a la página.
  4. Cree el Line forma y configure sus coordenadas.
  5. Añada el Line al Graph contenedor.
  6. Guarde el PDF de salida Document.
public static void addLine(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Graph graph = new Graph(100.0, 400.0);
        page.getParagraphs().add(graph);

        Line line = new Line(new float[]{100, 100, 200, 100});
        line.getGraphInfo().setDashArray(new int[]{0, 1, 0});
        line.getGraphInfo().setDashPhase(1);
        graph.getShapes().addItem(line);

        document.save(outputFile.toString());
    }
}

Agregar una línea punteada o discontinua de color

addDottedDashedLine utiliza las mismas coordenadas y configuraciones de guiones, pero también aplica Color.getRed().

Dibujar líneas a través de la página

  1. Cree un nuevo PDF Document.
  2. Agregue un Page al documento.
  3. Cree un Graph contenedor y añádalo a la página.
  4. Cree el Line forma y configure sus coordenadas.
  5. Añada el Line al Graph contenedor.
  6. Guarde el PDF de salida Document.
public static void drawLineAcrossPage(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        page.getPageInfo().getMargin().setLeft(0);
        page.getPageInfo().getMargin().setRight(0);
        page.getPageInfo().getMargin().setBottom(0);
        page.getPageInfo().getMargin().setTop(0);

        Graph graph = new Graph(page.getPageInfo().getWidth(), page.getPageInfo().getHeight());
        Line line = new Line(new float[]{
                (float) page.getRect().getLLX(),
                0,
                (float) page.getPageInfo().getWidth(),
                (float) page.getRect().getURY()
        });
        graph.getShapes().addItem(line);
        page.getParagraphs().add(graph);

        document.save(outputFile.toString());
    }
}