Add Line Object to PDF file
Add Line object
Aspose.PDF for Python via .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 Document instance.
- Create a Graph Object
- Add Graph object to paragraphs collection of page.
- Create and Configure the Line
- Add the Line to the Graph
- Save our PDF file.
import aspose.pdf as ap
import aspose.pdf.drawing as drawing
import datetime
# Create Document instance
document = ap.Document()
# Add page to pages collection of PDF file
page = document.pages.add()
# Create Graph instance
graph = drawing.Graph(100, 400)
# Add graph object to paragraphs collection of page instance
page.paragraphs.add(graph)
# Create Rectangle instance
line = drawing.Line([100, 100, 200, 100])
# Specify fill color for Graph object
line.graph_info.dash_array = [0, 1, 0]
line.graph_info.dash_phase = 1
# Add rectangle object to shape collection of Graph object
graph.shapes.append(line)
# Save PDF file
document.save(path_outfile)
How to add Dotted Dashed Line to your PDF document
import aspose.pdf as ap
import aspose.pdf.drawing as drawing
import datetime
# Create Document instance
document = ap.Document()
# Add page to pages collection of PDF file
page = document.pages.add()
# Create Graph instance
graph = drawing.Graph(100, 400)
# Add graph object to paragraphs collection of page instance
page.paragraphs.add(graph)
# Create Rectangle instance
line = drawing.Line([100, 100, 200, 100])
# Set color for Line object
line.graph_info.color = ap.Color.red
# Specify fill color for Graph object
line.graph_info.dash_array = [0, 1, 0]
line.graph_info.dash_phase = 1
# Add rectangle object to shape collection of Graph object
graph.shapes.append(line)
# Save PDF file
document.save(path_outfile)
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.
import aspose.pdf as ap
import aspose.pdf.drawing as drawing
import datetime
# Create Document instance
document = ap.Document()
# Add page to pages collection of PDF file
page = document.pages.add()
# Set page margin on all sides as 0
page.page_info.margin.left = 0
page.page_info.margin.right = 0
page.page_info.margin.bottom = 0
page.page_info.margin.top = 0
# Create Graph object with Width and Height equal to page dimensions
graph = drawing.Graph(page.page_info.width, page.page_info.height)
# Create first line object starting from Lower-Left to Top-Right corner of page
line = drawing.Line([page.rect.llx, 0, page.page_info.width, page.rect.ury])
# Add line to shape collection of Graph object
graph.shapes.append(line)
# Draw line from Top-Left corner of page to Bottom-Right corner of page
line2 = drawing.Line([0, page.rect.ury, page.page_info.width, page.rect.llx])
# Add line to shape collection of Graph object
graph.shapes.append(line2)
# Add Graph object to paragraphs collection of page
page.paragraphs.add(graph)
# Save PDF file
document.save(path_outfile)