How to draw shapes to PDF reports


In PDF, there are several ways to draw shapes (like drawing lines one by one, and moving the current point around), but it can be difficult to understand. [inlinetweet prefix="null" tweeter="null" suffix="null"]In PL/PDF, our job is to make things easier for you, so we have some simple procedures to create shapes into your PDF document[/inlinetweet]. Our related procedures are in the „plpdf” package, and they are called „Circle”, „Ellipse”, „Line”, „Polygon”, „Rect”, RoundedRect”, and „Sector”.
We will talk about all of them, so let’s start with the first:

Circle

It has 9 parameters, and their names help you understand what they mean:

  • p_x: The X coordinate for the center of the circle.
  • p_y: The Y coordinate for the center of the circle.
  • p_r: The radius of the circle.
  • p_draw: Set it false if you don't want the frame for your shape (it means you will only get the filled area).
  • p_fill: Set it true if you want to fill the circle.
  • p_draw_color: The drawing color of the circle (there are defined colors in the plpdf_const package).
  • p_fill_color: The filling color of the circle (if you set the fill true).
  • p_linewidth: The line width of the circle.
  • p_fill_pattern: You can give a pattern to fill the circle with it, but we won’t talk about the patterns now, it will be discussed in a new post about pattern usage.

For example: You want to draw a Blue circle at (50,50) position to the page, and you want it to be filled with yellow color, and drawn with a thicker line. This is how your code will look like. And for a second case, let’s try it unfilled.

plpdf.Circle(
p_x => 50,
p_y => 50,
p_r => 20,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.Blue,
p_fill_color => plpdf_const.Yellow,
p_linewidth => 4,
p_fill_pattern => null
);
plpdf.Circle(
p_x => 50,
p_y => 50,
p_r => 20,
p_draw => true,
p_fill => false,
p_draw_color => plpdf_const.Blue,
p_fill_color => null,
p_linewidth => 4,
p_fill_pattern => null
);

Ellipse

They are somewhat similar to the circles, but they have another parameter for their second range. The procedures' parameters look like this:

  • p_x: The X coordinate for the center of the ellipse.
  • p_y: The Y coordinate for the center of the ellipse.
  • p_rx: The X radius of the ellipse.
  • p_ry: The Y radius of the ellipse.
  • p_draw: Set it false if you don't want the frame for your shape (it means you will only get the filled area).
  • p_fill: Set it true if you want to fill the ellipse.
  • p_draw_color: The drawing color of the ellipse (there are defined colors in the plpdf_const package).
  • p_fill_color: The filling color of the ellipse (if you set the fill true).
  • p_linewidth: The line width of the ellipse.
  • p_fill_pattern: You can give a pattern to fill the ellipse with it, but we won’t talk about the patterns now, it will be discussed in a new post about pattern usage.

And with this, you can easily achieve a portal-like thing into your PDF. Let’s see it’s code:
plpdf.Ellipse(
p_x => 50,
p_y => 50,
p_rx => 10,
p_ry => 30,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.Dark_orange,
p_fill_color => plpdf_const.Light_salmon,
p_linewidth => 2,
p_fill_pattern => null
);
plpdf.Ellipse(
p_x => 120,
p_y => 50,
p_rx => 10,
p_ry => 30,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.blue,
p_fill_color => plpdf_const.Light_blue,
p_linewidth => 2,
p_fill_pattern => null
);
plpdf.setNonStrokingColor(plpdf_const.Green);
plpdf.PrintoutText(112,50,'PL/');
plpdf.PrintoutText(48,50, 'PDF');

Line

We are moving on straight to the Line procedure. It’s parameters are:

  • p_x1: X coordinate for the starting point of the line.
  • p_y1: Y coordinate for the starting point of the line.
  • p_x2: X coordinate for the ending point of the line.
  • p_y2: Y coordinate for the ending point of the line.
  • p_color: Color of the line.
  • p_width: The width of the line.

With this simple procedure, you can create tons of lines in just a second. Let's try it out with this short code:
for i in 1..15 loop
plpdf.Line(
p_x1 => i*10,
p_y1 => 20,
p_x2 => i*10 - 8,
p_y2 => 50,
p_color => plpdf_const.Dark_cyan,
p_width => 1
);
end loop;

Polygon

After you are clear with the line, it is time to talk about a more complex structure with lines, and it is called Polygon. For the polygon, you will have to create a collection of points (X and Y coordinates) which will define the points which will be connected. In this case, you will have to declare a „plpdf_type.t_points” and a „plpdf_type.t_point” variable, and we will give coordinates to the points one by one (you can do this with loop, or any way you like it).
declare
l_blob blob;
l_points plpdf_type.t_points;
l_point plpdf_type.t_point;
And defining the points:
l_point.X := 50;
l_point.Y := 50;
l_points(1) := l_point;
l_point.X := 60;
l_point.Y := 60;
l_points(2) := l_point;
l_point.X := 40;
l_point.Y := 60;
l_points(3) := l_point;
l_point.X := 40;
l_point.Y := 35;
l_points(4) := l_point;
l_point.X := 70;
l_point.Y := 40;
l_points(5) := l_point;
l_point.X := 70;
l_point.Y := 75;
l_points(6) := l_point;
l_point.X := 30;
l_point.Y := 70;
l_points(7) := l_point;
Now, with these points, you can start to draw the polygon. It has 5 main parameters, which are:

  • p_points: Collection of the points which will be connected.
  • p_draw: Set it false if you don't want the frame for your shape (it means you will only get the filled area).
  • p_fill: Set it true if you want to fill your polygon.
  • p_draw_color: Drawing color of the polygons' lines.
  • p_fill_color: Filling color of the polygon.
  • p_linewidth: Width of the lines in your polygon.
  • p_fill_pattern: We will talk about this later.

For example, after you created the points with the previous snippet:
plpdf.Polygon(
p_points => l_points,
p_draw => true,
p_fill => false,
p_draw_color => plpdf_const.Green,
p_fill_color => null,
p_linewidth => 1,
p_fill_pattern => null
);

Rectangle

The next shape we will discuss is the Rectangle. It can be drawn with the procedure called Rect. It’s parameters are :

  • p_x: X coordinate for the top left corner of the rectangle.
  • p_y: Y coordinate for the top left corner of the rectangle.
  • p_w: Width of the rectangle.
  • p_h: Height of the rectangle.
  • p_draw: Set it false if you don't want the frame for your shape (it means you will only get the filled area).
  • p_fill: Set true if you want to fill the rectangle.
  • p_draw_color: Drawing color of the rectangle.
  • p_fill_color: Filling color of the rectangle.
  • p_linewidth: Line width of the rectangle.
  • p_fill_pattern: We will talk about this in another post.

With this procedure, you can easily draw rectangles into your PDF file:
plpdf.Rect(
p_x => 30,
p_y => 40,
p_w => 60,
p_h => 20,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.Medium_spring_green,
p_fill_color => plpdf_const.Medium_aquamarine,
p_linewidth => 1,
p_fill_pattern => null
);

Rounded Rectangle

Our next but not last shape drawing procedure is the RoundedRect. It has 1 plus parameter than the Rect, and it is:

  • p_r: The curve range of the rectangle’s corners.

For example, let’s try our previous code in RoundedRect, and give it a p_r parameter.
plpdf.RoundedRect(
p_x => 30,
p_y => 40,
p_w => 60,
p_h => 20,
p_r => 5,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.Medium_spring_green,
p_fill_color => plpdf_const.Medium_aquamarine,
p_linewidth => 1,
p_fill_pattern => null
);

Sector

And we have arrived to our last shape drawing procedure, and it is the Sector. Let’s see its parameters:

  • p_xc: X coordinate for the center of the circle which will have one of its sector drawn.
  • p_yc: Y coordinate for the center of the circle which will have one of its sector drawn.
  • p_r: Range of the circle which will have one of its sector drawn.
  • p_a: The starting angle of the sector.
  • p_b: The ending angle of the sector.
  • p_draw: Set it false if you don't want the frame for your shape (it means you will only get the filled area).
  • p_fill: Set it true if you want your sector to be filled.
  • p_draw_color: Drawing color of the sector.
  • p_fill_color: Filling color of the sector.
  • p_linewidth: Width of the sector’s line.
  • p_fill_pattern: Filling pattern, it will be discussed later.
  • p_cw: Sets the drawing direction:
  • True: clockwise
  • False: anti-clockwise
  • p_o: Origin of angles.

With this sector drawing procedure you can easily create the world’s most accurate pie chart into your PDF.
plpdf.Sector(
p_xc => 60,
p_yc => 60,
p_r => 50,
p_a => -135,
p_b => 135,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.blue,
p_fill_color => plpdf_const.blue,
p_linewidth => 1,
p_fill_pattern => null,
p_cw => true,
p_o => 90
);
plpdf.Sector(
p_xc => 60,
p_yc => 60,
p_r => 50,
p_a => 160,
p_b => 225,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.Yellow,
p_fill_color => plpdf_const.Yellow,
p_linewidth => 1,
p_fill_pattern => null,
p_cw => true,
p_o => 90
);
plpdf.Sector(
p_xc => 60,
p_yc => 60,
p_r => 50,
p_a => 135,
p_b => 160,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.Brown,
p_fill_color => plpdf_const.Brown,
p_linewidth => 1,
p_fill_pattern => null,
p_cw => true,
p_o => 90
);
plpdf.Circle(
p_x => 130,
p_y => 35,
p_r => 5,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.Blue,
p_fill_color => plpdf_const.Blue,
p_linewidth => 1,
p_fill_pattern => null
);
plpdf.PrintoutText(
p_x => 140,
p_y => 37,
p_txt => 'Sky'
);
plpdf.Circle(
p_x => 130,
p_y => 50,
p_r => 5,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.Yellow,
p_fill_color => plpdf_const.Yellow,
p_linewidth => 1,
p_fill_pattern => null
);
plpdf.PrintoutText(
p_x => 140,
p_y => 52,
p_txt => 'Sunny side of pyramid'
);
plpdf.Circle(
p_x => 130,
p_y => 65,
p_r => 5,
p_draw => true,
p_fill => true,
p_draw_color => plpdf_const.Brown,
p_fill_color => plpdf_const.Brown,
p_linewidth => 1,
p_fill_pattern => null
);
plpdf.PrintoutText(
p_x => 140,
p_y => 67,
p_txt => 'Shady side of pyramid'
);