Code sample for add/barcode
API
C#
This C# sample demonstrates how to access the PDFTeam add/barcode
API endpoint. The sample takes an existing PDF and adds two barcodes: A QR code is positioned in the top right, a Code 128 in the middle left. Both codes are created without human readable text, the Code 128 barcode is rotated 90 degrees. The resulting PDF will be stored in AWS S3, the permalink is returned to the caller.
POST parameters are in a separate file. The sample code expects the params
-file to be in the same folder as the executable. Make sure to set a build action so that the file is copied to the output directory.
Here's a sample Visual Studio project (note yellow highlight):
The params
-file defines two overlays, one with QR (type: 19), the other one with Code 128 (type: 7):
// Sample C# code for accessing the PDFTeam API, endpoint: add/barcode
// File: params.json
{
"job": "00012345",
"user": "YOUR_USERNAME",
"key": "YOUR_APIKEY",
"test": false,
"inputOptions": {
"source": "https://pdfteam.com/samples/invoice.pdf"
},
"outputOptions": {
"storage": true,
"file": "addbarcode_csharp.pdf"
},
"overlays": [
{
"position": {
"x": "179mm",
"y": "237mm"
},
"type": "barcode",
"config": {
"type": 19,
"data": "https://pdfteam.com"
}
},
{
"rotate": 90,
"position": {
"x": "10mm",
"y": "middle"
},
"type": "barcode",
"config": {
"humanReadableText": false,
"type": 7,
"moduleHeight": 10,
"data": "00012345"
}
}
]
}
NOTE: Setting "test" to TRUE will not consume any credits but the generated PDF carries a watermark.
Main program:
// Sample C# code for accessing the PDFTeam API, endpoint: add/barcode
// Sample uses .NET 8 (LTS). For older versions, use Newtonsoft.JSON package
// to parse the params file
//
// File: program.cs
using System.Text;
namespace PDFTeam
{
class Program
{
static async Task Main(string[] args)
{
var uri = new Uri("https://api.pdfteam.com/api/add/barcode");
using (var client = new HttpClient())
{
StreamReader sr = new StreamReader("params.json");
string json = sr.ReadToEnd();
var content = new StringContent(json, Encoding.UTF8, "application/json");
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = uri,
Content = content
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
}
}
}
}
Sample response:
{"endpoint": "add/barcode", "job": "00012345", "credits": 1, "result": "https://pdfteam.s3.eu-north-1.amazonaws.com/.../addbarcode_csharp.pdf"}
Sample result: