Create barcodes and QR codes in Java PDF

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.

Preparation

1. From the official website of E-iceblue Free Spire.PDF for Java Download the free version.

f:id:lendoris:20201230155752p:plain

2. Launch the IDE, create a new project, and then add the appropriate Spire.PDF.jar for the installed files to your references.

f:id:lendoris:20201230155825p:plain

barcode

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();
    }
}

Completion example

f:id:lendoris:20201230160132p:plain

 

Recommended Posts

Create barcodes and QR codes in Java PDF
Program PDF headers and footers in Java
Create JSON in Java
Save Java PDF in Excel
Java encryption and decryption PDF
Create hyperlinks in Java PowerPoint
Create Azure Functions in Java
Create QR code for Google Authenticator using ZXing in Java
I created a PDF in Java.
Encoding and Decoding example in Java
StringBuffer and StringBuilder Class in Java
Output PDF and TIFF with Java 8
Understanding equals and hashCode in Java
Java draws shapes in PDF documents
Hello world in Java and Gradle
[Java] Create and apply a slide master
Create variable length binary data in Java
Difference between final and Immutable in Java
Java implementation to create and solve mazes
[Java] for Each and sorted in Lambda
Create Java Spring Boot project in IntelliJ
Create a TODO app in Java 7 Create Header
Arrylist and linked list difference in java
Learn Flyweight patterns and ConcurrentHashMap in Java
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java
Difference between int and Integer in Java
Discrimination of Enums in Java 7 and above
Install Docker and create Java runtime environment
How to create your own annotation in Java and get the value
[Java] [POI] Create a table in Word and start a new line in one cell
Regarding the transient modifier and serialization in Java
Create API using Retrofit2, Okhttp3 and Gson (Java)
Read a string in a PDF file with Java
Detect similar videos in Java and OpenCV rev.2
Create a CSR with extended information in Java
Parallel and parallel processing in various languages (Java edition)
Difference between next () and nextLine () in Java Scanner
Try to create a bulletin board in Java
Differences in writing Java, C # and Javascript classes
Text extraction in Java from PDF with pdfbox-2.0.8
Let's create a super-simple web framework in Java
JAVA: Realizes generation and scanning of various barcodes
Capture and save from selenium installation in Java
Detect similar videos in Java and OpenCV rev.3
Add, read, and delete Excel comments in Java
Check static and public behavior in Java methods
[Java] Understand in 10 minutes! Associative array and HashMap
Basics of threads and Callable in Java [Beginner]
Distinguish between positive and negative numbers in Java
Java adds and removes watermarks in word documents
Detect similar videos in Java and OpenCV rev.1
I tried to create Alexa skill in Java
Represents "next day" and "previous day" in Java / Android
Questions in java exception handling throw and try-catch
Upload and download notes in java on S3
Encrypt / decrypt with AES256 in PHP and Java
Generate OffsetDateTime from Clock and LocalDateTime in Java
Compare PDF output in Java for snapshot testing
Java enables extraction of PDF text and images