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

Додайте кліп у документ PS

У документі PS кліп — це межа, визначена шляхом, який обмежує видимість вмісту в межах поточного стану графіки в програмах перегляду або редакторів PS. Будь-який вміст, що виходить за межі цієї межі, буде скорочено.
У Python відсічні контури можна призначати трьома способами:

Наразі бібліотека Aspose.Page для Python через .NET підтримує перший і другий методи відсікання. У наведеному нижче прикладі ми створюємо круглу фігуру як відсічний контур і використовуємо її, щоб відрізати прямокутник із синьою заливкою в тому самому графічному стані.
У наведеному нижче прикладі ми отримуємо форму кола як відсічний контур і відрізаємо прямокутник із синьою заливкою в такому самому графічному стані.

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

  1. Створіть вихідний потік для отриманого файлу PS.
  2. Створіть об’єкт PsSaveOptions із параметрами за замовчуванням.
  3. Створіть 1-сторінковий PsDocument із уже створеним вихідним потоком і параметрами збереження.
  4. Створіть новий графічний стан.
  5. Створіть форму кола (aspose.pydrawing.GraphicsPath об’єкт).
  6. Встановіть кліп із цим шляхом.
  7. Установіть фарбу для поточного стану графіки PsDocument.
  8. Заповніть контур прямокутника поточною фарбою.
  9. Вийти з поточного стану графіки на верхній рівень.
  10. Перекладіть на місце зафарбованого прямокутника.
  11. Обведіть пунктирною лінією межі того самого прямокутника над зафарбованим, щоб показати межі обрізаного прямокутника з заливкою.
  12. Закрийте сторінку.
  13. Збережіть документ.
 1# Create an output stream for PostScript document
 2with open(data_dir + "Clipping_outPS.ps", "wb") as out_ps_stream:
 3    # Create the save options with default values
 4    options = PsSaveOptions()
 5    
 6    # Create new 1-paged PS Document
 7    document = PsDocument(out_ps_stream, options, False)
 8    
 9    # Create a graphics path from the rectangle
10    rectange_path = aspose.pydrawing.drawing2d.GraphicsPath()
11    rectange_path.add_rectangle(aspose.pydrawing.RectangleF(0, 0, 300, 200))
12    
13    ##################################### Clipping by the shape //////////////////////////////////////////////////////////////////////
14    
15    # Save the graphics state in order to return back to this state after the transformation
16    document.write_graphics_save()
17    
18    # Displace the current graphics state on 100 points to the right і 100 points to the bottom.
19    document.translate(100, 100)
20    
21    # Create a graphics path from the circle
22    circle_path = aspose.pydrawing.drawing2d.GraphicsPath()
23    circle_path.add_ellipse(aspose.pydrawing.RectangleF(50, 0, 200, 200))
24    
25    # Add clipping by circle to the current graphics state
26    document.clip(circle_path)
27    
28    # Set the paint in the current graphics state
29    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue))
30    
31    # Fill the rectangle in the current graphics state (with clipping)
32    document.fill(rectange_path)
33    
34    # Restore the graphics state to the previus (upper) level
35    document.write_graphics_restore()
36    
37    # Displace the upper level graphics state on 100 points to the right і 100 points to the bottom.
38    document.translate(100, 100)
39    
40    pen = aspose.pydrawing.Pen(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue))
41    pen.width = float(2)
42    pen.dash_style = aspose.pydrawing.drawing2d.DashStyle.DASH
43    
44    document.set_stroke(pen)
45    
46    # Draw the rectangle in the current graphics state (has no clipping) above clipped rectangle
47    document.draw(rectange_path)
48    
49    ########################################################################################################################
50    
51    # Close the current page
52    document.close_page()
53    
54    # Save the document
55    document.save()

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

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

Вирізка

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

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.