Как DOCX4J вставлен изображение в заголовокJAVA

Программисты JAVA общаются здесь
Anonymous
Как DOCX4J вставлен изображение в заголовок

Сообщение Anonymous »

Я использую docx4j, чтобы вставить изображение в заголовок документа. Тем не менее, я заметил, что в сгенерированном файле DOCX в заголовке есть просто пустое изображение. При использовании того же метода для вставки изображения в основной корпус документа изображение отображается правильно. Я понятия не имею, что идет не так.

Код: Выделить всё

   public static void main(String[] args) throws Exception {
// Load the DOCX file as an input stream
InputStream in = Files.newInputStream(new File("C:\\Users\\lixuejie\\Desktop\\test\\1.docx").toPath());
// Load the WordprocessingML package from the input stream
WordprocessingMLPackage word = WordprocessingMLPackage.load(in);
// Get all sections in the document
List sections = word.getDocumentModel().getSections();
for (SectionWrapper section : sections) {
// Get the header-footer policy for the current section
HeaderFooterPolicy headerFooterPolicy = section.getHeaderFooterPolicy();
// Get the default header part of the current section
HeaderPart headerPart = headerFooterPolicy.getDefaultHeader();
// Get the content of the header part
List content = headerPart.getContent();

// Load the image file
File imageFile = new File("C:\\Users\\lixuejie\\Desktop\\test\\1.jpg");
InputStream imageStream = new FileInputStream(imageFile);

// Convert the image stream to a byte array
byte[] imageBytes = TestService.toByteArray(imageStream);
// Create a paragraph containing the picture
P pictureParagraph = createPictureParagraph(imageBytes, word);

// Add the picture paragraph to the header content
content.add(pictureParagraph);
//            System.out.println(content);
}
// Save the modified WordprocessingML package to a new file
word.save(new File("C:\\Users\\lixuejie\\Desktop\\test\\2.docx"));
}

private static P createPictureParagraph(byte[] imageBytes, WordprocessingMLPackage word) throws Exception {
// The image data in byte array format
byte[] imageData = imageBytes;
// Create a paragraph with the picture
org.docx4j.wml.P pictureParagraph = createPicture(imageData, word);
return pictureParagraph;
}
private static org.docx4j.wml.P createPicture(byte[] imageBytes, WordprocessingMLPackage word) throws Exception {
// The image can be created as an object in the following way:

// Supported image formats: bmp, png, jpg, gif, etc.
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(word, imageBytes);
Inline inlineImage = imagePart.createImageInline("1", "2", 11, 22, false);

// The paragraph for the image format
org.docx4j.wml.P p = new org.docx4j.wml.P();
org.docx4j.wml.R run = new org.docx4j.wml.R();

// Add the image to the run
org.docx4j.wml.Drawing drawing = new org.docx4j.wml.Drawing();
drawing.getAnchorOrInline().add(inlineImage);
//        JAXBElement jaxbElement = new JAXBElement(new QName("drawing"), Drawing.class, R.class, drawing);
run.getContent().add(drawing);
p.getContent().add(run);
//        p.setPPr(new PPr());
return p;
}


Подробнее здесь: https://stackoverflow.com/questions/794 ... headerpart

Вернуться в «JAVA»