Skip to main content

Add Text, Images, Lines, and Rectangles to Existing PDFs


Add text, images, lines, and rectangles to your PDF documents using templates with the pdf endpoint.

You can add text, images, lines, and rectangles to an existing PDF document.

tip

Check out Getting Started and Task Roadmap if you are new to The DynamicPDF API.

Calling Endpoint Directly

{
"author": "CeteSoftware",
"creator": "DynamicPDF Cloud Api",
"templates": [
{
"id": "Temp1",
"elements": [
{
"type": "text",
"color": "Blue",
"text": "Hello PDF",
"fontSize": 42,
"placement": "topCenter",
"xOffset": -50,
"yOffset": 100
},
{
"type": "rectangle",
"width": 100,
"height": 300,
"borderWidth": 5,
"cornerRadius": 10,
"fillColor": "Green",
"borderColor": "Blue",
"borderStyle": "dots",
"placement": "topCenter",
"xOffset": 150,
"yOffset": 100
},
{
"type": "line",
"color": "Red",
"lineStyle": "solid",
"x2Offset": 500,
"y2Offset": 150,
"width": 4,
"placement": "topLeft",
"xOffset": 105,
"yOffset": 50
},
{
"type": "image",
"resourceName": "dynamicpdfLogo.png",
"placement": "topLeft",
"xOffset": 400,
"yOffset": 10
}
]
}
],
"inputs": [
{
"type": "pdf",
"resourceName": "DocumentA.pdf",
"templateId": "Temp1"
},
{
"type": "word",
"resourceName": "Doc1.docx",
"templateId": "Temp1"
},
{
"type": "image",
"resourceName": "testimage.png",
"templateId": "Temp1"
},
{
"type": "html",
"resourceName": "products.html",
"templateId": "Temp1"
}
]
}

Run In Postman

Calling Endpoint Using Client Library

static void CreatePdf(string apiKey, string basePath, string outputPath)
{
Template template = new Template("Temp1");
TextElement textElement = new TextElement("Hello PDF", ElementPlacement.TopCenter);
textElement.Color = RgbColor.Blue;
textElement.FontSize = 42;
textElement.XOffset = -50;
textElement.YOffset = 100;

template.Elements.Add(textElement);
RectangleElement recElement = new RectangleElement(ElementPlacement.TopCenter, 100, 300);
recElement.XOffset = 150;
recElement.YOffset = 100;
recElement.CornerRadius = 10;
recElement.BorderColor = RgbColor.Blue;
recElement.FillColor = RgbColor.Green;
recElement.BorderWidth = 5;
recElement.BorderStyle = LineStyle.Dots;

template.Elements.Add(recElement);
LineElement element = new LineElement(ElementPlacement.TopLeft, 500, 150);
element.Color = RgbColor.Red;
element.XOffset = 105;
element.YOffset = 50;
element.LineStyle = LineStyle.Solid;
element.Width = 4;

template.Elements.Add(element);
ImageResource imgResource = new ImageResource(basePath + "/dynamicpdfLogo.png");
ImageElement imageElement = new ImageElement(imgResource);
imageElement.XOffset = 400;
imageElement.YOffset = 10;
imageElement.Placement = ElementPlacement.TopLeft;

template.Elements.Add(imageElement);

Pdf pdf = new Pdf();
pdf.Author = "Test Author";
pdf.Creator = "Test Creator";

PdfInput input = pdf.AddPdf(new PdfResource(basePath + "DocumentA.pdf"));
input.Template = template;

WordResource wordResource = new WordResource(basePath + "Doc1.docx");
WordInput word = new WordInput(wordResource);
word.PageWidth = 300;
word.PageHeight = 200;
word.TopMargin = 10;
word.BottomMargin = 10;
word.RightMargin = 40;
word.LeftMargin = 40;
word.Template = template;
pdf.Inputs.Add(word);

ImageResource ir = new ImageResource(basePath + "testimage.png");
ImageInput imageInput = new ImageInput(ir);
imageInput.Template = template;

pdf.Inputs.Add(imageInput);

HtmlResource hr = new HtmlResource(basePath + "products.html");
HtmlInput hi = new HtmlInput(hr);
hi.Template = template;
pdf.Inputs.Add(hi);

pdf.ApiKey = apiKey;
PdfResponse response = pdf.Process();

if (response.ErrorJson != null)
{
Console.WriteLine(Utility.PrettyPrintUtil.JsonPrettify(response.ErrorJson));
}
else
{
File.WriteAllBytes(outputPath + "/combined-pdf-output.pdf", response.Content);
}
}
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.

   Follow us on social media for latest news!