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"
}
]
}
Calling Endpoint Using Client Library
- C# (.NET)
- Java
- Node.js
- PHP
- GO
- Python
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);
}
}
TBD
static void Run(String apiKey, String basePath)
{
Template template = new Template("Temp1");
TextElement textElement = new TextElement("Hello PDF", ElementPlacement.TOPCENTER);
textElement.setColor(RgbColor.getBlue());
textElement.setFontSize(42);
textElement.setXOffset(-50);
textElement.setYOffset(100);
template.getElements().add(textElement);
RectangleElement recElement = new RectangleElement(ElementPlacement.TOPCENTER, 100, 500);
recElement.setXOffset(-150);
recElement.setYOffset(100);
recElement.setCornerRadius(10);
recElement.setBorderWidth(5);
recElement.setBorderStyle(LineStyle.getDots());
recElement.setBorderColor(RgbColor.getBlue());
recElement.setFillColor(RgbColor.getGreen());
template.getElements().add(recElement);
LineElement element = new LineElement(ElementPlacement.TOPLEFT, 500, 150);
element.setColor(RgbColor.getRed());
element.setXOffset(105);
element.setYOffset(50);
element.setX2Offset(900);
element.setY2Offset(150);
element.setLineStyle(LineStyle.getSolid());
element.setWidth(4);
template.getElements().add(element);
ImageResource imgResource = new ImageResource(basePath + "dynamicpdfLogo.png");
ImageElement imageElement = new ImageElement(imgResource);
imageElement.setPlacement(ElementPlacement.TOPLEFT);
imageElement.setXOffset(400);
imageElement.setYOffset(10);
template.getElements().add(imageElement);
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
PdfInput input = pdf.addPdf(new PdfResource(basePath + "DocumentA.pdf"));
input.setTemplate(template);
pdf.getInputs().add(input);
WordResource wordResource = new WordResource(basePath + "Doc1.docx");
WordInput word = new WordInput(wordResource);
word.setTemplate(template);
pdf.getInputs().add(word);
ImageResource ir = new ImageResource(basePath + "testimage.png");
ImageInput imageInput = new ImageInput(ir);
imageInput.setTemplate(template);
pdf.getInputs().add(imageInput);
HtmlResource hr = new HtmlResource(basePath + "products.html");
HtmlInput hi = new HtmlInput(hr);
hi.setTemplate(template);
pdf.getInputs().add(hi);
pdf.setApiKey(apiKey);
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
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.