彩票走势图

PDF管理控件Aspose.PDF for .Net使用教程(四十三):图形的使用

翻译|使用教程|编辑:李显亮|2020-07-30 14:12:44.333|阅读 567 次

概述:在本系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何使用图形,包括创建填充的矩形对象、在图形对象中添加文本、将线对象添加到PDF、在页面上画线等。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

相关链接:

Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。

在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何使用图形,包括:

  • 创建填充的矩形对象
  • 在图形对象中添加文本
  • 将线对象添加到PDF
  • 在页面上画线
  • 使用Alpha颜色通道创建矩形
  • 添加具有透明颜色的图形
  • 控制矩形的Z顺序
  • 添加带有渐变填充的图形

>>Aspose.PDF for .NET更新至最新版v20.7,欢迎下载体验。


对于使用Adobe Acrobat Writer或其他PDF处理应用程序的开发人员,将图形添加到PDF文档是一项非常常见的任务。PDF应用程序中可以使用多种图形。

Aspose.PDF还支持将图形添加到PDF文档。为此,Graph提供了该类。Graph是段落级别的元素,可以Paragraphs在Page实例中添加到集合中。

创建填充的矩形对象

用于.NET的Aspose.PDF支持将图形对象(例如图形,线,矩形等)添加到PDF文档的功能。它还提供了用某种颜色填充矩形对象的功能。

下面的代码片段显示了如何添加一个Rectangle充满颜色的对象。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();

// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Create Graph instance
Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(100, 400);
// Add graph object to paragraphs collection of page instance
page.Paragraphs.Add(graph);
// Create Rectangle instance
Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 120);
// Specify fill color for Graph object
rect.GraphInfo.FillColor = Aspose.Pdf.Color.Red;
// Add rectangle object to shapes collection of Graph object
graph.Shapes.Add(rect);
dataDir = dataDir + "CreateFilledRectangle_out.pdf";
// Save PDF file
doc.Save(dataDir);

在图形对象中添加文本

Aspose.PDF支持在Graph对象内添加文本。图形对象的文本属性提供了设置图形对象文本的选项。以下代码段显示了如何在Rectangle对象内添加文本。

// Open document
string outFile = "Graph.pdf";
Aspose.PDF.Document pdfDoc = new Aspose.PDF.Document();
Aspose.PDF.Page pdfPage = pdfDoc.Pages.Add();
Aspose.PDF.Drawing.Graph graph = new Aspose.PDF.Drawing.Graph(500, 100);
pdfPage.Paragraphs.Add(graph);
 
//Add rectangle with text
Aspose.PDF.Drawing.Rectangle rect = new Aspose.PDF.Drawing.Rectangle(0, 30, 50, 40);
rect.GraphInfo.LineWidth = 1f;
rect.GraphInfo.Color = Aspose.PDF.Color.Black;
rect.Text = new TextFragment("Rectangle");
graph.Shapes.Add(rect);
pdfDoc.Save(outFile);

将线对象添加到PDF

Aspose.PDF支持利用杠杆来添加Line对象,还可以在其中指定虚线元素,颜色和Line元素的其他格式。下面的代码片段显示了如何添加一个Rectangle充满颜色的对象。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();

// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Create Graph instance
Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(100, 400);
// Add graph object to paragraphs collection of page instance
page.Paragraphs.Add(graph);
// Create Rectangle instance
Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { 100, 100, 200, 100 });
// Specify fill color for Graph object
line.GraphInfo.DashArray = new int[] { 0, 1, 0 };
line.GraphInfo.DashPhase = 1;
// Add rectangle object to shapes collection of Graph object
graph.Shapes.Add(line);
dataDir = dataDir + "AddLineObject_out.pdf";
// Save PDF file
doc.Save(dataDir);

在页面上画线

Aspose.PDF支持线对象绘制从左下角到右上角以及从左上角到右下角的十字。请仔细阅读以下代码片段以实现此要求。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();

// Create Document instance
Document pDoc = new Document();
// Add page to pages collection of PDF document
Page pg = pDoc.Pages.Add();
// Set page margin on all sides as 0
pg.PageInfo.Margin.Left = pg.PageInfo.Margin.Right = pg.PageInfo.Margin.Bottom = pg.PageInfo.Margin.Top = 0;
// Create Graph object with Width and Height equal to page dimensions
Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)pg.PageInfo.Width , (float)pg.PageInfo.Height);
// Create first line object starting from Lower-Left to Top-Right corner of page
Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { (float)pg.Rect.LLX, 0, (float)pg.PageInfo.Width, (float)pg.Rect.URY });
// Add line to shapes collection of Graph object
graph.Shapes.Add(line);
// Draw line from Top-Left corner of page to Bottom-Right corner of page
Aspose.Pdf.Drawing.Line line2 = new Aspose.Pdf.Drawing.Line(new float[] { 0, (float)pg.Rect.URY, (float)pg.PageInfo.Width, (float)pg.Rect.LLX });
// Add line to shapes collection of Graph object
graph.Shapes.Add(line2);
// Add Graph object to paragraphs collection of page
pg.Paragraphs.Add(graph);

dataDir = dataDir + "DrawingLine_out.pdf";
// Save PDF file
pDoc.Save(dataDir);

使用Alpha颜色通道创建矩形

Aspose.PDF支持用某种颜色填充矩形对象。矩形对象也可以具有Alpha颜色通道以提供透明外观。以下代码段显示了如何添加具有Alpha颜色通道的Rectangle对象。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();

// Instantiate Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Aspose.Pdf.Page page = doc.Pages.Add();
// Create Graph instance
Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400);
// Create rectangle object with specific dimensions
Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 100);
// Set graph fill color from System.Drawing.Color structure from a 32-bit ARGB value
rect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(12957183)));
// Add rectangle object to shapes collection of Graph instance
canvas.Shapes.Add(rect);

// Create second rectangle object
Aspose.Pdf.Drawing.Rectangle rect1 = new Aspose.Pdf.Drawing.Rectangle(200, 150, 200, 100);
rect1.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(16118015)));
canvas.Shapes.Add(rect1);
// Add graph instance to paragraph collection of page object
page.Paragraphs.Add(canvas);

dataDir = dataDir + "CreateRectangleWithAlphaColor_out.pdf";
// Save PDF file
doc.Save(dataDir);

使用Alpha颜色通道创建矩形

创建矩形,圆形,Eclipse等图形对象时,我们提供边框的颜色信息以及填充颜色信息。为了具有透明的填充效果,可以使用Aspose.PDF.Color对象的FromArgb(..)方法。请查看以下代码片段,该代码片段演示了使用透明颜色填充矩形对象的功能。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();

int alpha = 10;
int green = 0;
int red = 100;
int blue = 0;
// Create Color object using Alpha RGB 
Aspose.Pdf.Color alphaColor = Aspose.Pdf.Color.FromArgb(alpha, red, green, blue); // Provide alpha channel
// Instantiate Document object
Document document = new Document();
// Add page to pages collection of PDF file
Page page = document.Pages.Add();
// Create Graph object with certain dimensions
Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(300, 400);
// Set border for Drawing object
graph.Border = (new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Black));
// Add graph object to paragraphs collection of Page instance
page.Paragraphs.Add(graph);
// Create Rectangle object with certain dimensions
Aspose.Pdf.Drawing.Rectangle rectangle = new Aspose.Pdf.Drawing.Rectangle(0, 0, 100, 50);
// Create graphInfo object for Rectangle instance
Aspose.Pdf.GraphInfo graphInfo = rectangle.GraphInfo;
// Set color information for GraphInfo instance
graphInfo.Color = (Aspose.Pdf.Color.Red);
// Set fill color for GraphInfo
graphInfo.FillColor = (alphaColor);
// Add rectangle shape to shapes collection of graph object
graph.Shapes.Add(rectangle);
dataDir = dataDir + "AddDrawing_out.pdf";
// Save PDF file
document.Save(dataDir);

控制矩形的Z顺序

在PDF文件中添加同一对象的多个实例时,我们可以通过指定Z顺序来控制它们的呈现。当我们需要在彼此上方渲染对象时,也可以使用Z顺序。下面的代码片段显示了将Rectangle对象彼此呈现的步骤。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();

// Instantiate Document class object
Document doc1 = new Document();
/// Add page to pages collection of PDF file
Aspose.Pdf.Page page1 = doc1.Pages.Add();
// Set size of PDF page
page1.SetPageSize(375, 300);
// Set left margin for page object as 0
page1.PageInfo.Margin.Left = 0;
// Set top margin of page object as 0
page1.PageInfo.Margin.Top = 0;
// Create a new rectangle with Color as Red, Z-Order as 0 and certain dimensions
AddRectangle(page1, 50, 40, 60, 40, Aspose.Pdf.Color.Red, 2);
// Create a new rectangle with Color as Blue, Z-Order as 0 and certain dimensions
AddRectangle(page1, 20, 20, 30, 30, Aspose.Pdf.Color.Blue, 1);
// Create a new rectangle with Color as Green, Z-Order as 0 and certain dimensions
AddRectangle(page1, 40, 40, 60, 30, Aspose.Pdf.Color.Green, 0);
dataDir = dataDir + "ControlRectangleZOrder_out.pdf";
// Save resultant PDF file
doc1.Save(dataDir);


private static void AddRectangle(Aspose.Pdf.Page page, float x, float y, float width, float height, Aspose.Pdf.Color color, int zindex)
{
    // Create graph object with dimensions same as specified for Rectangle object
    Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(width, height);
    // Can we change the position of graph instance
    graph.IsChangePosition = false;
    // Set Left coordinate position for Graph instance
    graph.Left = x;
    // Set Top coordinate position for Graph object
    graph.Top = y;
    // Add a rectangle inside the "graph"
    Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(0, 0, width, height);
    // Set rectangle fill color
    rect.GraphInfo.FillColor = color;
    // Color of graph object
    rect.GraphInfo.Color = color;
    // Add rectangle to shapes collection of graph instance
    graph.Shapes.Add(rect);
    // Set Z-Index for rectangle object
    graph.ZIndex = zindex;
    // Add graph to paragraphs collection of page object
    page.Paragraphs.Add(graph);
}

添加带有渐变填充的图形

Aspose.PDF支持创建纯PDF文档的功能,该文档具有从一种专色/印刷色到另一种专色/印刷色的单一渐变。在下面的代码示例中对此进行了说明。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();

// Instantiate Document class object
Document doc1 = new Document();
/// Add page to pages collection of PDF file
Aspose.Pdf.Page page1 = doc1.Pages.Add();
// Set size of PDF page
page1.SetPageSize(375, 300);
// Set left margin for page object as 0
page1.PageInfo.Margin.Left = 0;
// Set top margin of page object as 0
page1.PageInfo.Margin.Top = 0;
// Create a new rectangle with Color as Red, Z-Order as 0 and certain dimensions
AddRectangle(page1, 50, 40, 60, 40, Aspose.Pdf.Color.Red, 2);
// Create a new rectangle with Color as Blue, Z-Order as 0 and certain dimensions
AddRectangle(page1, 20, 20, 30, 30, Aspose.Pdf.Color.Blue, 1);
// Create a new rectangle with Color as Green, Z-Order as 0 and certain dimensions
AddRectangle(page1, 40, 40, 60, 30, Aspose.Pdf.Color.Green, 0);
dataDir = dataDir + "ControlRectangleZOrder_out.pdf";
// Save resultant PDF file
doc1.Save(dataDir);


还想要更多吗?您可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询



标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP