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

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

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

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

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

  1. Створіть вихідний потік для отриманого файлу PS.
  2. Створіть об’єкт PsSaveOptions із параметрами за замовчуванням.
  3. Створіть 1-сторінковий PsDocument із уже створеним вихідним потоком і параметрами збереження.
  4. Створіть новий графічний стан.
  5. Створіть форму кола (об’єкт java.awt.geom.Ellipse2D).
  6. Встановіть кліп із цим шляхом.
  7. Установіть фарбу для поточного стану графіки PsDocument.
  8. Заповніть контур прямокутника поточною фарбою.
  9. Вихід із поточного стану графіки на верхній рівень.
  10. Перекладіть на місце зафарбованого прямокутника.
  11. Обведіть пунктирною лінією межі того самого прямокутника над зафарбованим, щоб показати межі обрізаного прямокутника з заливкою.
  12. Закрийте сторінку.
  13. Збережіть документ.
 1//Create an output stream for the PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "Clipping_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6// Create a new PS Document with the page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Create a rectangle
10Shape rectangle = new Rectangle2D.Float(0, 0, 300, 200);
11
12//Set the paint in the upper level graphics state
13document.setPaint(Color.BLUE);
14
15//Save the  graphics state to return back to this state after the transformation
16document.writeGraphicsSave();
17
18//Displace the current graphics state on 100 points to the right і 100 points to the bottom.
19document.translate(100, 100);
20
21//Create a circle shape
22Shape circle = new Ellipse2D.Float(50, 0, 200, 200);
23
24//Add clipping by the circle to the current graphics state
25document.clip(circle);
26
27//Fill the rectangle in the current graphics state (with clipping)
28document.fill(rectangle);
29
30//Restore the graphics state to the previus (upper) level
31document.writeGraphicsRestore();
32
33//Displace the upper-level graphics state on 100 points to the right і 100 points to the bottom.
34document.translate(100, 100);
35
36BasicStroke stroke = new BasicStroke(2,
37        BasicStroke.CAP_BUTT,
38        BasicStroke.JOIN_MITER,
39        10.0f, new float []{5.0f}, 0.0f);
40
41document.setStroke(stroke);
42
43//Draw the rectangle in the current graphics state (has no clipping) above the clipped rectangle
44document.draw(rectangle);
45
46//Close the current page
47document.closePage();
48//Save the document
49document.save();

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

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

Вирізка

У наступному прикладі ми отримуємо шрифт, який використовується для вирізання синього прямокутника з контуром тексту.

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

  1. Створіть вихідний потік для отриманого файлу PS.
  2. Створіть об’єкт PsSaveOptions із параметрами за замовчуванням.
  3. Створіть 1-сторінковий PsDocument із уже створеним вихідним потоком і параметрами збереження.
  4. Створіть новий графічний стан.
  5. Створіть шрифт.
  6. Встановіть кліп із текстом і шрифтом.
  7. Установіть фарбу для поточного стану графіки PsDocument.
  8. Заповніть контур прямокутника поточною фарбою.
  9. Вихід із поточного стану графіки на верхній рівень.
  10. Перекладіть на місце зафарбованого прямокутника.
  11. Обведіть пунктирною лінією межі того самого прямокутника над зафарбованим, щоб показати межі обрізаного прямокутника з заливкою.
  12. Закрийте сторінку.
  13. Збережіть документ.
 1//Create output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "Clipping_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6// Create a new PS Document with the page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Create a rectangle
10Shape rectangle = new Rectangle2D.Float(0, 0, 300, 200);
11
12//Set paint in the upper-level graphics state
13document.setPaint(Color.BLUE);
14
15//Save graphics state to return back to this state after the transformation
16document.writeGraphicsSave();
17
18//Displace current graphics state on 100 points to the right і 100 points to the bottom.
19document.translate(100, 100);
20
21int fontSize = 120;
22Font font = new Font("Arial", Font.BOLD, fontSize);
23
24//Clip rectangle by text's outline
25document.clipText("ABC", font, 20, fontSize + 10);		
26document.fill(rectangle);
27
28//Restore graphics state to the previous (upper) level
29document.writeGraphicsRestore();
30
31//Displace upper-level graphics state on 100 points to the right і 100 points to the bottom.
32document.translate(100, 100);
33
34BasicStroke stroke = new BasicStroke(2,
35        BasicStroke.CAP_BUTT,
36        BasicStroke.JOIN_MITER,
37        10.0f, new float []{5.0f}, 0.0f);
38
39document.setStroke(stroke);
40
41//Draw the rectangle in the current graphics state (has no clipping) above the clipped rectangle
42document.draw(rectangle);
43
44//Close the current page
45document.closePage();
46//Save the document
47document.save();

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


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

ClippingByText

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

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.