Робота з фігурами у файлі PS | Python

Додайте форми до документа PS

Додайте прямокутник до PS

Щоб вставити прямокутник у PsDocument за допомогою Aspose.Page для Python через бібліотеку .NET, виконайте такі дії:

  1. Створіть вихідний потік для отриманого файлу PS.
  2. Створіть об’єкт PsSaveOptions із параметрами за замовчуванням.
  3. Створіть 1-сторінковий PsDocument із уже створеним вихідним потоком і параметрами збереження.
  4. Створіть прямокутник aspose.pydrawing.GraphicsPath з прямокутника.
  5. Установіть фарбу для поточного стану графіки PsDocument.
  6. Заповніть прямокутник.
  7. Закрийте сторінку.
  8. Збережіть документ.

Для обведення (контуру) прямокутника перші 4 і останні 2 кроки будуть однаковими, але 5-й і 6-й будуть:

  1. Установіть обведення на поточний графічний стан PsDocument.
  2. Обведіть (обведіть контур) прямокутник.
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_shapes()
 3
 4# Create an output stream for the PostScript document
 5with open(data_dir + "AddRectangle_outPS.ps", "wb") as out_ps_stream:
 6    # Create save options with A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    # Create a graphics path from the first rectangle
13    path = aspose.pydrawing.drawing2d.GraphicsPath()
14    path.add_rectangle(aspose.pydrawing.RectangleF(250, 100, 150, 100))
15    # Set the paint
16    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange))
17    # Fill the rectangle
18    document.fill(path)
19    
20    # Create a graphics path from the second rectangle
21    path = aspose.pydrawing.drawing2d.GraphicsPath()
22    path.add_rectangle(aspose.pydrawing.RectangleF(250, 300, 150, 100))
23    # Set stroke
24    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.red), 3))
25    # Stroke (outline) the rectangle
26    document.draw(path)
27    
28    # Close the current page
29    document.close_page()
30    
31    # Save the document
32    document.save()

Дивіться роботу з фігурами в документах PS у .NET, Java.

Результатом виконання цього коду є

Додати прямокутник

Додайте Ellipse до PS

Щоб додати еліпс до PsDocument, також необхідно виконати 8 кроків:

  1. Створіть вихідний потік для отриманого файлу PS.
  2. Створіть об’єкт PsSaveOptions із параметрами за замовчуванням.
  3. Створіть 1-сторінковий PsDocument із уже створеним вихідним потоком і параметрами збереження.
  4. Створіть еліпс aspose.pydrawing.drawing2d.GraphicsPath із прямокутника.
  5. Встановіть для фарби поточний стан графіки PsDocument.
  6. Заповніть контур еліпса.
  7. Закрийте сторінку.
  8. Збережіть документ.

Щоб обвести (окреслити) еліпс, перші 4 і останні 2 кроки будуть однаковими, але 5-й і 6-й будуть такими:

  1. Установіть штрих на поточний графічний стан PsDocument.
  2. Обведіть (обведіть контур) еліпс.
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_shapes()
 3
 4# Create an output stream for PostScript document
 5with open(data_dir + "AddEllipse_outPS.ps", "wb") as out_ps_stream:
 6    # Create save options with the A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    # Create a graphics path from the first ellipse
13    path = aspose.pydrawing.drawing2d.GraphicsPath()
14    path.add_ellipse(aspose.pydrawing.RectangleF(250, 100, 150, 100))
15    # Set the paint
16    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange))
17    # Fill the ellipse
18    document.fill(path)
19    
20    # Create a graphics path from the second ellipse
21    path = aspose.pydrawing.drawing2d.GraphicsPath()
22    path.add_ellipse(aspose.pydrawing.RectangleF(250, 300, 150, 100))
23    # Set the stroke
24    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.red), 3))
25    # Stroke (outline) the ellipse
26    document.draw(path)
27    
28    # Close the current page
29    document.close_page()
30    
31    # Save the document
32    document.save()

Результатом виконання цього коду є

Додати еліпс

Як ми бачимо, будь-яку форму, як закриту, так і незамкнену, можна заповнити або намалювати PsDocument. Його також можна обрізати, але це буде описано в іншій статті.

Ви можете завантажити приклади і файли даних з GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.