Barcode is an identifier that represents a numerical value or character by the thickness of a striped line. A QR code is a display code that has information in the horizontal and vertical directions, as opposed to a barcode that has information only in the horizontal direction. Today I will show you how to create barcodes and QR codes in PDF using Spire.PDF for Java.
1. From the official website of E-iceblue Free Spire.PDF for Java Download the free version.
2. Launch the IDE, create a new project, and then add the appropriate Spire.PDF.jar for the installed files to your references.
Spire.PDF for Java PDF with Codebar, Code11, Code128A, Code128B, Code32, Code39, Code39 Extended, Code93 sum Code93 Extended barcodes Can be created. To save time, let's show you how to create Codebar, Code128A and Code39.
```java import com.spire.pdf.barcode.*; import com.spire.pdf.graphics.*; import static com.spire.pdf.graphics.PdfFontStyle.Bold;import java.awt.*; import java.awt.geom.Point2D; import java.util.EnumSet;
public class DrawBarcode {
public static void main(String[] args) {
//Create a PdfDocument.
PdfDocument doc = new PdfDocument();
//Add one page.
PdfPageBase page = doc.getPages().add();
//Initialize the y variable.
double y = 15;
//Create a font.
PdfFont font= new PdfFont(PdfFontFamily.Helvetica, 12, EnumSet.of(Bold));
//“Codebar” in PDF:Create the text.
PdfTextWidget text = new PdfTextWidget(); text.setFont(font); text.setText("Codebar:"); PdfLayoutResult result = text.draw(page, 0, y); y =(float)(result.getBounds().getY()+ result.getBounds().getHeight() + 2);
//Codebar Create a barcode.
PdfCodabarBarcode codebar= new PdfCodabarBarcode("00:12-3456/7890");
codebar.setBarcodeToTextGapHeight(1f);
codebar.setBarHeight(50f);
codebar.setEnableCheckDigit(true);
codebar.setShowCheckDigit(true);
codebar.setTextDisplayLocation(TextLocation.Bottom);
PdfRGBColor blue = new PdfRGBColor(Color.blue);
codebar.setTextColor(blue);
Point2D.Float point = new Point2D.Float();
point.setLocation(0,y);
codebar.draw(page,point);
y = codebar.getBounds().getY()+ codebar.getBounds().getHeight() + 5;
//Code128 in PDF-A:Create the text.
text.setText("Code128-A:");
result = text.draw(page, 0, y);
page = result.getPage();
y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;
//Create a Code128A barcode.
PdfCode128ABarcode code128 = new PdfCode128ABarcode("HELLO 00-123");
code128.setBarcodeToTextGapHeight(1f);
code128.setBarHeight(50f);
code128.setTextDisplayLocation(TextLocation.Bottom);
code128.setTextColor(blue);
point.setLocation(point.x,y);
code128.draw(page, point);
y =code128.getBounds().getY()+ code128.getBounds().getHeight() + 5;
//In PDF "Code39:Create the text.
text.setText("Code39:");
result = text.draw(page, 0, y);
page = result.getPage();
y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;
//Create a Code39 barcode.
PdfCode39Barcode code39 = new PdfCode39Barcode("16-273849");
code39.setBarcodeToTextGapHeight(1f);
code39.setBarHeight(50f);
code39.setTextDisplayLocation(TextLocation.Bottom);
code39.setTextColor(blue);
point.setLocation(point.x,y);
code39.draw(page, point);
//Save the PDF.
doc.saveToFile("DrawBarcode.pdf");
}
}
<h4> <strong> Completion example </strong> </h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20201230/20201230155930.png " alt="f:id:lendoris:20201230155930p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<h4> <strong> QR code </strong> </h4>
```java
import com.spire.barcode.*;
import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;
import java.awt.*;
import java.awt.image.BufferedImage;
public class AddQRCode {
public static void main(String[] args) {
//Create a PdfDocument and add one page.
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.getPages().add();
//Create a QR code.
BarcodeSettings settings = new BarcodeSettings();
settings.setType(BarCodeType.QR_Code);
settings.setData("123456789");
settings.setData2D("123456789");
settings.setX(1f);
settings.setLeftMargin(0);
settings.setShowTextOnBottom(true);
settings.setQRCodeECL(QRCodeECL.Q);
settings.setQRCodeDataMode(QRCodeDataMode.Numeric);
//Create an image of the QR code.
BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.generateImage();
//Creates an image of the QR code at the specified location.
PdfImage pdfImage = PdfImage.fromImage((BufferedImage)image);
page.getCanvas().drawImage(pdfImage,100,0);
//Save the PDF.
pdf.saveToFile("QR code.pdf");
pdf.dispose();
}
}
Recommended Posts