2017-05-11 3 views
0

docx 파일의 XML 구조에서 그림으로 텍스트를 바꾸기를 원합니다. 나는 그런 식으로 뭔가를 시도했다 : 먼저 내가 XML에 좋은 텍스트를 검색하고 내가 그리기 개체를 만든 후에 사진을 넣어 내가 무승부에 넣어 모르는 지금Docx4j가 XML의 그림으로 텍스트를 바꿉니다.

List<Object> list = this.getDocumentPart().getJAXBNodesViaXPath(xpath, false); 
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory(); 
org.docx4j.wml.P para = factory.createP(); 
org.docx4j.wml.Drawing draw = factory.createDrawing(); 
((R)list.get(0)).getContent().clear(); 
((R)list.get(0)).getContent().add(draw); 
para.getContent().add(((R)list.get(0))); 
try { 
    this.getWordMLPackage().save(new java.io.File("C:\\user\\result.docx")); 
} catch (Docx4JException e) { 
    e.printStackTrace(); 
} 

. 내 그림을 추가하고,이 단계에서 내 docx를 열고 싶을 때 문제가있다. 어떤 생각?

답변

1

나는 해결책을 게시 할 수 있도록 문제를 수정하여 누군가를 도울 수 있습니다.

먼저 그림에서 인라인을 추가해야하므로 2 가지 작업이 필요합니다. 먼저 ByteArray에서 그림을 변환합니다.

private static byte[] convertImageToByteArray(File file) throws FileNotFoundException, IOException { 
    InputStream is = new FileInputStream(file); 
    long length = file.length(); 
    if (length > Integer.MAX_VALUE) { 
     System.out.println("Fichier trop volumineux."); 
    } 
    byte[] bytes = new byte[(int)length]; 
    int offset = 0; 
    int numRead = 0; 
    while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
     offset += numRead; 
    } 
    if (offset < bytes.length) { 
     System.out.println("Impossible de lire en entier le fichier: " + file.getName()); 
    } 
    is.close(); 
    return bytes; 
} 

그 후에는 인라인을 만들어야합니다

File fileLogo = new File(this.cusDir+mappings.get("logo")); 
org.docx4j.wml.Drawing draw = factory.createDrawing(); 
((R)list.get(i)).getContent().clear(); 
((R)list.get(i)).getContent().add(draw); 
draw.getAnchorOrInline().add(createInline(fileLogo)); 
:
public Inline createInline(File filePict) throws Exception{ 
    byte[] bytes = convertImageToByteArray(filePict); 
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(this.getWordMLPackage(), bytes); 
    int id1 = 1; 
    int id2 = 2; 
    Inline inline = imagePart.createImageInline("Filename hint", filePict.getName(), id1, id2, false); 
    return inline; 
} 

그리고 도면에 인라인을 추가로

관련 문제