Робота з кліпами в PostScript | .NET

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

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

Відсічний контур у .NET можна призначити трьома способами:

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

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

  1. Створіть вихідний потік для отриманого файлу PS.
  2. Створіть об’єкт PsSaveOptions із параметрами за замовчуванням.
  3. Створіть 1-сторінковий PsDocument із уже створеним вихідним потоком і параметрами збереження.
  4. Створіть новий графічний стан.
  5. Створіть коло System.Drawing.Drawing2D.GraphicsPath із прямокутника.
  6. Встановіть кліп із цим шляхом.
  7. Установіть фарбу для поточного стану графіки PsDocument.
  8. Заповніть контур прямокутника поточною фарбою.
  9. Вихід із поточного стану графіки на верхній рівень.
  10. Перекладіть на місце зафарбованого прямокутника.
  11. Обведіть пунктирною лінією межі того самого прямокутника над зафарбованим, щоб показати межі обрізаного прямокутника з заливкою.
  12. Закрийте сторінку.
  13. Збережіть документ.
 1//Create an output stream for the PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "Clipping_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with default values
 5    PsSaveOptions options = new PsSaveOptions();
 6
 7    // Create a new 1-paged PS Document
 8    PsDocument document = new PsDocument(outPsStream, options, false);
 9
10    //Create a graphics path from the rectangle
11    GraphicsPath rectangePath = new GraphicsPath();
12    rectangePath.AddRectangle(new RectangleF(0, 0, 300, 200));
13
14    //Save the graphics state in order to return back to this state after transformation
15    document.WriteGraphicsSave();
16
17    //Displace the current graphics state on 100 points to the right і 100 points to the bottom.
18    document.Translate(100, 100);
19
20    //Create a graphics path from the circle
21    GraphicsPath circlePath = new GraphicsPath();
22    circlePath.AddEllipse(new RectangleF(50, 0, 200, 200));
23
24    //Add a clipping by the circle to the current graphics state
25    document.Clip(circlePath);
26
27    //Set the paint in the current graphics state
28    document.SetPaint(new SolidBrush(Color.Blue));
29
30    //Fill the rectangle in the current graphics state (with the clipping)
31    document.Fill(rectangePath);
32
33    //Restore the graphics state to the previus (upper) level
34    document.WriteGraphicsRestore();
35
36    //Displace the upper level graphics state on 100 points to the right і 100 points to the bottom.
37    document.Translate(100, 100);
38
39    Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
40    pen.DashStyle = DashStyle.Dash;
41
42    document.SetStroke(pen);
43
44    //Draw the rectangle in the current graphics state (has no clipping) above the clipped rectngle
45    document.Draw(rectangePath);
46
47    //Close the current page
48    document.ClosePage();
49
50    //Save the document
51    document.Save();
52}

Для Linux, MacOS та інших операційних систем, відмінних від Windows, ми пропонуємо використовувати наш пакет Nuget Aspose.Page.Drawing. Він використовує бекенд Aspose.Drawing замість системної бібліотеки System.Drawing.

Тому імпортуйте простір імен Aspose.Page.Drawing замість System.Drawing. У наведеному вище фрагменті коду Aspose.Page.Drawing.Rectangle буде використано замість System.Drawing.Rectangle, Aspose.Page.Drawing.Drawing2D.GraphicsPath використовуватиметься замість System.Drawing.Drawing2D.GraphicsPath тощо. Наші приклади коду на GitHub містять усі необхідні заміни.

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


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

Вирізка

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

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

  1. Створіть вихідний потік для отриманого файлу PS.
  2. Створіть об’єкт PsSaveOptions із параметрами за замовчуванням.
  3. Створіть 1-сторінковий PsDocument із уже створеним вихідним потоком і параметрами збереження.
  4. Створіть новий графічний стан.
  5. Створіть шрифт.
  6. Встановіть кліп із текстом і шрифтом.
  7. Установіть фарбу для поточного стану графіки PsDocument.
  8. Заповніть контур прямокутника поточною фарбою.
  9. Вихід із поточного стану графіки на верхній рівень.
  10. Перекладіть на місце зафарбованого прямокутника.
  11. Обведіть пунктирною лінією межі того самого прямокутника над зафарбованим, щоб показати межі обрізаного прямокутника з заливкою.
  12. Закрийте сторінку.
  13. Збережіть документ.
 1//Create an output stream for the PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "Clipping_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with default values
 5    PsSaveOptions options = new PsSaveOptions();
 6
 7    // Create a new 1-paged PS Document
 8    PsDocument document = new PsDocument(outPsStream, options, false);
 9
10    //Create a graphics path from the rectangle
11    GraphicsPath rectangePath = new GraphicsPath();
12    rectangePath.AddRectangle(new RectangleF(0, 0, 300, 200));
13
14    //Save the graphics state in order to return back to this state after transformation
15    document.WriteGraphicsSave();
16
17    //Displace the current graphics state on 100 points to the right і 100 points to the bottom.
18    document.Translate(100, 100);
19    
20    //Set the paint in the current graphics state
21    document.SetPaint(new SolidBrush(Color.Blue));
22
23    //Create a font
24    int fontSize = 120;
25    Font font = new Font("Arial", fontSize, FontStyle.Bold);
26
27    //Clip the rectangle by text's outline
28    document.ClipText("ABC", font, 20, fontSize + 10);
29    document.Fill(rectanglePath);
30
31    //Restore the graphics state to the previus (upper) level
32    document.WriteGraphicsRestore();
33
34		//Displace the upper level graphics state on 100 points to the right і 100 points to the bottom.
35    document.Translate(100, 100);
36
37		Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
38    pen.DashStyle = DashStyle.Dash;
39
40    document.SetStroke(pen);
41    
42    //Draw the rectangle in the current graphics state (has no clipping) above the clipped rectangle
43    document.Draw(rectanglePath);
44
45    //Close the current page
46    document.ClosePage();
47
48    //Save the document
49    document.Save();
50}

Для Linux, MacOS та інших операційних систем, відмінних від Windows, ми пропонуємо використовувати наш пакет Nuget Aspose.Page.Drawing. Він використовує бекенд Aspose.Drawing замість системної бібліотеки System.Drawing. Тому імпортуйте простір імен Aspose.Page.Drawing замість System.Drawing. У наведеному вище фрагменті коду Aspose.Page.Drawing.Rectangle буде використано замість System.Drawing.Rectangle, Aspose.Page.Drawing.Drawing2D.GraphicsPath використовуватиметься замість System.Drawing.Drawing2D.GraphicsPath тощо. Наші приклади коду на GitHub містять усі необхідні заміни.

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


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

ClippingByText

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

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.