java - 将 .png 图像插入 MS Word 给出白色方块“此时无法显示此图像
问题描述
有很多问题要重新获得将图像插入 MS Word 的工作代码,我在其他在线资源中使用过这些代码来让我走到这一步。
我已经能够创建要插入到 word 中的图像(条形码)数组,以便创建一个要打印的文件。我正在使用 Barbecue 库来生成我的条形码图像,并使用 Apache POI 将它们插入到 MS Word 中。现在发生的事情是创建了图像,创建了word文档,将图像插入到文档中,但是它是一个白色的正方形,左上角的红色x表示“当前无法显示此图像”。
[sample barcode][[https://imgur.com/a/bD4GrBE]
public class Barcode {//Creates a bar code object
//Instance fields
public String barcode;
public int count;
//Getters and Setters
public String getBarcode(){
return barcode.toString();
}
public void setBarcode(String barcode){
this.barcode = barcode.toString();
}
public int getCount(){
return count;
}
//Methods
public Barcode(String code){
setBarcode(code);
++count;
}
}
public class BarcodeArray {//creates the bar code array
// Instance fields
Barcode[] barcodes;
public int numBarcodes;
final static int MAX_BARCODES = 2000;
// Getters and Setters
public Barcode[] getBarcodes() {
return barcodes;
}
public void setBarcodes(Barcode[] barcodes) {
this.barcodes = barcodes;
}
public int getNumBarcodes() {
return numBarcodes;
}
// Methods
BarcodeArray() {//instantiates the bar code array with a fixed length.. //this will be changed to create the array based on the number of bar codes in the input file
barcodes = new Barcode[MAX_BARCODES];
}
void add(Barcode Barcode) {//method for adding a bar code to the array
if (numBarcodes < MAX_BARCODES) {
barcodes[numBarcodes] = Barcode;
numBarcodes++;
System.out.println("Barcode: " + Barcode.getBarcode() + " added to the array");
}
}
}
public class PrintBarcodes {
public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException {
BarcodeArray myBarcode = new BarcodeArray();// instantiates new array instantiates new text file
File file = new File("C:\\Java_Training\\Barcodes\\BarcodesForTesting.txt");
try {
Scanner sc = new Scanner(file);// opens a new scanner object adds bar codes from the input file to the array
while (sc.hasNextLine())
myBarcode.add(new Barcode(sc.nextLine()));//steps through the file and creates bar codes until scanner hits the end of file
sc.close();
} catch (Exception e) {
System.out.println("File not found");
}System.out.println("Total barcodes added to print queue: " + myBarcode.getNumBarcodes());//Prints total count
try {
for (int i = 0; i < myBarcode.numBarcodes; i++) {//For loop to save bar codes to the file directory
net.sourceforge.barbecue.Barcode b = BarcodeFactory.createCode128(myBarcode.getBarcodes()[i].barcode.toString());//Creates the bar code
File save = new File("C:\\Java_Training\\Barcodes\\" + myBarcode.getBarcodes()[i].barcode.toString() + ".jpg");
BarcodeImageHandler.saveJPEG(b, save);
String dirPath = "C:\\Java_Training\\Barcodes\\";//file path to look in
File dir = new File(dirPath);
File[] images = dir.listFiles();//array of file objects
for(File aFile : images){
System.out.println(aFile.getName());
XWPFDocument doc = new XWPFDocument();//creates a new word document
XWPFParagraph title = doc.createParagraph();//defines a paragraph
XWPFRun run = title.createRun();
run.setText("Barcodes");//Sets title
run.setBold(true);//Title style
title.setAlignment(ParagraphAlignment.CENTER);//title location on page
String imgFile = "C:\\Java_Training\\Barcodes\\" + aFile.getName();
FileInputStream fs = new FileInputStream(imgFile);
run.addBreak();
run.addPicture(fs, XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(50), Units.toEMU(150)); // 200x200 pixels
FileOutputStream fos = new FileOutputStream("C:\\Java_Training\\Barcodes\\Barcodes.docx");
fs.close();
doc.write(fos);
fos.close();
}
}
} catch (Exception e) {
System.out.println("Unable to save file");
}
有没有人知道我可以做些什么来将渲染的图像转换成文字。目的是为我的企业制作一个条形码生成器应用程序,该应用程序采用简单的 .txt 输入文件并将其转换为项目条形码。UI 和重构工作将在稍后进行 :)
解决方案
推荐阅读
- sql - SQL 列命名最佳实践,我应该使用缩写吗?
- python - 'dict' 对象没有属性 'load'
- python - sqlalchemy:如何在不修改现有类的情况下映射它?
- spring-boot - 基于单个不同列选择唯一行的 JPA 规范
- c - 使用 Mingw64 或 Cygwin 编译器编写 Windows STDCALL DLL
- symfony - 如何使我的 composer.lock 版本适应较低的 php 版本?
- flutter - Fluttertoast.showToast - 如何在 toast 中居中文本
- azure - 创建 MSTeams Bot 是否需要 Azure 订阅
- java - 从 Redis 服务器中检索和删除单个调用中的密钥
- python - QGraphicScene 背景