gpt4 book ai didi

java - 生成并使用 PDF 底部的条形码

转载 作者:行者123 更新时间:2023-12-02 05:36:07 25 4
gpt4 key购买 nike

我已经使用IText为我的pdf生成条形码。我对这行特定的代码有疑问。

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Mobile/billPayment.pdf");

当我将输出文件指向billPayment.pdf时,它会覆盖现有数据,删除pdf上的所有内容,只给我条形码。我是否可以保留现有数据以及生成的条形码?

最佳答案

以下是使用开源技术生成 pdf 并具有条形码的示例:(https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CCUQiBUwAWoVChMI6KiL5urIyAIVRSmICh1WhASJ&url=https%3A%2F%2Fplus.google.com%2F106756017574705877581%2Fposts%2FZLE22J3jPMa&usg=AFQjCNFFs6Vfdq4Sr-mtjcDXfgcX0SxNKw&sig2=swMxmIhVUqq2iiZnuwssow)

使用开源 API 生成具有字符串条形码(即 SKU)的 PDF。

验收标准是生成的条形码应该能够使用任何条形码扫描仪进行扫描。为了生成条形码,我使用了“Zxing”API。以下示例中使用的 jar 是:core-2.2.jar、javase.jar。为了生成 PDF,我使用了 PDFBox API。在此示例中,我使用了以下 jar:pdfbox-1.8.10.jar、pdfbox-app-1.8.10.jar。上述 jar 可以在下面的链接中找到:

https://sites.google.com/site/sujeetcorp/files/PDFBox%26Zxing_jars.zip?attredirects=0&d=1

在下面的示例中,我创建了一个包含两页的 PDF 文件,每页上有两个不同的条形码。这是代码:

        package com.barcode;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;


public class BarCode {

public static void main (String[] args) throws Exception {
BitMatrix bitMatrix;

String outputFileName = "Simple.pdf";
File outPutFile=new File(outputFileName);

PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
PDRectangle rect = page.getMediaBox();
document.addPage(page);

// Create a new font object selecting one of the PDF base fonts

PDFont fontMono = PDType1Font.COURIER;

// Start a new content stream which will "hold" the to be created content
PDPageContentStream cos = new PDPageContentStream(document, page);

int line = 0;
// add an image
try {

cos.beginText();
cos.setFont(fontMono, 20);
cos.setNonStrokingColor(Color.BLUE);
cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
cos.drawString("APL IPHONE 5C BLUE 16GB KIT");
cos.endText();

cos.beginText();
cos.setFont(fontMono, 20);
cos.setNonStrokingColor(Color.BLUE);
cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
cos.drawString("SKU:");
cos.endText();

bitMatrix = new Code128Writer().encode("M1G542LL/A", BarcodeFormat.CODE_128, 150, 80, null);
BufferedImage buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);
PDXObjectImage ximage = new PDPixelMap(document, buffImg);
cos.drawXObject(ximage, 150, rect.getHeight() - 50*(++line), 150, 50);

cos.beginText();
cos.setFont(fontMono, 10);
cos.setNonStrokingColor(Color.BLUE);
cos.moveTextPositionByAmount(200, rect.getHeight() - 50*(++line));
cos.drawString("M1G542LL/A");
cos.endText();

cos.beginText();
cos.setFont(fontMono, 20);
cos.setNonStrokingColor(Color.BLUE);
cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
cos.drawString("IMEI:");
cos.endText();

bitMatrix = new Code128Writer().encode("123456789", BarcodeFormat.CODE_128, 150, 80, null);
buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);
ximage = new PDPixelMap(document, buffImg);
cos.drawXObject(ximage, 150, rect.getHeight() - 50*(++line), 150, 50);

cos.close();



page = new PDPage(PDPage.PAGE_SIZE_A4);
rect = page.getMediaBox();
document.addPage(page);
line = 0;

// Start a new content stream which will "hold" the to be created content
cos = new PDPageContentStream(document, page);
cos.beginText();
cos.setFont(fontMono, 20);
cos.setNonStrokingColor(Color.BLUE);
cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
cos.drawString("APL IPHONE 5C BLUE 16GB KIT");
cos.endText();

cos.beginText();
cos.setFont(fontMono, 20);
cos.setNonStrokingColor(Color.BLUE);
cos.moveTextPositionByAmount(100, rect.getHeight() - 60*(++line));
cos.drawString("SKU:");
cos.endText();


bitMatrix = new Code128Writer().encode("M1G542LL/A", BarcodeFormat.CODE_128, 150, 80, null);
buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);

ximage = new PDPixelMap(document, buffImg);
cos.drawXObject(ximage, 150, rect.getHeight() - 60*(++line), 150, 50);
cos.beginText();
cos.setFont(fontMono, 10);
cos.setNonStrokingColor(Color.BLUE);
cos.moveTextPositionByAmount(200, rect.getHeight() - 60*(line)-10);
cos.drawString("M1G542LL/A");
cos.endText();


cos.beginText();
cos.setFont(fontMono, 20);
cos.setNonStrokingColor(Color.BLUE);
cos.moveTextPositionByAmount(100, rect.getHeight() - 60*(++line));
cos.drawString("IMEI:");
cos.endText();

bitMatrix = new Code128Writer().encode("352065061762230", BarcodeFormat.CODE_128, 150, 80, null);
buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);
ximage = new PDPixelMap(document, buffImg);
cos.drawXObject(ximage, 150, rect.getHeight() - 60*(++line), 150, 50);


} catch (FileNotFoundException fnfex) {
System.out.println("No image for you");
}

cos.close();

document.save(outputFileName);
document.close();
}
}

关于java - 生成并使用 PDF 底部的条形码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24948065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com