Location>code7788 >text

Python Draw Lines, Rectangles, Ellipses in PDFs

Popularity:548 ℃/2024-11-18 16:45:48

Drawing shapes in PDF can enhance the visual effect of the document. By adding different types of shapes, such as solid lines, dashed lines, rectangles, circles, etc., you can make the document more vivid and interesting to enhance the reader's interest in reading. This is especially useful for creating reports, presentations or teaching materials. In this article, we will show you how toUse Python to draw different shapes in the PDF

  • Python draw solid lines, dashed lines in PDF
  • Python Draw Rectangles in PDF
  • Python Drawing Ovals in PDF

 

Required Python libraries for Python. It can be installed directly using the pip command below:

pip install

 

Python draw solid lines, dashed lines in PDF

for Python library provides() method is used to draw lines at a specified location on the PDF page. By setting the style of the brush PdfPen, you can draw solid or dashed lines.

Python code:

from  import *
from  import *
 
# Create PDF documents
pdf = PdfDocument()
 
# Add Page
page = ()
 
# Save the current drawing state
state = ()
 
# Specify the X and Y coordinates of the starting point of the line.
x = 100.0  
y = 50.0   
 
# Developing line lengths
width = 300.0  
 
# Creating brushes with specified colors and thicknesses
pen = PdfPen(PdfRGBColor(Color.get_Blue()), 2.0)  
 
# Drawing solid lines on a page with a brush
(pen, x, y, x + width, y)
 
# Set the brush style to dashed
 =   
 
# Set dashed line style to [1, 4, 1]
 = [1, 4, 1]  
 
# Specify the y-coordinate of the start point of the dashed line
y = 80.0  
 
# Drawing dotted lines on a page with a paintbrush
(pen, x, y, x + width, y)
 
# Restore a previously saved drawing state
(state)
 
# Save PDF document
("Drawing Lines.pdf")
()
()

 

Python in PDFDrawing Rectangles

() method can be used to draw a rectangle at a specified location on the PDF page. By passing different parameters to the method, you can specify the size of the rectangle, the fill color, and so on.

Python code:

from  import *
from  import *
 
# Create PDF documents
pdf = PdfDocument()
 
# Add Page
page = ()
 
# Save the current drawing state
state = ()
 
# Creating brushes with specified colors and thicknesses
pen = PdfPen(PdfRGBColor(Color.get_Blue()), 1.5)
 
# Draw a rectangle on the page with the paintbrush
(pen, RectangleF(PointF(20.0, 60.0), SizeF(150.0, 90.0)))
 
# Create a linear gradient brush
linearGradientBrush = PdfLinearGradientBrush(PointF(220.0, 60.0), PointF(350.0, 180.0), PdfRGBColor(Color.get_Green()), PdfRGBColor(Color.get_Pink()))
 
# Drawing a filled rectangle with the Linear Gradient Brush
 (linearGradientBrush, RectangleF(PointF(220.0, 60.0), SizeF(150.0, 90.0)))
 
# Restore a previously saved drawing state
(state)
 
# Save PDF document
("Drawing Rectangles.pdf")
()
()

 

Python Drawing Ovals in PDF

Drawing an ellipse at a specified location on a PDF page can be done using the() Methods. Different styles of ellipses can be drawn by specifying different PDF brushes or painting brushes.

Python code:

from  import *
from  import *
 
# Create PDF documents
pdf = PdfDocument()
 
# Add Page
page = ()
 
# Save the current drawing state
state = ()
 
# Creating Brushes
pen = PdfPens.get_Violet()
 
# Draw an ellipse on the page with the paintbrush
(pen, 30.0, 60.0, 150.0, 100.0)
 
# Creating a Fill Brush Object
brush = PdfSolidBrush(PdfRGBColor(Color.get_Violet()))
 
# Drawing filled oval shapes
(brush, 220.0, 60.0, 150.0, 100.0)
 
# Restore a previously saved drawing state
(state)
 
# Save PDF document
("Drawing Ellipses.pdf")
()
()

 

 


 

The red watermark in the generated document can be removed by clicking the link below and requesting a free one-month license:

/misc/