Skip to main content

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.

tip

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
}
]
}
]
}

Run In Postman

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.

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);
}
}
Source: SolutionImagesTextRecs.cs
info

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.