Agregar formas de línea a PDF en Java
Contents
[
Hide
]
Agregar una línea discontinua
- Cree un nuevo PDF
Document. - Agregue un
Pageal documento. - Cree un
Graphcontenedor y añádalo a la página. - Cree el
Lineforma y configure sus coordenadas. - Añada el
LinealGraphcontenedor. - 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
- Cree un nuevo PDF
Document. - Agregue un
Pageal documento. - Cree un
Graphcontenedor y añádalo a la página. - Cree el
Lineforma y configure sus coordenadas. - Añada el
LinealGraphcontenedor. - 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());
}
}