Inserir imagem em uma tabela

0 respostas
L

Estou usando esses metodo para adicionar uma tabela e nela vai uma imagem em um campo e adiciona so que tem um problema quando passo para pdf nao vai a imagem o metodo ta ai tbem alguem ajuda ai por favor… Percebi que no xml tem inline, drawing diferente na imagem so que nao consigo colocar pra inserir na linha e coluna.

public static void addRowToTable(Tbl reviewtable, Tr templateRow, Map<String, String> replacements) {
		Tr workingRow = (Tr) XmlUtils.deepCopy(templateRow);
		List<?> textElements = getAllElementFromObject(workingRow, Text.class);
		int p = 0;
		for (Object object : textElements) {
			Text text = (Text) object;
			String replacementValue = (String) replacements.get(text.getValue());
			if (text.getValue().toUpperCase().equals("CLIENTE_IMG")) {
				//------------------------------------------ COMECO ---------------------------------------------
				try {
					ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
					String path = servletContext.getRealPath("") + File.separator + replacementValue;
				 	File file = new File(path);  
				 	workingRow.getContent().add(p, criarXmlImage(file));
				}catch(Exception ex){
					System.out.println("Error.: " + ex.getMessage());
					ex.printStackTrace();
				}
				//----------------------------------- FIM -----------------------------------------------------
			}else if (replacementValue != null){
				text.setValue(replacementValue);
			}
			p++;
		}
		reviewtable.getContent().add(workingRow);
	}
public static Object criarXmlImage(File file) throws Exception {
        byte[] bytes = convertImageToByteArray(file);
 
        BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordprocessingMLPackage, bytes);  
        Relationship relImageObject = wordprocessingMLPackage.getMainDocumentPart().addTargetPart(imagePart);  
        int docPrId = 1;
        int cNvPrId = 2;
        Inline inline = imagePart.createImageInline("Filename hint", "Alternative text", docPrId, cNvPrId, false);
        
        String ml = "<w:p xmlns:ve=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" "
        		+ "xmlns:o=\"urn:schemas-microsoft-com:office:office\" "
        		+ "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" "
        		+ "xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" "
        		+ "xmlns:v=\"urn:schemas-microsoft-com:vml\" "
        		+ "xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
        		+ "xmlns:w10=\"urn:schemas-microsoft-com:office:word\" "
        		+ "xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" "
        		+ "xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\">"
        		+ "<w:r><w:pict>"
        		+ "<v:shapetype id=\"_x0000_t75\" coordsize=\"21600,21600\" o:spt=\"75\" o:preferrelative=\"t\" path=\"m@4@5l@4@11@9@11@9@5xe\" filled=\"f\" stroked=\"f\">"
        		+ "<v:stroke joinstyle=\"miter\"/>"
        		+ "<v:formulas>"
        		+ "<v:f eqn=\"if lineDrawn pixelLineWidth 0\"/>"
        		+ "<v:f eqn=\"sum @0 1 0\"/><v:f eqn=\"sum 0 0 @1\"/>"
        		+ "<v:f eqn=\"prod @2 1 2\"/>"
        		+ "<v:f eqn=\"prod @3 21600 pielWidth\"/>"
        		+ "<v:f eqn=\"prod @3 21600 pixelHeight\"/>"
        		+ "<v:f eqn=\"sum @0 0 1\"/>"
        		+ "<v:f eqn=\"prod @6 1 2\"/>"
        		+ "<v:f eqn=\"prod @7 21600 pixelWidth\"/>"
        		+ "<v:f eqn=\"sum @8 21600 0\"/>"
        		+ "<v:f eqn=\"prod @7 21600 pixelHeight\"/>"
        		+ "<v:f eqn=\"sum @10 21600 0\"/>"
        		+ "</v:formulas>"
        		+ "<v:path o:extrusionok=\"f\" gradientshapeok=\"t\" o:connecttype=\"rect\"/><o:lock v:ext=\"edit\" aspectratio=\"t\"/>"
        		+ "</v:shapetype>"
        		+ "<v:shape id=\"_x0000_i1025\" type=\"#_x0000_t75\" style=\"width:100pt;height:100pt\">"
        		+ "<v:imagedata r:id=\"${rEmbedId}\" o:title=\"\"/>"
        		+ "</v:shape></w:pict></w:r></w:p>";

        HashMap<String, String> map = new HashMap<String, String>();  
        map.put("rEmbedId", relImageObject.getId());
        XmlUtils.unmarshallFromTemplate(ml, map);
         ObjectFactory factory = new ObjectFactory();
         P p = factory.createP();
         R r = factory.createR();
         p.getContent().add(r);
         Drawing drawing = factory.createDrawing();
         r.getContent().add(drawing);
         drawing.getAnchorOrInline().add(inline);
        
        return p;
    }
private static void createPDF(File entrada, File saida) {
    	
    	InputStream inputStream = null;
    	WordprocessingMLPackage wordprocessingMLPackage = null;
    	
    	try{
    		 // 1) Le arquivo word.DOCX pelo WordprocessingMLPackage
    		inputStream = new FileInputStream(entrada);
            wordprocessingMLPackage = WordprocessingMLPackage .load(inputStream);
           
            // 2) Configurando pdf
          //  PdfSettings pdfSettings = new PdfSettings();
 
            // 3) Convert usando WordprocessingMLPackage para Pdf
            //OutputStream out = new FileOutputStream(saida);
            //PdfConversion converter = new Conversion(wordprocessingMLPackage);
            //converter.output(out, pdfSettings);
           // entrada.delete(); 
            System.out.println("Funcionando!!!");
            
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
Criado 20 de novembro de 2013
Respostas 0
Participantes 1