Export Barcodes to Images and PDF
- 4 minutes to read
Use the BarcodeGenerator class to export barcodes to image streams, in-memory images, and PDF documents.
Export API
| Method | Use when you need |
|---|---|
| BarcodeGenerator.export() | To write barcode image data directly to an OutputStream or WritableByteChannel. |
| BarcodeGenerator.exportToImage() | To create a barcode image in memory as a DXImage object. |
| BarcodeGenerator.exportToPdf() | To write barcode content directly to a PDF document. |
Barcode export methods include overloads for the following input and output data types:
- Input
String textString text, Charset encodingbyte[] binaryData
- Output
OutputStreamWritableByteChannel- DXImage
- Optional parameter
- DXImageFormat for image export
Note
If you omit the image format or pass null, the API uses PNG.
Export to an Image Stream
Use the export() method group to write barcode image data directly to an OutputStream or WritableByteChannel.
The following code snippet exports a QR Code to a PNG file:
import com.devexpress.drawing.*;
import com.devexpress.docs.barcode.*;
import java.io.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
QRCodeOptions options = new QRCodeOptions();
options.setModuleSize(6);
options.setShowText(false);
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
try (FileOutputStream stream = new FileOutputStream(
outputDir.resolve("qr-code.png").toFile());
BarcodeGenerator generator = new BarcodeGenerator(options)) {
// Export the barcode as a PNG image.
generator.export(
"https://www.devexpress.com",
stream,
DXImageFormat.getPng());
}
}
}
Use export(String text, OutputStream stream) when PNG output is sufficient. Use an overload that accepts DXImageFormat to export the barcode in another image format.
Return a DXImage Object
Use the exportToImage() method to obtain a DXImage object (for example, for image previews, post-processing, and custom save logic).
The following code snippet creates a barcode image in memory and saves it to a file:
import com.devexpress.drawing.*;
import com.devexpress.docs.barcode.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
QRCodeOptions options = new QRCodeOptions();
options.setModuleSize(8);
options.setShowText(false);
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
try (BarcodeGenerator generator = new BarcodeGenerator(options)) {
// Create an image in memory.
DXImage image = generator.exportToImage(
"https://www.devexpress.com",
StandardCharsets.UTF_8,
DXImageFormat.getPng());
// Save the generated image.
image.save(
outputDir.resolve("qr-code-from-image-object.png").toString(),
DXImageFormat.getPng());
}
}
}
Use exportToImage(byte[] binaryData) or exportToImage(byte[] binaryData, DXImageFormat format) when the symbology requires binary input.
Export to a Vector Image
Use the getEmf() method to generate a vector image. Use the EMF format with export() or exportToImage().
The following code snippet exports binary barcode data to an EMF file:
import com.devexpress.drawing.*;
import com.devexpress.docs.barcode.*;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
Code128Options options = new Code128Options();
options.setModuleSize(4);
options.setShowText(true);
byte[] binaryData = "ORD-4589-US-25A".getBytes(StandardCharsets.UTF_8);
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
try (WritableByteChannel channel = Files.newByteChannel(
outputDir.resolve("code128.emf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
BarcodeGenerator generator = new BarcodeGenerator(options)) {
// Export the barcode as an EMF image.
generator.export(
binaryData,
channel,
DXImageFormat.getEmf());
}
}
}
Export to PDF
Use the exportToPdf() method to generate PDF output. This method writes PDF content to an OutputStream or WritableByteChannel.
The following code snippet exports a barcode to a PDF document:
import com.devexpress.docs.barcode.*;
import java.io.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
EAN128Options options = new EAN128Options();
options.setShowText(true);
options.setWidth(600);
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
try (FileOutputStream stream = new FileOutputStream(
outputDir.resolve("ean128.pdf").toFile());
BarcodeGenerator generator = new BarcodeGenerator(options)) {
// Export the barcode to PDF.
generator.exportToPdf(
"(01)09506000134352(17)260630(10)ABC123",
stream);
}
}
}
Use the exportToPdf(byte[] binaryData, WritableByteChannel channel) method if you work with raw binary data.
Specify Character Encoding
Use a Charset overload when the text requires a specific encoding. These overloads are available for export(), exportToImage(), and exportToPdf() methods.
The following code snippet exports text with UTF-8 encoding:
import com.devexpress.drawing.*;
import com.devexpress.docs.barcode.*;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
QRCodeOptions options = new QRCodeOptions();
options.setModuleSize(6);
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
try (FileOutputStream stream = new FileOutputStream(
outputDir.resolve("utf8-qr-code.png").toFile());
BarcodeGenerator generator = new BarcodeGenerator(options)) {
// Use UTF-8 for the input text.
generator.export(
"Überweisung 123",
StandardCharsets.UTF_8,
stream,
DXImageFormat.getPng());
}
}
}
Note
Ensure that the selected barcode symbology supports characters in the encoded text.