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.
Check out Getting Started and Task Roadmap if you are new to The DynamicPDF API.
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.
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.
- Create a new instructions JSON document.
- Create a new
inputs
array and add apage
input type. - 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
}
]
}
]
}
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.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
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);
}
}
static async Run() {
var pdf = new Pdf();
pdf.apiKey = Constants.ApiKey;
var pageInput = pdf.addPage(1008, 612);
var code11BarcodeElement = new Code11BarcodeElement("12345678", elementPlacement.topCenter, 200, 50, 50);
code11BarcodeElement.color = RgbColor.red;
pageInput.elements.push(code11BarcodeElement);
var res = await pdf.process();
if (res.isSuccessful) {
var outFile = Constants.OutputPath + "barcode-create-pdf-output.pdf";
var outStream = fs.createWriteStream(outFile);
outStream.write(res.content);
outStream.close();
}
}
public static void Run(String apiKey)
{
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
PageInput pageInput = pdf.addPage(1008, 612);
pdf.getInputs().add(pageInput);
Code11BarcodeElement code11BarcodeElement = new Code11BarcodeElement("12345678", ElementPlacement.TOPCENTER, 200, 50, 50);
code11BarcodeElement.setColor(RgbColor.getRed());
pageInput.getElements().add(code11BarcodeElement);
PdfResponse pdfResponse = pdf.process();
if(pdfResponse.getIsSuccessful())
{
try {
FileUtils.writeByteArrayToFile(new File(DynamicPdfCloudApiExamples.OUTPUT_PATH + "/barcode-createpdf-output.pdf"),
pdfResponse.getContent());
} catch (IOException e) {
e.printStackTrace();
}
} else
{
System.out.println(pdfResponse.getErrorJson());
}
}
public static function Run(string $apikey, string $path)
{
$pdf = new Pdf();
$pdf->ApiKey = $apikey;
$pdf->Author = "John Doe";
$pdf->Title = "My Blank PDF Page";
$pageInput = $pdf->AddPage(1008, 612);
$code11BarcodeElement = new Code11BarcodeElement("12345678", ElementPlacement::TopCenter, 200, 50, 50);
$code11BarcodeElement->Color = RgbColor::Red();
array_push($pageInput->Elements, $code11BarcodeElement);
$pdfResponse = $pdf->Process();
if($pdfResponse->IsSuccessful)
{
file_put_contents($path . "barcode-pdf-output.pdf", $pdfResponse->Content);
} else {
echo($pdfResponse->ErrorJson);
}
}
func barcodeNew() {
barcodePdf := endpoint.NewPdf()
barcodePdf.Endpoint.ApiKey = apiKey
pageInput := input.NewPage()
pageInput.SetPageHeight(621)
pageInput.SetPageWidth(1008)
barcode := element.NewCode11Barcode("12345678910", element.TopCenter, 200, 50, 50)
barcode.SetColor(color.NewRgbColorDefault().Red().Color)
pageInput.Elements = append(pageInput.Elements, barcode)
barcodePdf.Inputs = append(barcodePdf.Inputs, pageInput)
fmt.Print(barcodePdf.GetInstructionsJson(true))
resp := barcodePdf.Process()
res := <-resp
if res.IsSuccessful() == true {
os.Remove(outputPath + "barcode-new-example-go-output.pdf")
os.WriteFile(outputPath + "barcode-new-example-go-output.pdf",
res.Content().Bytes(), os.ModeType)
} else {
fmt.Println("Error Message:" + res.ErrorMessage())
fmt.Print(res.ErrorJson())
}
}
def pdfbarcode_example(apikey):
pdf=Pdf()
pdf.api_key = apikey
pdf.author = "John Doe"
pdf.title = "My PDF"
inputPage = pdf.add_page(1008, 612)
code11BarcodeElement = Code11BarcodeElement("12345678", ElementPlacement.TopCenter, 200, 50, 50)
code11BarcodeElement.color = RgbColor.red();
inputPage.elements.append(code11BarcodeElement);
response = pdf.process()
if response.is_successful:
with open(output_path + "barcode-pdf-output.pdf", "wb") as output_file:
output_file.write(response.content)
else:
print(response.error_id)
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.
- Create a new instructions JSON document.
- Create a new
templates
array and anelements
array. - Add the barcode element to the array.
- Add a new
inputs
array and add apdf
input type. - 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"
}
]
}
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.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
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;
}
static async BarcodeExample(apiKey, basePath) {
var pdf = new Pdf();
pdf.author = "John Doe";
pdf.title = "Barcode Example";
var resource = new PdfResource(basePath + "DocumentA.pdf");
var input = new PdfInput(resource);
pdf.inputs.push(input);
var template = new Template("Temp1");
var element = new AztecBarcodeElement("Hello World", elementPlacement.TopCenter, 0, 500);
template.elements.push(element);
input.template = template;
await this.ProcessAndSave(pdf, apiKey, basePath, "json-Barcode-Example-output.pdf");
}
public static Pdf BarcodeExample(String basePath) {
Pdf pdf = new Pdf();
pdf.setAuthor("John Doe");
pdf.setTitle("Barcode Example");
PdfResource resource = new PdfResource(basePath + "/DocumentA.pdf");
PdfInput input = new PdfInput(resource);
pdf.getInputs().add(input);
Template template = new Template("Temp1");
AztecBarcodeElement element = new AztecBarcodeElement("Hello World", ElementPlacement.TOPCENTER, 0, 500);
template.getElements().add(element);
input.setTemplate(template);
return pdf;
}
public static function BarcodeExample() {
$pdf = new Pdf();
$pdf->Author = "John Doe";
$pdf->Title = "Barcode Example";
$resource = new PdfResource(InstructionsExample::$BasePath . "DocumentA.pdf");
$input = new PdfInput($resource);
array_push($pdf->Inputs, $input);
$template = new Template("Temp1");
$element = new AztecBarcodeElement("Hello World", ElementPlacement::TopCenter, 0, 500);
array_push($template->Elements, $element);
$input->Template = $template;
return $pdf;
}
func barcodeExisting() {
barcodePdf := endpoint.NewPdf()
barcodePdf.Endpoint.ApiKey = apiKey
pdfResource := resource.NewPdfResourceWithResourcePath(basePath+"DocumentA.pdf", "DocumentA.pdf")
pdfInput := input.NewPdfWithResource(pdfResource)
barcodePdf.Inputs = append(barcodePdf.Inputs, pdfInput)
pdfTemplate := element.NewTemplate()
pdfTemplate.Id = "Temp1"
barcode := element.NewAztecBarcodeElement("Hello World", element.TopCenter, 0, 500)
barcode.SetColor(color.NewRgbColorDefault().Red().Color)
pdfTemplate.Elements = append(pdfTemplate.Elements, barcode)
pdfInput.SetTemplate(pdfTemplate)
resp := barcodePdf.Process()
res := <-resp
if res.IsSuccessful() == true {
os.Remove(outputPath + "barcode-existing-example-go-output.pdf")
os.WriteFile(outputPath + "barcode-existing-example-go-output.pdf",
res.Content().Bytes(), os.ModeType)
} else {
fmt.Println("Error Message:" + res.ErrorMessage())
fmt.Print(res.ErrorJson())
}
}
def ug_barcode_example(documentPath):
pdf = Pdf()
resource = PdfResource(documentPath + "DocumentA.pdf")
input = pdf.add_pdf(resource)
template = Template("Temp1")
element = AztecBarcodeElement("Hello World", ElementPlacement.TopCenter, 0, 500)
template.elements.append(element)
input.template = template
return pdf
Refer to the documentation on Designer for examples of using barcodes with Designer.
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.