2013-01-06 7 views
0

docx4j를 사용하여 docx를 조작하고 있습니다. 무엇을 하려는지 모든 테이블의 모든 이미지의 버퍼링 된 이미지를 만드는 것입니다. org.docx4j.wml.Drawing의 인스턴스가 있는데 거기에서 버퍼링 된 이미지를 만들 수있는 방법이 있습니까?Docx4j 버퍼 이미지

소스 코드는 대부분 어딘가에서 다운로드됩니다.

만 관련 부분 :

for (Object o : children) {     
      System.out.println(" " + o.getClass().getName()); 
      if (o instanceof javax.xml.bind.JAXBElement) { 
       System.out.println("  " + ((JAXBElement)o).getName()); 
       System.out.println("  " + ((JAXBElement)o).getDeclaredType().getName()); 

       // TODO - unmarshall directly to Text. 
       if (((JAXBElement)o).getDeclaredType().getName().equals("org.docx4j.wml.Text")) { 
        org.docx4j.wml.Text t = (org.docx4j.wml.Text)((JAXBElement)o).getValue(); 
        System.out.println("  " + t.getValue()); 


       } else if (((JAXBElement)o).getDeclaredType().getName().equals("org.docx4j.wml.Drawing")) { 
        org.docx4j.wml.Drawing d = (org.docx4j.wml.Drawing)((JAXBElement)o).getValue(); 
        describeDrawing(d); 


       } 

이 기본적으로 표의 셀을 통해 이동하고 텍스트 나 그림

내가 사진에서 BufferedImage의를 만들고 싶어 wherther 결정합니다.

완전한 소스 코드 :

private static void bordel(org.docx4j.wml.Drawing d){ 
     MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); 
     RelationshipsPart relsPart = documentPart.getRelationshipsPart(); 
     Relationships rels = relsPart.getRelationships(); 
     List<Relationship> relsList = rels.getRelationship(); 

     for (Relationship r : relsList) { 

      JAXBElement el = printInfo(relsPart.getPart(r)); 
      if (el != null) { 

        if (el.getDeclaredType().getName().equals("org.docx4j.wml.Drawing")) { 


         org.docx4j.wml.Drawing c = (org.docx4j.wml.Drawing) el.getValue(); 
         if (c.equals(d)){ 
          System.out.println("I found part"); 
         } 


        } 
      } 



     }   
    } 

이 방법은 셀에있는 모든 이미지를 불러 내가 믿는 부분을 찾기 위해 시도한다됩니다 :

import java.util.List; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBElement; 

import org.docx4j.dml.picture.Pic; 
import org.docx4j.dml.wordprocessingDrawing.Anchor; 
import org.docx4j.dml.wordprocessingDrawing.Inline; 
import org.docx4j.openpackaging.io.SaveToZipFile; 
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; 
import org.docx4j.wml.Body; 



public class Test { 

    public static JAXBContext context = org.docx4j.jaxb.Context.jc; 

    /** 
    * @param args 
    */ 
    private static WordprocessingMLPackage wordMLPackage; 

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

     //String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/jpeg.docx"; 
     String inputfilepath = "C:\\Users\\Blizzard\\Desktop\\VSE\\15.docx"; 

     boolean save = false; 
     String outputfilepath = "C:\\Users\\Blizzard\\Desktop\\VSE\\112.docx"; 


     // Open a document from the file system 
     // 1. Load the Package 
     wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); 

     // 2. Fetch the document part  
     MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); 

     // Display its contents 
     System.out.println("\n\n OUTPUT "); 
     System.out.println("====== \n\n "); 

     org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement(); 
     Body body = wmlDocumentEl.getBody(); 

     List <Object> bodyChildren = body.getEGBlockLevelElts(); 

     walkJAXBElements(bodyChildren);   

     // Save it 

     if (save) {  
      SaveToZipFile saver = new SaveToZipFile(wordMLPackage); 
      saver.save(outputfilepath); 
     } 
    } 

    static void walkJAXBElements(List <Object> bodyChildren){ 

     for (Object o : bodyChildren) { 

      if (o instanceof javax.xml.bind.JAXBElement) { 

       System.out.println(o.getClass().getName()); 
       System.out.println(((JAXBElement)o).getName()); 
       System.out.println(((JAXBElement)o).getDeclaredType().getName() + "\n\n"); 

       if (((JAXBElement)o).getDeclaredType().getName().equals("org.docx4j.wml.Tbl")) { 
        org.docx4j.wml.Tbl tbl = (org.docx4j.wml.Tbl)((JAXBElement)o).getValue(); 
        describeTable(tbl); 
       } 
      } else if (o instanceof org.docx4j.wml.P) { 
       System.out.println("Paragraph object: "); 

       if (((org.docx4j.wml.P)o).getPPr() != null 
         && ((org.docx4j.wml.P)o).getPPr().getRPr() != null 
         && ((org.docx4j.wml.P)o).getPPr().getRPr().getB() !=null) { 
        System.out.println("For a ParaRPr bold!"); 
       } 


       walkList(((org.docx4j.wml.P)o).getParagraphContent()); 
      } 
     } 
    } 

    static void walkList(List children){ 

     for (Object o : children) {      
      System.out.println(" " + o.getClass().getName()); 
      if (o instanceof javax.xml.bind.JAXBElement) { 
       System.out.println("  " + ((JAXBElement)o).getName()); 
       System.out.println("  " + ((JAXBElement)o).getDeclaredType().getName()); 

       // TODO - unmarshall directly to Text. 
       if (((JAXBElement)o).getDeclaredType().getName().equals("org.docx4j.wml.Text")) { 
        org.docx4j.wml.Text t = (org.docx4j.wml.Text)((JAXBElement)o).getValue(); 
        System.out.println("  " + t.getValue()); 


       } else if (((JAXBElement)o).getDeclaredType().getName().equals("org.docx4j.wml.Drawing")) { 
        org.docx4j.wml.Drawing d = (org.docx4j.wml.Drawing)((JAXBElement)o).getValue(); 
        String relation = describeDrawing(d); 


       } 



      } else if (o instanceof org.w3c.dom.Node) { 
       System.out.println(" IGNORED " + ((org.w3c.dom.Node)o).getNodeName());     
      } else if (o instanceof org.docx4j.wml.R) { 
       org.docx4j.wml.R run = (org.docx4j.wml.R)o; 
       if (run.getRPr()!=null) { 
        System.out.println("  " + "Properties..."); 
        if (run.getRPr().getB()!=null) { 
         System.out.println("  " + "B not null ");      
         System.out.println("  " + "--> " + run.getRPr().getB().isVal()); 
        } else { 
         System.out.println("  " + "B null.");            
        } 
       } 
       walkList(run.getRunContent());    

      } else { 

       System.out.println(" IGNORED " + o.getClass().getName()); 

      } 
//   else if (o instanceof org.docx4j.jaxb.document.Text) { 
//    org.docx4j.jaxb.document.Text t = (org.docx4j.jaxb.document.Text)o; 
//    System.out.println("  " + t.getValue());     
//   } 
     } 
    } 

    static void describeTable(org.docx4j.wml.Tbl tbl) { 

     // What does a table look like? 
     boolean suppressDeclaration = false; 
     boolean prettyprint = true; 
     System.out.println(org.docx4j.XmlUtils.marshaltoString(tbl, suppressDeclaration, prettyprint)); 

     // Could get the TblPr if we wanted them 
     org.docx4j.wml.TblPr tblPr = tbl.getTblPr(); 

     // Could get the TblGrid if we wanted it 
     org.docx4j.wml.TblGrid tblGrid = tbl.getTblGrid(); 

     // But here, let's look at the table contents 
     for (Object o : tbl.getEGContentRowContent()) { 

      if (o instanceof org.docx4j.wml.Tr) { 

       org.docx4j.wml.Tr tr = (org.docx4j.wml.Tr)o; 

       for (Object o2 : tr.getEGContentCellContent()) { 

         System.out.println(" " + o2.getClass().getName()); 
         if (o2 instanceof javax.xml.bind.JAXBElement) { 

          if (((JAXBElement)o2).getDeclaredType().getName().equals("org.docx4j.wml.Tc")) { 
           org.docx4j.wml.Tc tc = (org.docx4j.wml.Tc)((JAXBElement)o2).getValue(); 

           // Look at the paragraphs in the tc 
           walkJAXBElements(tc.getEGBlockLevelElts()); 

          } 

          else { 
           // What is it, if it isn't a Tc? 
           System.out.println("  " + ((JAXBElement)o).getName()); 
           System.out.println("  " + ((JAXBElement)o).getDeclaredType().getName()); 
          } 
         } else { 
          System.out.println("A " + o.getClass().getName());        
         } 

       } 


      } else { 
       System.out.println("C " + o.getClass().getName()); 
      } 

     } 



    } 

    static String describeDrawing(org.docx4j.wml.Drawing d) { 

     System.out.println(" describeDrawing "); 
     String vrat = null; 
     if (d.getAnchorOrInline().get(0) instanceof Anchor) { 

      System.out.println(" ENCOUNTERED w:drawing/wp:anchor "); 
      // That's all for now... 

     } else if (d.getAnchorOrInline().get(0) instanceof Inline) { 

      // Extract w:drawing/wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:embed 

      Inline inline = (Inline)d.getAnchorOrInline().get(0); 

      Pic pic = inline.getGraphic().getGraphicData().getPic(); 

      //pic. 

      vrat = pic.getNvPicPr().getCNvPr().getName(); 

      System.out.println("image name: " + vrat); 
      //bordel(inline); 



     } else { 

      System.out.println(" Didn't get Inline :( How to handle " + d.getAnchorOrInline().get(0).getClass().getName()); 
     } 
     return vrat; 
    } 



} 

나는 RLY 필사적 받고 몇 가지 방법과 메신저를 추가하는 시도 버퍼링 된 이미지로 변환 할 수 있습니다.

그러나 Part를 JAXBElement로 변환하는 다음 메소드는 항상 null을 반환합니다.

public static JAXBElement printInfo(Part p) { 

     JAXBElement el = null; 


     if (p instanceof JaxbXmlPart) { 

      Object o = ((JaxbXmlPart)p).getJaxbElement(); 

      if (o instanceof javax.xml.bind.JAXBElement) { 
       //sb.append(" containing JaxbElement:" + XmlUtils.JAXBElementDebug((JAXBElement)o)); 
       el= (JAXBElement) o; 
      } else { 
       //sb.append(" containing JaxbElement:" + o.getClass().getName()); 
      } 
     } 

     return el; 
    } 
+0

이것은 엄청난 양의 코드입니다. 나는 많은 사람들이 그것을 읽지 않을지 의심 스럽다. 관련 부분으로 축소 할 수 있습니까? – marko

답변

0

다음 코드는 당신을 위해 이미지의 관계를 발견 할 것이다 :

BlipFinder bf = new BlipFinder(); 
    new TraversalUtil(paragraphs, bf); 

    for (CTBlip imageReference : bf.blips) { 

     if (imageReference.getLink() != null 
       && !imageReference.getLink().equals("")) { 

      Relationship existingRel = docxPkg.getMainDocumentPart() 
        .getRelationshipsPart().getRelationshipByID(
          imageReference.getLink()); 

      : 

     } else if (imageReference.getEmbed() != null) { 

      String relId = imageReference.getEmbed(); 

      Relationship r = docxPkg.getMainDocumentPart().getRelationshipsPart().getRelationshipByID(relId); 
      if (r.getTargetMode()!=null 
        && r.getTargetMode().toLowerCase().equals("external")) { 
       : 

      } else { 

       BinaryPartAbstractImage oldPart = (BinaryPartAbstractImage)docxPkg.getMainDocumentPart().getRelationshipsPart().getPart(relId); 

       : 

      } 

     } else { 
      log.error("HELP! neither linked nor embedded?"); 
     } 

    } 
} 

    static class BlipFinder extends CallbackImpl { 

     List<CTBlip> blips = new ArrayList<CTBlip>(); 

     @Override 
     public List<Object> apply(Object o) { 

      if (o instanceof CTBlip) 
       blips.add((CTBlip)o); 

      return null; 
     } 

및 표시된 경우

, 당신에게 BinaryPartAbstractImage을 제공합니다. 일단 당신이 그것을 가지고 있다면, 당신은 무엇이든 할 수 있습니다 ...

+0

와우, 이건 정말 끝내주는 데, 그 사람이 실제로 그걸로 귀찮게 할 것을 기대하지 않았다. 정말 고마워! –

+0

BufferedImage를 가져 오는 데 사용했습니다. BufferedImage a = (BufferedImage) ImageIO.createImageInputStream (oldPart); 이 맞나요? –

+0

아니요,하지만 oldPart.getBuffer()는 ByteBuffer를 반환합니다. – JasonPlutext