Add Text, Images, Lines, and Rectangles to PDFs
Add text, images, lines, and rectangles to a one page PDF (page input) using the pdf endpoint.
Add text, images, lines, and rectangles directly to a new PDF document using the page
input type combined with one or more elements. The examples here illustrate creating a simple cover page with text, a line, a rectangle, and an image, first by calling the pdf endpoint directly and then using the DynamicPDF API client libraries.
Check out Getting Started and Task Roadmap if you are new to The DynamicPDF API.
Calling Endpoint Directly
The following JSON instructions document illustrates creating a PDF cover-page containing text, a line, a rectangle, and an image.
{
"author": "CeteSoftware",
"creator": "DynamicPDF Cloud Api",
"inputs": [
{
"type": "page",
"pageWidth": 1008,
"pageHeight": 612,
"elements": [
{
"type": "text",
"color": "Blue",
"text": "Hello PDF",
"fontSize": 42,
"placement": "topCenter",
"xOffset": -50,
"yOffset": 100
},
{
"type": "line",
"color": "Red",
"lineStyle": "solid",
"x2Offset": 900,
"y2Offset": 150,
"width": 4,
"placement": "topLeft",
"xOffset": 305,
"yOffset": 150
},
{
"type": "rectangle",
"width": 100,
"height": 500,
"borderWidth": 5,
"cornerRadius": 10,
"fillColor": "Green",
"borderColor": "Blue",
"borderStyle": "dots",
"placement": "topCenter",
"xOffset": -250,
"yOffset": -10
},
{
"type": "image",
"resourceName": "dynamicpdfLogo.png",
"placement": "topLeft",
"xOffset": 835,
"yOffset": 75
}
]
}
]
}
curl --location 'https://api.dynamicpdf.com/v1.0/pdf'
--header 'Authorization: Bearer DP--api-key--'
--form 'Instructions=@C:/temp/solutions/images-text-recs/instructions.json'
--form 'Resource=@C:/temp/solutions/images-text-recs/dynamicpdfLogo.png'
Calling Endpoint Using Client Library
The DynamicPDF API's six client libraries can also programmatically create the same PDF that was created using the previous instructions JSON.
- C# (.NET)
- Java
- Node.js
- PHP
- GO
- Python
public static void Run(string apiKey, string basePath, string outputPath)
{
Pdf pdf = new Pdf();
pdf.ApiKey = apiKey;
PageInput pageInput = pdf.AddPage(1008, 612);
pdf.Inputs.Add(pageInput);
TextElement textElement = new TextElement("Hello PDF", ElementPlacement.TopCenter, 50, 100);
textElement.Color = RgbColor.Blue;
textElement.FontSize = 42;
pageInput.Elements.Add(textElement);
textElement.XOffset = -50;
textElement.YOffset = 100;
LineElement element = new LineElement(ElementPlacement.TopLeft, 200, 200);
element.Color = RgbColor.Red;
element.XOffset = 305;
element.YOffset = 150;
element.X2Offset = 900;
element.Y2Offset = 150;
element.LineStyle = LineStyle.Solid;
element.Width = 4;
pageInput.Elements.Add(element);
RectangleElement recElement = new RectangleElement(ElementPlacement.TopCenter, 100, 500);
recElement.XOffset = -250;
recElement.YOffset = -10;
recElement.CornerRadius = 10;
recElement.BorderWidth = 5;
recElement.BorderStyle = LineStyle.Dots;
recElement.BorderColor = RgbColor.Blue;
recElement.FillColor = RgbColor.Green;
pageInput.Elements.Add(recElement);
ImageResource imgResource = new ImageResource("c:/temp/solutions/images-text-recs/dynamicpdfLogo.png");
ImageElement imageElement = new ImageElement(imgResource);
imageElement.XOffset = 835;
imageElement.YOffset = 75;
pageInput.Elements.Add(imageElement);
PdfResponse pdfResponse = pdf.Process();
if (pdfResponse.IsSuccessful)
{
File.WriteAllBytes(outputPath + "/create-pdf-img-text-rec-output.pdf", pdfResponse.Content);
}
else
{
Console.WriteLine(pdfResponse.ErrorJson);
}
}
TBD
public static void Run(String apiKey, String basePath)
{
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
PageInput pageInput = pdf.addPage(1008, 612);
pdf.getInputs().add(pageInput);
TextElement textElement = new TextElement("Hello PDF", ElementPlacement.TOPCENTER, 50, 100);
textElement.setColor(RgbColor.getBlue());
textElement.setFontSize(42);
pageInput.getElements().add(textElement);
textElement.setXOffset(-50);
textElement.setYOffset(100);
LineElement element = new LineElement(ElementPlacement.TOPLEFT, 200, 200);
element.setColor(RgbColor.getRed());
element.setXOffset(305);
element.setYOffset(150);
element.setX2Offset(900);
element.setY2Offset(150);
element.setLineStyle(LineStyle.getSolid());
element.setWidth(4);
pageInput.getElements().add(element);
RectangleElement recElement = new RectangleElement(ElementPlacement.TOPCENTER, 100, 500);
recElement.setXOffset(-250);
recElement.setYOffset(-10);
recElement.setCornerRadius(10);
recElement.setBorderWidth(5);
recElement.setBorderStyle(LineStyle.getDots());
recElement.setBorderColor(RgbColor.getBlue());
recElement.setFillColor(RgbColor.getGreen());
pageInput.getElements().add(recElement);
ImageResource imgResource = new ImageResource(basePath + "dynamicpdfLogo.png");
ImageElement imageElement = new ImageElement(imgResource);
imageElement.setXOffset(835);
imageElement.setYOffset(75);
pageInput.getElements().add(imageElement);
PdfResponse response = pdf.process();
if(response.getIsSuccessful())
{
try {
FileUtils.writeByteArrayToFile(new File(DynamicPdfCloudApiExamples.OUTPUT_PATH + "/images-text-rec-pdf-output.pdf"),
response.getContent());
} catch (IOException e) {
e.printStackTrace();
}
} else
{
System.out.println(response.getErrorJson());
}
}
TBD
TBD
def solutionsImageTextRecs_example(apikey, full_path):
pdf=Pdf()
pdf.api_key=apikey
pdf.author = "John Doe"
pdf.title = "My PDF"
inputPage = pdf.add_page(1008, 612)
textElement = TextElement("Hello PDF", ElementPlacement.TopCenter, 50, 100)
textElement.color = RgbColor.blue();
textElement.font_size = 42;
textElement.x_offset = -50;
textElement.y_offset = 100;
inputPage.elements.append(textElement)
element = LineElement(ElementPlacement.TopLeft, 200, 200);
element._color = RgbColor.red();
element.x_offset = 305;
element.y_offset = 150;
element.x2_offset = 900;
element.y2_offset = 150;
element.line_style = LineStyle.solid();
element.width = 4;
inputPage.elements.append(element);
recElement = RectangleElement(ElementPlacement.TopCenter, 100, 500);
recElement.x_offset = -250;
recElement.y_offset = -10;
recElement.corner_radius = 10;
recElement.border_width = 5;
recElement.border_style = LineStyle.dots();
recElement.border_color = RgbColor.blue();
recElement.fill_color = RgbColor.green();
inputPage.elements.append(recElement);
imgResource = ImageResource(full_path + "dynamicpdfLogo.png");
imageElement = ImageElement(imgResource, ElementPlacement.TopCenter, 835, 75);
inputPage.elements.append(imageElement);
response = pdf.process()
if response.is_successful:
with open(output_path + "solution-images-text-rec-output.pdf", "wb") as output_file:
output_file.write(response.content)
else:
print(response.error_id)
The pdf
endpoint takes a JSON instructions document that provides instructions for creating, transforming, merging, and formatting inputs into a combined PDF. Refer to documentation on the instructions schema for information on how to use the pdf
endpoint.