Adding text and images to PDF
AOP provides several options to add texts and images in the PDF. Here are the examples on how you can perform the operations.
Adding Texts to PDF
AOP supports overlaying text on PDFs. To add text to PDFs, you can define per-page text or text on every page by placing text inside the attribute all
, with flexible options for styling and positioning.
To utilize this feature, insert your text and desired styling properties into the aop_pdf_texts
.
The supported styling properties include:
- Position, defined by X and Y coordinates
- Rotation, measured in degrees
- Font style, such as typeface, bold, and italic
- Font color
- Font size
From AOP 24.2.2 onwards, it is possible to specify x and y coordinates as a percentage of the page width and height. For that, you can provide the x and y coordinates as a string with a percentage sign at the end. e.g.
select
'First text to be shown in first page' as "text",
'50%' as "x",
'50%' as "y",
60 as "rotation",
'false' as "bold",
'false' as "italic",
'Arial' as "font",
'#FF00FF' as "font_color"
from dual
Example
The following example demonstrates how to insert per-page text with text styling and positioning.
Data Source
This SQL query generates a structured dataset for overlaying styled text on specific pages of a PDF.
It defines the text content, position, rotation, font style, font color, and formatting for each page (P1 and P2).
AOP uses this data to overlay the text onto the PDF.
- SQL
- PL/SQL returning SQL
- PL/SQL returning JSON
- JSON
SELECT 'file1' AS "filename",
CURSOR (
SELECT CURSOR (
SELECT CURSOR (
SELECT 'First text to be shown in first page' AS "text",
50 AS "x",
600 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Arial' AS "font",
'#FF00FF' AS "font_color"
FROM dual
UNION ALL
SELECT
'Second text to be shown in first page' AS "text",
150 AS "x",
50 AS "y",
60 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Times new roman' AS "font",
'#00FFFF' AS "font_color",
20 AS "font_size"
FROM
dual
) AS "P1",
CURSOR (
SELECT 'First text to be shown in second page' AS "text",
50 AS "x",
600 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Arial' AS "font",
'#FF00FF' AS "font_color"
FROM dual
) AS "P2",
CURSOR (
SELECT 'Page {currentpage}/{totalpage}' AS "text",
10 AS "x",
10 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Arial' AS "font",
'#000000' AS "font_color",
'15' AS "font_size"
FROM dual
) AS "all"
FROM dual
) AS "aop_pdf_texts"
FROM dual
) AS "data"
FROM dual;
declare
l_return clob;
begin
l_return := q'[
SELECT 'file1' AS "filename",
CURSOR (
SELECT CURSOR (
SELECT CURSOR (
SELECT 'First text to be shown in first page' AS "text",
50 AS "x",
600 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Arial' AS "font",
'#FF00FF' AS "font_color"
FROM dual
UNION ALL
SELECT 'second text to be shown in first page' AS "text",
50 AS "x",
400 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Times New Roman' AS "font",
'#00FFFF' AS "font_color"
FROM dual
) AS "P1",
CURSOR (
SELECT 'First text to be shown in second page' AS "text",
50 AS "x",
600 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Arial' AS "font",
'#FF00FF' AS "font_color"
FROM dual
) AS "P2",
CURSOR (
SELECT 'Page {currentpage}/{totalpage}' AS "text",
10 AS "x",
10 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Arial' AS "font",
'#000000' AS "font_color",
'15' AS "font_size"
FROM dual
) AS "all"
FROM dual
) AS "aop_pdf_texts"
FROM dual
) AS "data"
FROM dual;
]';
return l_return;
end;
declare
l_cursor sys_refcursor;
l_return clob; -- can also be varchar2, make sure it corresponds to the structure in the JSON
begin
apex_json.initialize_clob_output(dbms_lob.call, true, 2);
open l_cursor for
SELECT 'file1' AS "filename",
CURSOR (
SELECT CURSOR (
SELECT CURSOR (
SELECT 'First text to be shown in first page' AS "text",
50 AS "x",
600 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Arial' AS "font",
'#FF00FF' AS "font_color"
FROM dual
UNION ALL
SELECT 'second text to be shown in first page' AS "text",
50 AS "x",
400 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Times New Roman' AS "font",
'#00FFFF' AS "font_color"
FROM dual
) AS "P1",
CURSOR (
SELECT 'First text to be shown in second page' AS "text",
50 AS "x",
600 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Arial' AS "font",
'#FF00FF' AS "font_color"
FROM dual
) AS "P2",
CURSOR (
SELECT 'Page {currentpage}/{totalpage}' AS "text",
10 AS "x",
10 AS "y",
0 AS "rotation",
'false' AS "bold",
'false' AS "italic",
'Arial' AS "font",
'#000000' AS "font_color",
'15' AS "font_size"
FROM dual
) AS "all"
FROM dual
) AS "aop_pdf_texts"
FROM dual
) AS "data"
FROM dual;
apex_json.write(l_cursor);
l_return := apex_json.get_clob_output;
return l_return;
end;
[
{
"filename": "file1",
"data": [
{
"aop_pdf_texts": [
{
"P1": [
{
"text": "First text to be shown in first page",
"x": 50,
"y": 600,
"rotation": 0,
"bold": false,
"italic": false,
"font": "Arial",
"font_color": "#FF00FF"
},
{
"text": "second text to be shown in first page",
"x": 50,
"y": 400,
"rotation": 0,
"bold": false,
"italic": false,
"font": "Times New Roman",
"font_color": "#00FFFF"
}
],
"P2": [
{
"text": "First text to be shown in second page",
"x": 50,
"y": 600,
"rotation": 0,
"bold": false,
"italic": false,
"font": "Arial",
"font_color": "#FF00FF"
}
],
"all": [
{
"text": "Page {currentpage}/{totalpage}",
"x": 10,
"y": 10,
"rotation": 0,
"bold": false,
"italic": false,
"font": "Arial",
"font_color": "#000000",
"font_size": 15
}
]
}
]
}
]
}
]
The P1 and P2 attributes in the code define the text for pages 1 and 2, respectively.
The all
attribute adds text that appears on all pages of the PDF.
For example, the text Page {currentpage}/{totalpage}
is added to all
pages as it is specified within the all
attribute.
Template
This example uses a general DOCX template.
Output
When you provide a template and define your formatting and positioning specifications in the data source, AOP generates a PDF based on those settings.
For example, as shown in the image, the first page includes two text blocks, and the second page displays one, all styled according to the defined specifications on data source.
Adding Images to PDF
AOP supports overlaying images on a PDF. To implement this feature, place the image and its parameters inside the aop_pdf_images
.
You can specify an image for each individual page or for all pages by including the image and its parameters within the all
attribute.
The styling options available for images include:
- Position, specified by X and Y coordinates,
- Rotation, measured in degrees,
- Width of the image.
You can also use semi transparent PNG for wartermarking purposes.
Example
This example demonstrates how to overlay images onto a PDF.
Data Source
The data source includes the Apex Office Print logo along with its positioning specifications and width to overlay it onto the PDF.
- SQL
- PL/SQL returning SQL
- PL/SQL returning JSON
- JSON
SELECT
'file1' AS "filename",
CURSOR (
SELECT
CURSOR (
SELECT
CURSOR (
SELECT
'https://united-codes.com/uc_logos/aop/Branded/logo-apex-office@2x.png' AS "image",
50 AS "x",
400 AS "y",
500 AS "image_width",
- 75 AS "rotation"
FROM
dual
) AS "all"
FROM
dual
) AS "aop_pdf_images"
FROM
dual
) AS "data"
FROM
dual;
DECLARE
l_return CLOB;
BEGIN
l_return := q'[
SELECT
'file1' AS "filename",
CURSOR (
SELECT
CURSOR (
SELECT
CURSOR (
SELECT
'https://united-codes.com/uc_logos/aop/Branded/logo-apex-office@2x.png' AS "image",
50 AS "x",
400 AS "y",
500 AS "image_width",
- 75 AS "rotation"
FROM
dual
) AS "all"
FROM
dual
) AS "aop_pdf_images"
FROM
dual
) AS "data"
FROM
dual;
]';
RETURN l_return;
END;
DECLARE
l_cursor SYS_REFCURSOR;
l_return CLOB; -- can also be varchar2, make sure it corresponds to the structure in the JSON
BEGIN
apex_json.initialize_clob_output(dbms_lob.call, TRUE, 2);
OPEN l_cursor FOR SELECT
'file1' AS "filename",
CURSOR (
SELECT
CURSOR (
SELECT
CURSOR (
SELECT
'https://united-codes.com/uc_logos/aop/Branded/logo-apex-office@2x.png' AS "image",
50 AS "x",
400 AS "y",
500 AS "image_width"
,
- 75 AS "rotation"
FROM
dual
) AS "all"
FROM
dual
) AS "aop_pdf_images"
FROM
dual
) AS "data"
FROM
dual;
apex_json.write(l_cursor);
l_return := apex_json.get_clob_output;
RETURN l_return;
END;
[
{
"filename": "file1",
"data": [
{
"aop_pdf_images": [
{
"all": [
{
"image": "https://united-codes.com/uc_logos/aop/Branded/logo-apex-office@2x.png",
"x": 50,
"y": 400,
"image_width": 500,
"rotation": -75
}
]
}
]
}
]
}
]
Template
This example uses the W-8BEN form, a Certificate of Foreign Status for Beneficial Owners, as a template. You can use any template that generates a PDF output, and images can be added to the PDF as needed.
Output
When you provide the image orientation details in the data source and a template, the output will be as follows.