Trabajar con formas en un archivo PS | Java
Agregar formas en un documento PS
Agregar rectángulo a PS
Para agregar un rectángulo a PsDocument con la biblioteca Aspose.Page para Java, debemos realizar los siguientes pasos:
- Cree una secuencia de salida para el archivo PS resultante.
- Cree el objeto PsSaveOptions con opciones predeterminadas.
- Cree un PsDocument de 1 página con un flujo de salida ya creado y opciones para guardar.
- Cree un rectángulo (objeto java.awt.geom.Rectangle2D).
- Establezca una pintura en el estado de gráficos actual de PsDocument.
- Rellena el rectángulo.
- Cierra la página.
- Guarde el documento.
Si necesitamos trazar (delinear) un rectángulo los primeros 4 y los últimos 2 pasos serán iguales, pero los puntos 5 y 6 serán:
Establezca el trazo en el estado de gráficos actual de PsDocument.
Stroke (outline) the rectangle.
1// Add Rectangle to PS document.
2
3String outputFileName = "AddRectangle_outPS.ps";
4
5//Create save options with A4 size
6PsSaveOptions options = new PsSaveOptions();
7
8// Create new 1-paged PS Document
9PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
10
11//Create graphics path from the first rectangle
12GeneralPath path = new GeneralPath();
13path.append(new Rectangle2D.Float(250, 100, 150, 100), false);
14//Set paint
15document.setPaint(Color.ORANGE);
16//Fill the rectangle
17document.fill(path);
18
19//Create graphics path from the second rectangle
20path = new GeneralPath();
21path.append(new Rectangle2D.Float(250, 300, 150, 100), false);
22//Set stroke
23document.setStroke(new java.awt.BasicStroke(3));
24//Stroke (outline) the rectangle
25document.draw(path);
26
27//Close current page
28document.closePage();
29
30//Save the document
31document.save();Vea cómo trabajar con formas en documentos PS en .NET.
El resultado de ejecutar este código aparece como

Agregar elipse a PS
Para agregar elipse a PsDocument también se requieren 8 pasos:
- Cree una secuencia de salida para el archivo PS resultante.
- Cree el objeto PsSaveOptions con opciones predeterminadas.
- Cree un PsDocument de 1 página con un flujo de salida ya creado y opciones para guardar.
- Cree una elipse (objeto java.awt.geom.Ellipse2D).
- Establezca la pintura en el estado de gráficos actual de PsDocument.
- Rellena el camino de la elipse.
- Cierra la página.
- Guarde el documento.
Si necesitamos trazar (delinear) una elipse, los primeros 4 y los últimos 2 pasos serán iguales, pero los puntos 5 y 6 serán:
- Establezca el trazo en el estado de gráficos actual de PsDocument.
- Traza (delinea) la elipse:
1// Add ellipse to PS document.
2
3String outputFileName = "AddEllipse_outPS.ps";
4
5//Create save options with A4 size
6PsSaveOptions options = new PsSaveOptions();
7
8// Create new 1-paged PS Document
9PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
10
11//Create graphics path from the first ellipse
12GeneralPath path = new GeneralPath();
13path.append(new java.awt.geom.Ellipse2D.Float(250, 100, 150, 100), false);
14//Set paint
15document.setPaint(Color.ORANGE);
16//Fill the ellipse
17document.fill(path);
18
19//Create graphics path from the second ellipse
20path = new GeneralPath();
21path.append(new java.awt.geom.Ellipse2D.Float(250, 300, 150, 100), false);
22//Set stroke
23document.setStroke(new java.awt.BasicStroke(3));
24//Stroke (outline) the ellipse
25document.draw(path);
26
27//Close current page
28document.closePage();
29
30//Save the document
31document.save();El resultado de ejecutar este código aparece como

Como podemos ver, cualquier forma, tanto cerrada como no cerrada, se puede rellenar o dibujar con PsDocument. También se puede recortar, pero se describirá en otro artículo.
Puede descargar ejemplos y archivos de datos desde GitHub.