article

How to Add Rectangle or Line Object to An Existing PDF File using .NET

Email
Submitted on: 7/20/2016 7:50:24 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 3569
 
     This technical tip shows how .NET developers can add line object to an Existing PDF File inside their .NET applications. 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. The legacy Aspose.Pdf.Generator provides the feature to set DashLengthInBlack and DashLengthInWhite properties where dash pattern for line object can be defined. Similar features can be accomplished while using DOM approach.

 
				This technical tip shows how .NET developers can add line object to an Existing PDF File inside their .NET applications. 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. The legacy Aspose.Pdf.Generator provides the feature to set DashLengthInBlack and DashLengthInWhite properties where dash pattern for line object can be defined. Similar features can be accomplished while using DOM approach.
//The following code snippet shows how to add a Rectangle object that is filled with color.
[C#]
 
// 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);
// Save PDF file
doc.Save("c:/pdftest/LineAdded.pdf");
[VB.NET]
 
' Create Document instance
Dim doc As Document = New Document()
' Add page to pages collection of PDF file
Dim page As Page = doc.Pages.Add()
' Create Graph instance
Dim graph As Aspose.Pdf.Drawing.Graph = New Aspose.Pdf.Drawing.Graph(100, 400)
' Add graph object to paragraphs collection of page instance
page.Paragraphs.Add(graph)
' Create Rectangle instance
Dim line As Aspose.Pdf.Drawing.Line = New Aspose.Pdf.Drawing.Line(New Single() {100, 100, 200, 100})
' Specify fill color for Graph object
line.GraphInfo.DashArray = New Integer() {0, 1, 0}
line.GraphInfo.DashPhase = 1
' Add rectangle object to shapes collection of Graph object
graph.Shapes.Add(line)
' Save PDF file
doc.Save("c:/pdftest/LineAdded.pdf")
//DashLengthInBlack and DashLengthInWhite properties for Line object
[C#]
 
 // instantiate Document instance
Document doc = new Document();
// add page to pages collection of Document object
Page page = doc.Pages.Add();
// create Drawing object with certain dimensions
Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400);
// add drawing object to paragraphs collection of page instance
page.Paragraphs.Add(canvas);
// create Line object
Aspose.Pdf.Drawing.Line 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[] { 0, 1, 0 };
// 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 document
doc.Save("c:/DashLineInBlack.pdf");
[VB.NET]
 
 
' instantiate Document instance
Dim doc As Document = New Document()
' add page to pages collection of Document object
Dim page As Page = doc.Pages.Add()
' create Drawing object with certain dimensions
Dim canvas As Aspose.Pdf.Drawing.Graph = New Aspose.Pdf.Drawing.Graph(100, 400)
' add drawing object to paragraphs collection of page instance
page.Paragraphs.Add(canvas)
' create Line object
Dim line As Aspose.Pdf.Drawing.Line = New Aspose.Pdf.Drawing.Line(New Single() {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 Integer() {0, 1, 0}
' 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 document
doc.Save("c:/DashLineInBlack.pdf")
More about Aspose.Pdf for .NET


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.