1

There are plenty of questions re - getting working code for inserting images into MS word which I have used amongst other online resources to get me this far.

I've been able to create an array of images(barcodes) to be inserted into word in order to create a single file to print out. I'm using the Barbecue library to generate my bar code images and using Apache POI to insert these into MS Word. What is happening now is that the images are created ,the word document created, an image is inserted into the document however it is a white square, red x in the top left that states "This image cannot be displayed at current."

[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");
    }

Has anyone got any idea what I can do to get the rendered images into word.. The aim is to make a barcode generator app for my business that takes a simple .txt input file and converts this to item barcodes. UI and refactoring work will come later :)

James Sowter
  • 61
  • 1
  • 1
  • 5
  • Forgot to post the link of what the image looks like [1]: https://i.stack.imgur.com/ruX45.png – James Sowter Sep 07 '18 at 15:19
  • 3
    Instead of adding info you forgot in a link please [edit] your question. – Thomas Sep 07 '18 at 15:21
  • 2
    Make sure you validate these PNGs. I can't tell from your description if you actually made sure these generated PNGs were somewhat valid. –  Sep 07 '18 at 15:29
  • I'm having a bit of trouble setting up a hyperlink properly.. Either way the link to the .pngs I've made is above. I will also add, that when I open one of these .pngs and the CTRL+C / CTRL+V in MS word they copy in fine and display as actual barcodes.. I believe the issue lies in how these are either input into word or how the image is rendered once in - I could also be completely wrong! :) – James Sowter Sep 10 '18 at 08:08

0 Answers0