Split By Barcode
Splits pages of a document based on the text inside barcode
Code Samples
Try the API in the language you prefer
- C#
- Java
- JavaScript
- PHP
- Python
- Ruby
var splitByBarcodeReq = new SplitByBarcodeReq()
{
Document = new Document
{
DocData = File.ReadAllBytes("mypdf.pdf")
},
SplitByBarcodeAction = new SplitByBarcodeAction
{
BarcodeString = "ara",
BarcodeFilter = SplitByBarcodeActionBarcodeFilter.StartsWith,
BarcodeType = SplitByBarcodeActionBarcodeType.Any,
SplitBarcodePage = SplitByBarcodeActionSplitBarcodePage.Before
}
};
var res = await Pdf4me.Instance.BarcodeClient.SplitByBarcodeAsync(splitByBarcodeReq).ConfigureAwait(false);
int i = 0;
foreach (var doc in res.SplittedDocuments)
File.WriteAllBytes("pdf_" + i + ".pdf", doc.DocData);
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import pdf4me from "pdf4me";
const PDF4ME_API_KEY = 'YOUR API KEY'
// Setup the pdf4meClient
const pdf4meClient = pdf4me.createClient(PDF4ME_API_KEY)
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const splitByBarcode = async () => {
try {
// Create the Split object
const splitReq = {
// Document
document: {
name: "myPdf.pdf",
docData: fs.readFileSync(path.join(__dirname, 'barcode_pdf.pdf')).toString('base64')
},
// Action
splitByBarcodeAction: {
barcodeString: "ara",
barcodeFilter: "startsWith",
barcodeType: "any",
splitBarcodePage: "before",
pdfRenderDpi: 150,
combinePagesWithSameConsecutiveBarcodes: false,
},
};
const splitRes = await pdf4meClient.splitByBarcode(splitReq);
const { splittedDocuments } = splitRes;
// Extract the resulting documents and write them to disk
const length = splittedDocuments?.length || 0;
if (length) {
if (length >= 2) {
const pdf1 = Buffer.from(splittedDocuments[0].docData, 'base64')
const pdf2 = Buffer.from(splittedDocuments[1].docData, 'base64')
fs.writeFileSync(path.join(__dirname, 'splitByBarcode_pdf1_result.pdf'), pdf1)
fs.writeFileSync(path.join(__dirname, 'splitByBarcode_pdf2_result.pdf'), pdf2)
} else {
const pdf1 = Buffer.from(splittedDocuments[0].docData, 'base64')
fs.writeFileSync(path.join(__dirname, 'splitByBarcode_pdf1_result.pdf'), pdf1)
}
}
} catch (error) {
console.error(error);
}
};
splitByBarcode();