Add Line Object to PDF file
The following code snippet also work with Aspose.PDF.Drawing library.
Add Line object
Aspose.PDF for .NET supports the feature to add graph objects (for example graph, line, rectangle etc.) to PDF documents. You also get the leverage to add Line object where you can also specify the dash pattern, color and other formatting for Line element.
Follow the steps below:
-
Create a new PDF Document.
-
Add Page to pages collection of PDF file.
-
Create Graph instance.
-
Add Graph object to paragraphs collection of page instance.
-
Create Rectangle instance.
-
Set line width.
-
Add Rectangle object to shapes collection of Graph object.
-
Save your PDF file.
The following code snippet shows how to add a Rectangle object that is filled with color.
private static void AddLineObjectToPDF()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create Document instance using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of PDF file
var page = document.Pages.Add();
// Create Graph instance
var graph = new Aspose.Pdf.Drawing.Graph(100, 400);
// Add graph object to paragraphs collection of page instance
page.Paragraphs.Add(graph);
// Create Line instance with specified coordinates
var line = new Aspose.Pdf.Drawing.Line(new float[] { 100, 100, 200, 100 });
// Specify dash settings for the line
line.GraphInfo.DashArray = new int[] { 0, 1, 0 };
line.GraphInfo.DashPhase = 1;
// Add line object to shapes collection of Graph object
graph.Shapes.Add(line);
// Save PDF file
document.Save(dataDir + "AddLineObject_out.pdf");
}
}
How to add Dotted Dashed Line to your PDF document
private static void DashLengthInBlackAndDashLengthInWhite()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create Document instance using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of PDF file
var page = document.Pages.Add();
// Create Drawing object with certain dimensions
var canvas = new Aspose.Pdf.Drawing.Graph(100, 400);
// Add drawing object to paragraphs collection of page instance
page.Paragraphs.Add(canvas);
// Create Line object
var line = new Aspose.Pdf.Drawing.Line(new float[] { 100, 100, 200, 100 });
// Set color for Line object
line.GraphInfo.Color = Aspose.Pdf.Color.Red;
// Specify dash array for line object
line.GraphInfo.DashArray = new int[] { 3, 2 }; // Dash and gap lengths in points
// Set the dash phase for Line instance
line.GraphInfo.DashPhase = 1;
// Add line to shapes collection of drawing object
canvas.Shapes.Add(line);
// Save PDF file
document.Save(dataDir + "DashLengthInBlackAndDashLengthInWhite_out.pdf");
}
}
Let’s check the result:
Draw Line Across the Page
We can also use line object to draw a cross starting from Left-Bottom to Right-Upper corner and Left-Top corner to Bottom-Right corner.
Please take a look over following code snippet to accomplish this requirement.
private static void ExampleLineAcrossPage()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create Document instance using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of PDF file
var page = document.Pages.Add();
// Set page margin on all sides as 0
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
// Create Graph object with Width and Height equal to page dimensions
var graph = new Aspose.Pdf.Drawing.Graph(
(float)page.PageInfo.Width,
(float)page.PageInfo.Height);
// Create first line object starting from Lower-Left to Top-Right corner of page
var line1 = new Aspose.Pdf.Drawing.Line(new float[]
{
(float)page.Rect.LLX, 0,
(float)page.PageInfo.Width,
(float)page.Rect.URY
});
// Add line to shapes collection of Graph object
graph.Shapes.Add(line1);
// Create second line object starting from Top-Left corner to Bottom-Right corner of page
var line2 = new Aspose.Pdf.Drawing.Line(new float[]
{
0, (float)page.Rect.URY,
(float)page.PageInfo.Width, (float)page.Rect.LLX
});
// Add line to shapes collection of Graph object
graph.Shapes.Add(line2);
// Add Graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
document.Save(dataDir + "ExampleLineAcrossPage_out.pdf");
}
}