Trabajar con formas en PostScript | .NET

Agregar formas en un documento PS

Agregar rectángulo a PS

Para agregar un rectángulo a PsDocument con la biblioteca Aspose.Page para .NET debemos realizar los siguientes pasos:

  1. Cree una secuencia de salida para el archivo PS resultante.
  2. Cree el objeto PsSaveOptions con opciones predeterminadas.
  3. Cree un PsDocument de 1 página con un flujo de salida ya creado y opciones para guardar.
  4. Cree un rectángulo System.Drawing.Drawing2D.GraphicsPath a partir del rectángulo.
  5. Establezca una pintura en el estado de gráficos actual de PsDocument.
  6. Rellena el trazado del rectángulo.
  7. Cierra la página.
  8. 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:

  1. Establezca el trazo en el estado de gráficos actual de PsDocument.

  2. Traza (delinea) el trazado del rectángulo.

 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(OutputDir + outputFileName, options, false);
10
11//Create graphics path from the first rectangle
12GraphicsPath path = new GraphicsPath();
13path.AddRectangle(new RectangleF(250, 100, 150, 100));
14//Set paint
15document.SetPaint(new SolidBrush(Color.Orange));
16//Fill the rectangle
17document.Fill(path);
18
19//Create graphics path from the second rectangle
20path = new GraphicsPath();
21path.AddRectangle(new RectangleF(250, 300, 150, 100));
22//Set stroke
23document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
24//Stroke (outline) the rectangle
25document.Draw(path);
26
27//Close current page
28document.ClosePage();
29
30//Save the document
31document.Save();

Para Linux, MacOS y otros sistemas operativos distintos de Windows, ofrecemos utilizar nuestro paquete Nuget Aspose.Page.Drawing. Utiliza el backend Aspose.Drawing en lugar de la biblioteca del sistema System.Drawing.

Así que importe el espacio de nombres Aspose.Page.Drawing en lugar del System.Drawing. En los fragmentos de código anteriores y siguientes se utilizará Aspose.Page.Drawing.Rectangle en lugar de System.Drawing.Rectangle, se utilizará Aspose.Page.Drawing.Drawing2D.GraphicsPath en lugar de System.Drawing.Drawing2D.GraphicsPath, etc. . Nuestros ejemplos de código en GitHub contienen todas las sustituciones necesarias.

Vea cómo trabajar con formas en documentos PS en Java.


El resultado de ejecutar este código aparece como

Agregar rectángulo

Agregar elipse a PS

Para agregar elipse a PsDocument también se requieren 8 pasos:

  1. Cree una secuencia de salida para el archivo PS resultante.
  2. Cree el objeto PsSaveOptions con opciones predeterminadas.
  3. Cree un PsDocument de 1 página con un flujo de salida ya creado y opciones para guardar.
  4. Cree una elipse System.Drawing.Drawing2D.GraphicsPath a partir del rectángulo.
  5. Establezca la pintura en el estado de gráficos actual de PsDocument.
  6. Rellena el camino de la elipse.
  7. Cierra la página.
  8. 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:

  1. Establezca el trazo en el estado de gráficos actual de PsDocument.

  2. Stroke (outline) the ellipse path.

 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(OutputDir + outputFileName, options, false);
10
11//Create graphics path from the first ellipse
12GraphicsPath path = new GraphicsPath();
13path.AddEllipse(new RectangleF(250, 100, 150, 100));
14//Set paint
15document.SetPaint(new SolidBrush(Color.Orange));
16//Fill the ellipse
17document.Fill(path);
18
19//Create graphics path from the second ellipse
20path = new GraphicsPath();
21path.AddEllipse(new RectangleF(250, 300, 150, 100));
22//Set stroke
23document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
24//Stroke (outline) the ellipse
25document.Draw(path);
26
27//Close current page
28document.ClosePage();
29
30//Save the document
31document.Save();
Example-AddEllipsePS.cs hosted with ❤ by GitHub

El resultado de ejecutar este código aparece como

Agregar elipse

Como podemos ver, cualquier forma, tanto cerrada como no cerrada, que se pueda poner en System.Drawing.Drawing2D.GraphicsPath 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.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.