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.dpdf.io/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);
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, 900, 150);
element.Color = RgbColor.Red;
element.XOffset = 305;
element.YOffset = 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(basePath + "/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-csharp-img-text-rec-output.pdf", pdfResponse.Content);
}
else
{
Console.WriteLine(pdfResponse.ErrorJson);
}
}}
static async Run() {
var pdf = new Pdf();
pdf.apiKey = Constants.ApiKey;
var pageInput = pdf.addPage(1008,612);
var textElement = new TextElement("Hello PDF", elementPlacement.topCenter, 50, 100);
textElement.color = RgbColor.blue;
textElement.xOffset = -50;
textElement.yOffset = 100
textElement.fontSize = 42;
pageInput.elements.push(textElement);
var lineElement = new LineElement(elementPlacement.topCenter,900,150);
lineElement.color = RgbColor.red;
lineElement.xOffset = 305;
lineElement.yOffset = 150
lineElement.lineStyle = LineStyle.solid;
lineElement.width = 4;
pageInput.elements.push(lineElement);
var recElement = new RectangleElement(elementPlacement.TopCenter, 100, 500);
recElement.xOffset = -250;
recElement.yOffset = -10;
recElement.corner_radius = 10;
recElement.border_width = 5;
recElement.corner_radius = 10;
recElement.border_style = LineStyle.dots;
recElement.border_color = RgbColor.blue;
recElement.fill_color = RgbColor.green;
pageInput.elements.push(recElement);
var imgResource = new ImageResource(Constants.BasePath + "templates/dynamicpdfLogo.png");
var imageElement = new ImageElement(imgResource);
imageElement.elementPlacement = elementPlacement.topLeft;
imageElement.xOffset = 835;
imageElement.yOffset = 75;
pageInput.elements.push(imageElement);
var res = await pdf.process();
if (res.isSuccessful) {
var outFile = Constants.OutputPath +
"solutions-img-text-rec-node-js-example-output.pdf";
var outStream = fs.createWriteStream(outFile);
outStream.write(res.content);
outStream.close();
}
}
}
public static void Run(String apiKey, String basePath)
{
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
PageInput pageInput = pdf.addPage(1008, 612);
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, 900, 150);
element.setColor(RgbColor.getRed());
element.setXOffset(305);
element.setYOffset(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());
}
}
public static function Run(string $apikey, string $path, string $outputPath)
{
$pdf = new Pdf();
$pdf->ApiKey = $apikey;
$pdf->Author = "John Doe";
$pdf->Title = "My Blank PDF Page";
$pageInput = $pdf->AddPage(1008, 612);
$textElement = new TextElement("Hello PDF", ElementPlacement::TopCenter);
$textElement->Color = RgbColor::Blue();
$textElement->FontSize = 42;
$textElement->XOffset = -50;
$textElement->YOffset = 100;
array_push($pageInput->Elements, $textElement);
$lineElement = new LineElement(900, 150, ElementPlacement::TopLeft);
$lineElement->Color = RgbColor::Red();
$lineElement->XOffset = 305;
$lineElement->YOffset = 150;
$lineElement->LineStyle = LineStyle::Dash();
$lineElement->Width = 4;
array_push($pageInput->Elements, $lineElement);
$recElement = new RectangleElement(100, 500, ElementPlacement::TopCenter);
$recElement->XOffset = -250;
$recElement->YOffset = -10;
$recElement->CornerRadius = 10;
$recElement->BorderWidth = 5;
$recElement->BorderStyle = LineStyle::Dots();
$recElement->BorderColor = RgbColor::Blue();
$recElement->FillColor = RgbColor::Green();
array_push($pageInput->Elements, $recElement);
$imgResource = new ImageResource($path . "dynamicpdfLogo.png", "dynamicpdfLogo.png");
$imageElement = new ImageElement($imgResource, ElementPlacement::TopCenter, 835, 75);
array_push($pageInput->Elements, $imageElement);
$pdfResponse = $pdf->Process();
if($pdfResponse->IsSuccessful)
{
file_put_contents($outputPath . "solution-image-text-rec-example-output.pdf",
$pdfResponse->Content);
} else {
echo($pdfResponse->ErrorJson);
}
}
func main() {
pdfExample := endpoint.NewPdf()
pdfExample.Endpoint.ApiKey = apiKey
pageInput := pdfExample.AddPageWithDimension(1008, 612)
textElement := element.NewText("Hello PDF", element.TopCenter, 50, 100)
textElement.SetColor(color.NewRgbColorDefault().Blue().Color)
textElement.SetFontSize(42)
lineElement := element.NewLine(element.TopLeft, 900, 150)
lineElement.XOffset = 305
lineElement.YOffset = 150
lineElement.SetColor(color.NewRgbColorDefault().Red().Color)
lineElement.SetWidth(4)
lineStyle := lineElement.LineStyle()
lineElement.SetLineStyle(*lineStyle.Solid())
rectangle := element.NewRectangle(element.TopCenter, 100, 500)
rectangle.SetCornerRadius(10)
rectangle.SetBorderWidth(5)
rectangle.SetBorderStyle(*lineStyle.Dots())
rectangle.XOffset = -250
rectangle.YOffset = -10
rectangle.SetBorderColor(color.NewRgbColorDefault().Blue().Color)
rectangle.SetFillColor(color.NewRgbColorDefault().Green().Color)
imageResource := resource.NewImageResourceWithResourcePath(basePath + "dynamicPdfLogo.png",
"dynamicPdfLogo.png")
imageElement := element.NewImagewithImageResource(imageResource, element.TopLeft, 835, 75)
pageInput.Elements = append(pageInput.Elements, imageElement)
pageInput.Elements = append(pageInput.Elements, rectangle)
pageInput.Elements = append(pageInput.Elements, textElement)
pageInput.Elements = append(pageInput.Elements, lineElement)
resp := pdfExample.Process()
res := <-resp
if res.IsSuccessful() == true {
os.Remove(outputPath)
os.WriteFile(outputPath,
res.Content().Bytes(), os.ModeType)
} else {
fmt.Print(res.ErrorJson())
}
}
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, 950, 150);
element._color = RgbColor.red();
element.x_offset = 305;
element.y_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.