Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用AdobeAcrobat。此外,API还提供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 instanceDocument doc = new Document();// Add page to pages collection of PDF filePage page = doc.Pages.Add();// Create Graph instanceAspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(100, 400);// Add graph object to paragraphs collection of page instancepage.Paragraphs.Add(graph);// Create Rectangle instanceAspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 120);// Specify fill color for Graph objectrect.GraphInfo.FillColor = Aspose.Pdf.Color.Red;// Add rectangle object to shapes collection of Graph objectgraph.Shapes.Add(rect);dataDir = dataDir + "CreateFilledRectangle_out.pdf";// Save PDF filedoc.Save(dataDir);
在图形对象中添加文本
Aspose.PDF支持在Graph对象内添加文本。图形对象的文本属性提供了设置图形对象文本的选项。以下代码段显示了如何在Rectangle对象内添加文本。
// Open documentstring 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 textAspose.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 instanceDocument doc = new Document();// Add page to pages collection of PDF filePage page = doc.Pages.Add();// Create Graph instanceAspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(100, 400);// Add graph object to paragraphs collection of page instancepage.Paragraphs.Add(graph);// Create Rectangle instanceAspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { 100, 100, 200, 100 });// Specify fill color for Graph objectline.GraphInfo.DashArray = new int[] { 0, 1, 0 };line.GraphInfo.DashPhase = 1;// Add rectangle object to shapes collection of Graph objectgraph.Shapes.Add(line);dataDir = dataDir + "AddLineObject_out.pdf";// Save PDF filedoc.Save(dataDir);
在页面上画线
Aspose.PDF支持线对象绘制从左下角到右上角以及从左上角到右下角的十字。请仔细阅读以下代码片段以实现此要求。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();// Create Document instanceDocument pDoc = new Document();// Add page to pages collection of PDF documentPage pg = pDoc.Pages.Add();// Set page margin on all sides as 0pg.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 dimensionsAspose.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 pageAspose.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 objectgraph.Shapes.Add(line);// Draw line from Top-Left corner of page to Bottom-Right corner of pageAspose.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 objectgraph.Shapes.Add(line2);// Add Graph object to paragraphs collection of pagepg.Paragraphs.Add(graph);dataDir = dataDir + "DrawingLine_out.pdf";// Save PDF filepDoc.Save(dataDir);
使用Alpha颜色通道创建矩形
Aspose.PDF支持用某种颜色填充矩形对象。矩形对象也可以具有Alpha颜色通道以提供透明外观。以下代码段显示了如何添加具有Alpha颜色通道的Rectangle对象。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();// Instantiate Document instanceDocument doc = new Document();// Add page to pages collection of PDF fileAspose.Pdf.Page page = doc.Pages.Add();// Create Graph instanceAspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400);// Create rectangle object with specific dimensionsAspose.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 valuerect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(12957183)));// Add rectangle object to shapes collection of Graph instancecanvas.Shapes.Add(rect);// Create second rectangle objectAspose.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 objectpage.Paragraphs.Add(canvas);dataDir = dataDir + "CreateRectangleWithAlphaColor_out.pdf";// Save PDF filedoc.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 RGBAspose.Pdf.Color alphaColor = Aspose.Pdf.Color.FromArgb(alpha, red, green, blue); // Provide alpha channel// Instantiate Document objectDocument document = new Document();// Add page to pages collection of PDF filePage page = document.Pages.Add();// Create Graph object with certain dimensionsAspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(300, 400);// Set border for Drawing objectgraph.Border = (new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Black));// Add graph object to paragraphs collection of Page instancepage.Paragraphs.Add(graph);// Create Rectangle object with certain dimensionsAspose.Pdf.Drawing.Rectangle rectangle = new Aspose.Pdf.Drawing.Rectangle(0, 0, 100, 50);// Create graphInfo object for Rectangle instanceAspose.Pdf.GraphInfo graphInfo = rectangle.GraphInfo;// Set color information for GraphInfo instancegraphInfo.Color = (Aspose.Pdf.Color.Red);// Set fill color for GraphInfographInfo.FillColor = (alphaColor);// Add rectangle shape to shapes collection of graph objectgraph.Shapes.Add(rectangle);dataDir = dataDir + "AddDrawing_out.pdf";// Save PDF filedocument.Save(dataDir);
控制矩形的Z顺序
在PDF文件中添加同一对象的多个实例时,我们可以通过指定Z顺序来控制它们的呈现。当我们需要在彼此上方渲染对象时,也可以使用Z顺序。下面的代码片段显示了将Rectangle对象彼此呈现的步骤。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();// Instantiate Document class objectDocument doc1 = new Document();/// Add page to pages collection of PDF fileAspose.Pdf.Page page1 = doc1.Pages.Add();// Set size of PDF pagepage1.SetPageSize(375, 300);// Set left margin for page object as 0page1.PageInfo.Margin.Left = 0;// Set top margin of page object as 0page1.PageInfo.Margin.Top = 0;// Create a new rectangle with Color as Red, Z-Order as 0 and certain dimensionsAddRectangle(page1, 50, 40, 60, 40, Aspose.Pdf.Color.Red, 2);// Create a new rectangle with Color as Blue, Z-Order as 0 and certain dimensionsAddRectangle(page1, 20, 20, 30, 30, Aspose.Pdf.Color.Blue, 1);// Create a new rectangle with Color as Green, Z-Order as 0 and certain dimensionsAddRectangle(page1, 40, 40, 60, 30, Aspose.Pdf.Color.Green, 0);dataDir = dataDir + "ControlRectangleZOrder_out.pdf";// Save resultant PDF filedoc1.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 objectDocument doc1 = new Document();/// Add page to pages collection of PDF fileAspose.Pdf.Page page1 = doc1.Pages.Add();// Set size of PDF pagepage1.SetPageSize(375, 300);// Set left margin for page object as 0page1.PageInfo.Margin.Left = 0;// Set top margin of page object as 0page1.PageInfo.Margin.Top = 0;// Create a new rectangle with Color as Red, Z-Order as 0 and certain dimensionsAddRectangle(page1, 50, 40, 60, 40, Aspose.Pdf.Color.Red, 2);// Create a new rectangle with Color as Blue, Z-Order as 0 and certain dimensionsAddRectangle(page1, 20, 20, 30, 30, Aspose.Pdf.Color.Blue, 1);// Create a new rectangle with Color as Green, Z-Order as 0 and certain dimensionsAddRectangle(page1, 40, 40, 60, 30, Aspose.Pdf.Color.Green, 0);dataDir = dataDir + "ControlRectangleZOrder_out.pdf";// Save resultant PDF filedoc1.Save(dataDir);
还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!