Pdf - PdfUnidimensionalBarcode
18 Nov 20267 minutes to read
Abstract base class that provides common bar-height and human-readable text behavior for one-dimensional barcodes.
It adds narrowBarWidth, barHeight, and the text font, color, gap, alignment and location settings on top of PdfBarcode.
Create one of the concrete classes (for example PdfCode39Barcode) to draw a barcode on a page or graphics.
// Creates a document.
let document: PdfDocument = new PdfDocument();
// Creates a page.
let page: PdfPage = document.addPage();
// Creates a barcode.
let barcode: PdfCode39Barcode = new PdfCode39Barcode('CODE39');
// Draws the barcode at the specified point.
barcode.draw(page, { x: 20, y: 20 });
// Saves the document.
document.save('output.pdf');
// Destroys the document.
document.destroy();Properties
barHeight number
Gets or sets the bar height in points. The default value is 0, which enables automatic height calculation.
// Creates a document.
let document: PdfDocument = new PdfDocument();
// Creates a page.
let page: PdfPage = document.addPage();
// Creates a barcode.
let barcode: PdfCode39Barcode = new PdfCode39Barcode('CODE39');
// Sets the barHeight.
barcode.barHeight = 60;
// Draws the barcode at the specified point.
barcode.draw(page, { x: 20, y: 20 });
// Saves the document.
document.save('output.pdf');
// Destroys the document.
document.destroy();Defaults to 0 (automatic)
encodeStartStopSymbols boolean
Gets or sets whether applicable start and stop symbols are included in the displayed text. The default value is false.
// Creates a document.
let document: PdfDocument = new PdfDocument();
// Creates a page.
let page: PdfPage = document.addPage();
// Creates a barcode.
let barcode: PdfCode39Barcode = new PdfCode39Barcode('CODE39');
// Sets the encodeStartStopSymbols.
barcode.encodeStartStopSymbols = true;
// Draws the barcode at the specified point.
barcode.draw(page, { x: 20, y: 20 });
// Saves the document.
document.save('output.pdf');
// Destroys the document.
document.destroy();Defaults to false
font PdfFont
Gets or sets the font used for human-readable barcode text. The default value is the internal barcode text font.
// Creates a document.
let document: PdfDocument = new PdfDocument();
// Creates a page.
let page: PdfPage = document.addPage();
// Creates a barcode.
let barcode: PdfCode39Barcode = new PdfCode39Barcode('CODE39');
// Sets the font.
barcode.font = new PdfStandardFont(PdfFontFamily.helvetica, 10);
// Draws the barcode at the specified point.
barcode.draw(page, { x: 20, y: 20 });
// Saves the document.
document.save('output.pdf');
// Destroys the document.
document.destroy();Defaults to Helvetica 8 (PdfStandardFont)
narrowBarWidth number
Gets or sets the width, in points, of the narrowest bar. The default value is 1.
// Creates a document.
let document: PdfDocument = new PdfDocument();
// Creates a page.
let page: PdfPage = document.addPage();
// Creates a barcode.
let barcode: PdfCode39Barcode = new PdfCode39Barcode('CODE39');
// Sets the narrowBarWidth.
barcode.narrowBarWidth = 1.5;
// Draws the barcode at the specified point.
barcode.draw(page, { x: 20, y: 20 });
// Saves the document.
document.save('output.pdf');
// Destroys the document.
document.destroy();Defaults to 1
text string
Gets or sets the barcode payload text. The default value is an empty string.
// Creates a document.
let document: PdfDocument = new PdfDocument();
// Creates a page.
let page: PdfPage = document.addPage();
// Creates a barcode.
let barcode: PdfCode39Barcode = new PdfCode39Barcode('ABC');
// Sets the text.
barcode.text = 'ABC-123';
// Draws the barcode at the specified point.
barcode.draw(page, { x: 20, y: 20 });
// Saves the document.
document.save('output.pdf');
// Destroys the document.
document.destroy();Defaults to ’’
textAlignment PdfBarcodeTextAlignment
Gets or sets the horizontal alignment of human-readable text. The default value is Center.
// Creates a document.
let document: PdfDocument = new PdfDocument();
// Creates a page.
let page: PdfPage = document.addPage();
// Creates a barcode.
let barcode: PdfCode39Barcode = new PdfCode39Barcode('CODE39');
// Sets the textAlignment.
barcode.textAlignment = PdfBarcodeTextAlignment.Left;
// Draws the barcode at the specified point.
barcode.draw(page, { x: 20, y: 20 });
// Saves the document.
document.save('output.pdf');
// Destroys the document.
document.destroy();Defaults to PdfBarcodeTextAlignment.Center
textGap number
Gets or sets the gap, in points, between the bars and human-readable text. The default value is 5.
// Creates a document.
let document: PdfDocument = new PdfDocument();
// Creates a page.
let page: PdfPage = document.addPage();
// Creates a barcode.
let barcode: PdfCode39Barcode = new PdfCode39Barcode('CODE39');
// Sets the textGap.
barcode.textGap = 8;
// Draws the barcode at the specified point.
barcode.draw(page, { x: 20, y: 20 });
// Saves the document.
document.save('output.pdf');
// Destroys the document.
document.destroy();Defaults to 5
textLocation PdfBarcodeTextLocation
Gets or sets the location of human-readable text. The default value is Bottom.
// Creates a document.
let document: PdfDocument = new PdfDocument();
// Creates a page.
let page: PdfPage = document.addPage();
// Creates a barcode.
let barcode: PdfCode39Barcode = new PdfCode39Barcode('CODE39');
// Sets the textLocation.
barcode.textLocation = PdfBarcodeTextLocation.Top;
// Draws the barcode at the specified point.
barcode.draw(page, { x: 20, y: 20 });
// Saves the document.
document.save('output.pdf');
// Destroys the document.
document.destroy();Defaults to PdfBarcodeTextLocation.Bottom
Methods
draw
Draws the barcode within the supplied bounds and returns the occupied rectangle.
// Creates a document.
let document: PdfDocument = new PdfDocument();
// Creates a page.
let page: PdfPage = document.addPage();
// Creates a barcode.
let barcode: PdfCode39Barcode = new PdfCode39Barcode('CODE39');
// Draws the barcode at the specified point.
let natural: Rectangle = barcode.draw(page, { x: 20, y: 20 });
// Draws the barcode within the specified bounds.
barcode.draw(page.graphics, { x: 20, y: natural.y + natural.height + 20, width: 250, height: 100 });
// Saves the document.
document.save('output.pdf');
// Destroys the document.
document.destroy();| Parameter | Type | Description |
|---|---|---|
| target |
PdfPage | PdfGraphics
|
The page or graphics context to draw on. |
| locationOrBounds | Rectangle |
The drawing bounds. Width and height 0 select the natural size; otherwise both must be positive. |
Returns Rectangle