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


/ Published in: C#
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. //The following code snippet shows how to add a Rectangle object that is filled with color.
  2.  
  3. //[C# Code]
  4.  
  5. // Create Document instance
  6. Document doc = new Document();
  7. // Add page to pages collection of PDF file
  8. Page page = doc.Pages.Add();
  9. // Create Graph instance
  10. Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(100, 400);
  11. // Add graph object to paragraphs collection of page instance
  12. page.Paragraphs.Add(graph);
  13. // Create Rectangle instance
  14. Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[]{100, 100, 200, 100});
  15. // Specify fill color for Graph object
  16. line.GraphInfo.DashArray = new int[] { 0, 1, 0 };
  17. line.GraphInfo.DashPhase = 1;
  18. // Add rectangle object to shapes collection of Graph object
  19. graph.Shapes.Add(line);
  20. // Save PDF file
  21. doc.Save("c:/pdftest/LineAdded.pdf");
  22.  
  23. //[VB.NET Code]
  24.  
  25. ' Create Document instance
  26. Dim doc As Document = New Document()
  27. ' Add page to pages collection of PDF file
  28. Dim page As Page = doc.Pages.Add()
  29. ' Create Graph instance
  30. Dim graph As Aspose.Pdf.Drawing.Graph = New Aspose.Pdf.Drawing.Graph(100, 400)
  31. ' Add graph object to paragraphs collection of page instance
  32. page.Paragraphs.Add(graph)
  33. ' Create Rectangle instance
  34. Dim line As Aspose.Pdf.Drawing.Line = New Aspose.Pdf.Drawing.Line(New Single() {100, 100, 200, 100})
  35. ' Specify fill color for Graph object
  36. line.GraphInfo.DashArray = New Integer() {0, 1, 0}
  37. line.GraphInfo.DashPhase = 1
  38. ' Add rectangle object to shapes collection of Graph object
  39. graph.Shapes.Add(line)
  40. ' Save PDF file
  41. doc.Save("c:/pdftest/LineAdded.pdf")
  42.  
  43. //DashLengthInBlack and DashLengthInWhite properties for Line object
  44.  
  45. //[C# Code]
  46.  
  47. // instantiate Document instance
  48. Document doc = new Document();
  49. // add page to pages collection of Document object
  50. Page page = doc.Pages.Add();
  51. // create Drawing object with certain dimensions
  52. Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400);
  53. // add drawing object to paragraphs collection of page instance
  54. page.Paragraphs.Add(canvas);
  55. // create Line object
  56. Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { 100, 100, 200, 100 });
  57. // set color for Line object
  58. line.GraphInfo.Color = Aspose.Pdf.Color.Red;
  59. // specify dash array for line object
  60. line.GraphInfo.DashArray = new int[] { 0, 1, 0 };
  61. // set the dash phase for Line instance
  62. line.GraphInfo.DashPhase = 1;
  63. // add line to shapes collection of drawing object
  64. canvas.Shapes.Add(line);
  65. // save PDF document
  66. doc.Save("c:/DashLineInBlack.pdf");
  67.  
  68. //[VB.NET Code]
  69.  
  70. ' instantiate Document instance
  71. Dim doc As Document = New Document()
  72. ' add page to pages collection of Document object
  73. Dim page As Page = doc.Pages.Add()
  74. ' create Drawing object with certain dimensions
  75. Dim canvas As Aspose.Pdf.Drawing.Graph = New Aspose.Pdf.Drawing.Graph(100, 400)
  76. ' add drawing object to paragraphs collection of page instance
  77. page.Paragraphs.Add(canvas)
  78. ' create Line object
  79. Dim line As Aspose.Pdf.Drawing.Line = New Aspose.Pdf.Drawing.Line(New Single() {100, 100, 200, 100})
  80. ' set color for Line object
  81. line.GraphInfo.Color = Aspose.Pdf.Color.Red
  82. ' specify dash array for line object
  83. line.GraphInfo.DashArray = New Integer() {0, 1, 0}
  84. ' set the dash phase for Line instance
  85. line.GraphInfo.DashPhase = 1
  86. ' add line to shapes collection of drawing object
  87. canvas.Shapes.Add(line)
  88. ' save PDF document
  89. doc.Save("c:/DashLineInBlack.pdf")

URL: http://www.aspose.com/.net/pdf-component.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.