Trabalhar com Clips em PostScript | .NET
Adicionar Clip em Documento PS
Um clipe num documento PS é um caminho que delimita o conteúdo do estado gráfico atual que será apresentado no visualizador ou editor PS. O conteúdo que ficar para além dos limites será recortado.
Um caminho de recorte em .NET pode ser atribuído de três formas:
- por System.Drawing.Drawing2D.GraphicsPath, que pode conter quaisquer formas fechadas;
- por contorno de texto;
- por imagem bicolor de 1 bpp (bits por pixel) como máscara de stencil;
Neste momento, a biblioteca Aspose.Page para .NET oferece a primeira e a segunda forma de recorte. No exemplo abaixo, obtemos um círculo System.Drawing.Drawing2D.GraphicsPath de um retângulo como caminho de recorte e recortamos um retângulo preenchido a azul no mesmo estado gráfico.
Para adicionar um clip ao novo PsDocument com a biblioteca Aspose.Page para .NET, neste exemplo, seguimos os seguintes passos:
- Crie um fluxo de saída para o ficheiro PS resultante.
- Crie o objeto PsSaveOptions com as opções padrão.
- Crie um PsDocument de 1 página com um fluxo de saída já criado e opções de guardar.
- Crie um novo estado gráfico.
- Crie um círculo System.Drawing.Drawing2D.GraphicsPath a partir do retângulo.
- Defina um clipe com este percurso.
- Defina uma pintura para o estado gráfico atual do PsDocument.
- Preencha o percurso do retângulo com a tinta atual.
- Saia do estado gráfico atual para o nível superior.
- Transfira para o local do retângulo preenchido.
- Trace com uma linha tracejada os limites do mesmo retângulo acima do preenchido para mostrar os limites do retângulo preenchido recortado.
- Feche a página.
- Guarde o documento.
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 and 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 and 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}
Para Linux, MacOS e outros sistemas operativos não Windows, oferecemos a utilização do nosso pacote NuGet Aspose.Page.Drawing. Utiliza o backend Aspose.Drawing em vez da biblioteca de sistema System.Drawing.
Assim, importe o namespace Aspose.Page.Drawing em vez do namespace System.Drawing. No trecho de código acima, será utilizado Aspose.Page.Drawing.Rectangle em vez de System.Drawing.Rectangle, Aspose.Page.Drawing.Drawing2D.GraphicsPath em vez de System.Drawing.Drawing2D.GraphicsPath e assim por diante. Os nossos exemplos de código no GitHub contêm todas as substituições necessárias.
Veja como trabalhar com recortes em documentos PS em Java.
O resultado da execução deste código é apresentado como
No próximo exemplo, obtemos um tipo de letra que recorta um retângulo preenchido a azul com o contorno do texto.
Para adicionar um recorte por texto ao novo PsDocument com a biblioteca Aspose.Page para .NET, neste exemplo, seguimos os seguintes passos:
- Criar um fluxo de saída para o ficheiro PS resultante.
- Crie um objeto PsSaveOptions com as opções padrão.
- Crie um PsDocument de 1 página com um fluxo de saída já criado e opções de guardar.
- Crie um novo estado gráfico.
- Crie uma fonte.
- Defina um recorte com texto e a fonte.
- Defina uma pintura para o estado gráfico atual do PsDocument.
- Preencha o percurso do retângulo com a pintura atual.
- Saia do estado gráfico atual para o nível superior.
- Translação para o local do retângulo preenchido.
- Trace com uma linha tracejada os limites do mesmo retângulo acima do preenchido para mostrar os limites do retângulo preenchido recortado.
- Feche a página.
- Guarde o documento.
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 and 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 and 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}
Para Linux, MacOS e outros sistemas operativos não Windows, oferecemos a utilização do nosso pacote NuGet Aspose.Page.Drawing. Utiliza o backend Aspose.Drawing em vez da biblioteca de sistema System.Drawing.
Assim, importe o namespace Aspose.Page.Drawing em vez do System.Drawing. No excerto de código acima, será utilizado Aspose.Page.Drawing.Rectangle em vez de System.Drawing.Rectangle, Aspose.Page.Drawing.Drawing2D.GraphicsPath será utilizado em vez de System.Drawing.Drawing2D.GraphicsPath e assim por diante. Os nossos exemplos de código no GitHub contêm todas as substituições necessárias.
Veja como trabalhar com clips em documentos PS em Java.
O resultado da execução deste código é apresentado como
Pode descarregar exemplos e ficheiros de dados do GitHub.