Skip to main content

Add Barcodes to PDFs


Add barcodes to a new PDF document or add to an existing PDF using the pdf endpoint.

The DynamicPDF API supports linear, two-dimensional, and postal barcodes. You can add barcodes to a new PDF and also an existing PDF. The following examples illustrate both scenarios.

DynamicPDF API supports the following barcodes: code128Barcode, code39Barcode, code25Barcode, code93Barcode, code11Barcode, qrCode, gs1DataBarBarcode, stackedGs1DataBarBarcode, iata25Barcode, msiBarcode, aztecBarcode, dataMatrixBarcode, and pdf417Barcode.

tip

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

info

Refer to the documentation on barcodes for more information (Instructions Overview - barcodes).

Creating New PDF

In this first example, we illustrate creating a single-page PDF with a Code11Barcode.

tip

Using the DynamicPDF Designer to graphically layout a PDF with a barcode is easier and adds more design flexibility.

Calling Endpoint Directly

Creating a new PDF with a barcode requires the following steps.

  1. Create a new instructions JSON document.
  2. Create a new inputs array and add a page input type.
  3. Add an elements array and add an element of the desired barcode type.

The following JSON instructions document illustrates the instructions for creating a simple PDF with a Code11Barcode.

{
"inputs": [
{
"type": "page",
"pageWidth": 1008,
"pageHeight": 612,
"elements": [
{
"type": "code11Barcode",
"height": 200,
"color": "Red",
"value": "12345678",
"placement": "topCenter",
"xOffset": 50,
"yOffset": 50
}
]
}
]
}

Run In Postman

curl https://api.dpdf.io/v1.0/pdf
--header 'Authorization: Bearer DP--api-key--'
--form 'Instructions=@C:/temp/solutions/pdf-barcode/instruction2.json'

Calling Endpoint Using Client Library

Use one of our client libraries rather then call the pdf endpoint directly to create a PDF with a Code11Barcode.

public static void Run(string apiKey, string outputPath)
{
Pdf pdf = new Pdf();
pdf.ApiKey = apiKey;
PageInput pageInput = pdf.AddPage(1008, 612);
pdf.Inputs.Add(pageInput);

Code11BarcodeElement code11BarcodeElement = new Code11BarcodeElement("12345678", ElementPlacement.TopCenter, 200, 50, 50);
code11BarcodeElement.Color = RgbColor.Red;
pageInput.Elements.Add(code11BarcodeElement);

PdfResponse pdfResponse = pdf.Process();

if (pdfResponse.IsSuccessful)
{
File.WriteAllBytes(outputPath + "/create-barcode-output.pdf", pdfResponse.Content);
}
else
{
Console.WriteLine(pdfResponse.ErrorJson);
}
}
Source: PdfBarcode.cs

Adding to Existing PDF

You can add a barcode to an existing PDF, or the PDF created when converting an image, Word document, or HTML to a PDF.

Calling Endpoint Directly

Adding a barcode to an existing PDF requires the following steps.

  1. Create a new instructions JSON document.
  2. Create a new templates array and an elements array.
  3. Add the barcode element to the array.
  4. Add a new inputs array and add a pdf input type.
  5. Refer to the created template by its templateId.

The following JSON instructions document illustrates the instructions for adding an AztecBarcode to an existing PDF.

{
"templates": [
{
"id": "Temp1",
"elements": [
{
"type": "aztecBarcode",
"valueType": "string",
"value": "Hello World",
"placement": "topCenter",
"xOffset": 0.0,
"yOffset": 500.0
}
]
}
],
"author": "CeteSoftware",
"creator": "DynamicPDF Cloud Api",
"inputs": [
{
"type": "pdf",
"templateId": "Temp1",
"resourceName": "DocumentA.pdf"
}
]
}

Run In Postman

curl --location 'https://api.dpdf.io/v1.0/pdf'
--header 'Authorization: Bearer DP--api-key--'
--form 'Instructions=@C:/temp/solutions/pdf-barcode/instructions.json'
--form 'Resource=@C:/temp/solutions/pdf-barcode/DocumentA.pdf'

Calling Endpoint Using Client Library

You can also use one of the API client libraries to add a barcode to a PDF rather than calling the pdf endpoint directly.

public static Pdf BarcodeExample(String basePath) {
Pdf pdf = new Pdf();
PdfResource resource = new PdfResource(basePath + "/DocumentA.pdf");
PdfInput input = new PdfInput(resource);
pdf.Inputs.Add(input);

Template template = new Template("Temp1");
AztecBarcodeElement element = new AztecBarcodeElement("Hello Barcode", ElementPlacement.TopCenter, 0, 500);
template.Elements.Add(element);
input.Template = template;
return pdf;
}
Source: InstructionsExample.cs
tip

Refer to the documentation on Designer for examples of using barcodes with Designer.

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!