• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java BinaryPartAbstractImage类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage的典型用法代码示例。如果您正苦于以下问题:Java BinaryPartAbstractImage类的具体用法?Java BinaryPartAbstractImage怎么用?Java BinaryPartAbstractImage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



BinaryPartAbstractImage类属于org.docx4j.openpackaging.parts.WordprocessingML包,在下文中一共展示了BinaryPartAbstractImage类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: writeImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
@Override
public void writeImage(final BufferedImage image) {
	final P imageParagraph = this.wmlObjectFactory.createP();

	final R imageRun = this.wmlObjectFactory.createR();
	imageParagraph.getContent().add(imageRun);

	final Drawing drawing = this.wmlObjectFactory.createDrawing();
	imageRun.getContent().add(drawing);

	try {
		final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
		ImageIO.write(image, "PNG", byteOutputStream);
		final BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(this.extractedDocument.getDocumentPackage(),
				byteOutputStream.toByteArray());
		final Inline imageInline = imagePart.createImageInline("Filename", "An image", nextUniqueImageId(), nextUniqueImageId(), false);

		drawing.getAnchorOrInline().add(imageInline);
	} catch (final Exception e) {
		throw new RuntimeException(e);
	}

	addParagraph(imageParagraph);
}
 
开发者ID:mizitch,项目名称:story-inspector,代码行数:25,代码来源:DocXReportSummaryWriter.java


示例2: saveDocxImg

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
/** 
 * @Description: 提取word图片 
 */  
public void saveDocxImg(String filePath, String savePath) throws Exception {  
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage  
            .load(new File(filePath));  
    for (Entry<PartName, Part> entry : wordMLPackage.getParts().getParts()  
            .entrySet()) {  
        if (entry.getValue() instanceof BinaryPartAbstractImage) {  
            BinaryPartAbstractImage binImg = (BinaryPartAbstractImage) entry  
                    .getValue();  
            // 图片minetype  
            String imgContentType = binImg.getContentType();  
            PartName pt = binImg.getPartName();  
            String fileName = null;  
            if (pt.getName().indexOf("word/media/") != -1) {  
                fileName = pt.getName().substring(  
                        pt.getName().indexOf("word/media/")  
                                + "word/media/".length());  
            }  
            System.out.println(String.format("mimetype=%s,filePath=%s",  
                    imgContentType, pt.getName()));  
            FileOutputStream fos = new FileOutputStream(savePath + fileName);  
            ((BinaryPart) entry.getValue()).writeDataToOutputStream(fos);  
            fos.close();  
        }  
    }  
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:29,代码来源:Docx4j_SaveDocxImg_S3_Test.java


示例3: newImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
/**
 * 创建包含图片的内容
 *
 * @param word
 * @param sourcePart
 * @param imageFilePath
 * @return
 * @throws Exception
 */
public static P newImage(WordprocessingMLPackage word,
                         Part sourcePart,
                         String imageFilePath) throws Exception {
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
            .createImagePart(word, sourcePart, new File(imageFilePath));
    //随机数ID
    int id = (int) (Math.random() * 10000);
    //这里的id不重复即可
    Inline inline = imagePart.createImageInline("image", "image", id, id * 2, false);

    Drawing drawing = factory.createDrawing();
    drawing.getAnchorOrInline().add(inline);

    R r = factory.createR();
    r.getContent().add(drawing);

    P p = factory.createP();
    p.getContent().add(r);

    return p;
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:31,代码来源:Docx4jTest.java


示例4: newImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
public P newImage(WordprocessingMLPackage wordMLPackage,  
        ObjectFactory factory, byte[] bytes, String filenameHint,  
        String altText, int id1, int id2, long cx) throws Exception {  
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage  
            .createImagePart(wordMLPackage, bytes);  
    Inline inline = imagePart.createImageInline(filenameHint, altText, id1,  
            id2, cx, false);  
    // Now add the inline in w:p/w:r/w:drawing  
    P p = factory.createP();  
    R run = factory.createR();  
    p.getContent().add(run);  
    Drawing drawing = factory.createDrawing();  
    run.getContent().add(drawing);  
    drawing.getAnchorOrInline().add(inline);  
    return p;  
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:17,代码来源:Docx4j_创建表格_S5_Test.java


示例5: newImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
/**
 * Create image, without specifying width
 * 
 * @param wordMLPackage
 *            : a reference to the docx file
 * @param bytes
 *            : data of the image
 * @param filenameHint
 *            : file name or hint
 * @param altText
 *            : alternative text
 * @return the created paragraph that contains image
 * @throws Exception
 */
private static org.docx4j.wml.P newImage(WordprocessingMLPackage wordMLPackage, byte[] bytes, String filenameHint,
		String altText) throws Exception {

	BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);

	Inline inline = imagePart.createImageInline(filenameHint, altText, id1++, id2++, false);

	// Now add the inline in w:p/w:r/w:drawing
	org.docx4j.wml.P p = wmlObjectFactory.createP();
	org.docx4j.wml.R run = wmlObjectFactory.createR();
	p.getContent().add(run);
	org.docx4j.wml.Drawing drawing = wmlObjectFactory.createDrawing();
	run.getContent().add(drawing);
	drawing.getAnchorOrInline().add(inline);
	return p;

}
 
开发者ID:mjza,项目名称:MSThesis_Fidus_Docx_Converter,代码行数:32,代码来源:ImageUtil.java


示例6: newImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
/**
 * Create image, without specifying width
 */
private static org.docx4j.wml.P newImage(WordprocessingMLPackage wordMLPackage,
		byte[] bytes,
		String filenameHint, String altText, 
		int id1, int id2) throws Exception {
       BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
       Inline inline = imagePart.createImageInline( filenameHint, altText, id1, id2, false);
       // Now add the inline in w:p/w:r/w:drawing
	org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
	org.docx4j.wml.P  p = factory.createP();
	org.docx4j.wml.R  run = factory.createR();		
	p.getContent().add(run);        
	org.docx4j.wml.Drawing drawing = factory.createDrawing();		
	run.getContent().add(drawing);		
	drawing.getAnchorOrInline().add(inline);
	return p;
}
 
开发者ID:trackplus,项目名称:Genji,代码行数:20,代码来源:ImageUtil.java


示例7: newImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
public static org.docx4j.wml.P newImage( WordprocessingMLPackage wordMLPackage,
		Part sourcePart,
		byte[] bytes,
		String filenameHint, String altText, 
		int id1, int id2) throws Exception {
	
       BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, 
       		sourcePart, bytes);
	
       Inline inline = imagePart.createImageInline( filenameHint, altText, 
   			id1, id2, false);
       
       // Now add the inline in w:p/w:r/w:drawing
	org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
	org.docx4j.wml.P  p = factory.createP();
	org.docx4j.wml.R  run = factory.createR();		
	p.getContent().add(run);        
	org.docx4j.wml.Drawing drawing = factory.createDrawing();		
	run.getContent().add(drawing);		
	drawing.getAnchorOrInline().add(inline);
	
	return p;
}
 
开发者ID:asposemarketplace,项目名称:Aspose_Java_for_Docx4j,代码行数:24,代码来源:Docx4jFooterCreate.java


示例8: newImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
/**
 * Create image, without specifying width
 */
public static org.docx4j.wml.P newImage( WordprocessingMLPackage wordMLPackage,
		byte[] bytes,
		String filenameHint, String altText, 
		int id1, int id2) throws Exception {
	
       BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
	
       Inline inline = imagePart.createImageInline( filenameHint, altText, 
   			id1, id2, false);
       
       // Now add the inline in w:p/w:r/w:drawing
	org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
	org.docx4j.wml.P  p = factory.createP();
	org.docx4j.wml.R  run = factory.createR();		
	p.getContent().add(run);        
	org.docx4j.wml.Drawing drawing = factory.createDrawing();		
	run.getContent().add(drawing);		
	drawing.getAnchorOrInline().add(inline);
	
	return p;
	
}
 
开发者ID:asposemarketplace,项目名称:Aspose_Java_for_Docx4j,代码行数:26,代码来源:Docx4jImageAdd.java


示例9: inlineToXlsx

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
@Override
public void inlineToXlsx(SpreadsheetMLPackage pkg, WorksheetPart worksheetPart, Cell newCell, Object paramValue, Matcher matcher) {
    try {
        Image image = new Image(paramValue, matcher);
        if (image.isValid()) {
            BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(pkg, worksheetPart, image.imageContent);
            CTOneCellAnchor anchor = new CTOneCellAnchor();
            anchor.setFrom(new CTMarker());
            CellReference cellReference = new CellReference("", newCell.getR());
            anchor.getFrom().setCol(cellReference.getColumn() - 1);
            anchor.getFrom().setRow(cellReference.getRow() - 1);
            anchor.setExt(new CTPositiveSize2D());
            anchor.getExt().setCx(XlsxUtils.convertPxToEmu(image.width));
            anchor.getExt().setCy(XlsxUtils.convertPxToEmu(image.height));
            newCell.setV(null);
            putImage(worksheetPart, pkg, imagePart, anchor);
        }
    } catch (Exception e) {
        throw new ReportFormattingException("An error occurred while inserting bitmap to xlsx file", e);
    }
}
 
开发者ID:cuba-platform,项目名称:yarg,代码行数:22,代码来源:AbstractInliner.java


示例10: inlineToDocx

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
@Override
public void inlineToDocx(WordprocessingMLPackage wordPackage, Text text, Object paramValue, Matcher paramsMatcher) {
    try {
        Image image = new Image(paramValue, paramsMatcher);
        if (image.isValid()) {
            BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, resolveTextPartForDOCX(text, wordPackage),
                    image.imageContent);
            Inline inline = imagePart.createImageInline("", "", docxUniqueId1++, docxUniqueId2++, false);
            ImageSize oldSize = imagePart.getImageInfo().getSize();
            double widthExtent = (double) image.width / oldSize.getWidthPx();
            double heightExtent = (double) image.height / oldSize.getHeightPx();
            inline.getExtent().setCx((long) (inline.getExtent().getCx() * widthExtent));
            inline.getExtent().setCy((long) (inline.getExtent().getCy() * heightExtent));
            org.docx4j.wml.Drawing drawing = new org.docx4j.wml.ObjectFactory().createDrawing();
            R run = (R) text.getParent();
            run.getContent().add(drawing);
            drawing.getAnchorOrInline().add(inline);
            text.setValue("");
        }
    } catch (Exception e) {
        throw new ReportFormattingException("An error occurred while inserting bitmap to docx file", e);
    }
}
 
开发者ID:cuba-platform,项目名称:yarg,代码行数:24,代码来源:AbstractInliner.java


示例11: addImageToPackage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
/** 
 *  Docx4j拥有一个由字节数组创建图片部件的工具方法, 随后将其添加到给定的包中. 为了能将图片添加 
 *  到一个段落中, 我们需要将图片转换成内联对象. 这也有一个方法, 方法需要文件名提示, 替换文本,  
 *  两个id标识符和一个是嵌入还是链接到的指示作为参数. 
 *  一个id用于文档中绘图对象不可见的属性, 另一个id用于图片本身不可见的绘制属性. 最后我们将内联 
 *  对象添加到段落中并将段落添加到包的主文档部件. 
 * 
 *  @param wordMLPackage 要添加图片的包 
 *  @param bytes         图片对应的字节数组 
 *  @throws Exception    不幸的createImageInline方法抛出一个异常(没有更多具体的异常类型) 
 */  
public void addImageToPackage(byte[] bytes) throws Exception {  
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wmlPackage, bytes);  
   
    int docPrId = 1;  
    int cNvPrId = 2;  
    Inline inline = imagePart.createImageInline("Filename hint", "Alternative text", docPrId, cNvPrId, false);  
   
    P paragraph = WmlElementUtils.addInlineImageToParagraph(inline);  
   
    wmlPackage.getMainDocumentPart().addObject(paragraph);  
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:23,代码来源:WordprocessingMLPackageRender.java


示例12: addImageToPara

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
/**
 * @Description: 添加图片到段落
 */
public void addImageToPara(WordprocessingMLPackage wordMLPackage,
        ObjectFactory factory, P paragraph, String filePath,
        String content, RPr rpr, String altText, int id1, int id2)
        throws Exception {
    R run = factory.createR();
    if (content != null) {
        Text text = factory.createText();
        text.setValue(content);
        text.setSpace("preserve");
        run.setRPr(rpr);
        run.getContent().add(text);
    }

    InputStream is = new FileInputStream(filePath);
    byte[] bytes = IOUtils.toByteArray(is);
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
            .createImagePart(wordMLPackage, bytes);
    Inline inline = imagePart.createImageInline(filePath, altText, id1,
            id2, false);
    Drawing drawing = factory.createDrawing();
    drawing.getAnchorOrInline().add(inline);
    run.getContent().add(drawing);
    paragraph.getContent().add(run);
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:28,代码来源:Docx4j_工具类_S3_Test.java


示例13: addImageToPackage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
/**
 *  Docx4j拥有一个由字节数组创建图片部件的工具方法, 随后将其添加到给定的包中. 为了能将图片添加
 *  到一个段落中, 我们需要将图片转换成内联对象. 这也有一个方法, 方法需要文件名提示, 替换文本, 
 *  两个id标识符和一个是嵌入还是链接到的指示作为参数.
 *  一个id用于文档中绘图对象不可见的属性, 另一个id用于图片本身不可见的绘制属性. 最后我们将内联
 *  对象添加到段落中并将段落添加到包的主文档部件.
 *
 *  @param wordMLPackage 要添加图片的包
 *  @param bytes         图片对应的字节数组
 *  @throws Exception    不幸的createImageInline方法抛出一个异常(没有更多具体的异常类型)
 */
private static void addImageToPackage(WordprocessingMLPackage wordMLPackage,
                        byte[] bytes) throws Exception {
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
 
    int docPrId = 1;
    int cNvPrId = 2;
    Inline inline = imagePart.createImageInline("Filename hint","Alternative text", docPrId, cNvPrId, false);
 
    P paragraph = addInlineImageToParagraph(inline);
 
    wordMLPackage.getMainDocumentPart().addObject(paragraph);
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:24,代码来源:AddingAnInlineImage.java


示例14: createInlineImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
/**
 * 使用给定的文件创建一个内联图片.
 * 跟前面例子中一样, 我们将文件转换成字节数组, 并用它创建一个内联图片.
 *
 * @param file
 * @return
 * @throws Exception
 */
private static Inline createInlineImage(File file) throws Exception {
    byte[] bytes = convertImageToByteArray(file);
 
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
 
    int docPrId = 1;
    int cNvPrId = 2;
 
    return imagePart.createImageInline("Filename hint", "Alternative text", docPrId, cNvPrId, false);
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:19,代码来源:AddingAnInlineImageToTable.java


示例15: createRunWithImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
public static R createRunWithImage(WordprocessingMLPackage wordMLPackage, byte[] bytes, String filenameHint, String altText, Integer maxWidth) throws Exception {
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);

    // creating random ids assuming they are unique
    // id must not be too large, otherwise Word cannot open the document
    int id1 = random.nextInt(100000);
    int id2 = random.nextInt(100000);
    if (filenameHint == null) {
        filenameHint = "dummyFileName";
    }
    if (altText == null) {
        altText = "dummyAltText";
    }

    Inline inline;
    if (maxWidth == null) {
        inline = imagePart.createImageInline(filenameHint, altText,
                id1, id2, false);
    } else {
        inline = imagePart.createImageInline(filenameHint, altText,
                id1, id2, false, maxWidth);
    }

    // Now add the inline in w:p/w:r/w:drawing
    org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
    org.docx4j.wml.R run = factory.createR();
    org.docx4j.wml.Drawing drawing = factory.createDrawing();
    run.getContent().add(drawing);
    drawing.getAnchorOrInline().add(inline);

    return run;

}
 
开发者ID:thombergs,项目名称:docx-stamper,代码行数:34,代码来源:ImageResolver.java


示例16: main

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
		
		String outputfilepath = System.getProperty("user.dir") + "/data/xlsx4j/AddImage-Xlsx4j.xlsx";
		String imagefilePath  = System.getProperty("user.dir") + "/data/xlsx4j/greentick.png" ;
		
		SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
		WorksheetPart worksheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);

		// Create Drawing part and add to sheet 
		Drawing drawingPart = new Drawing();
		Relationship drawingRel = worksheet.addTargetPart(drawingPart);

		// Add anchor XML to worksheet
		org.xlsx4j.sml.CTDrawing drawing = org.xlsx4j.jaxb.Context.getsmlObjectFactory().createCTDrawing(); 
		    worksheet.getJaxbElement().setDrawing(drawing); 
		        drawing.setId( drawingRel.getId() ); 		
		
		// Create image part and add to Drawing part
        BinaryPartAbstractImage imagePart 
        	= BinaryPartAbstractImage.createImagePart(pkg, drawingPart, 
        			FileUtils.readFileToByteArray(new File(imagefilePath) ));
        String imageRelID = imagePart.getSourceRelationship().getId();
		
		// Create and set drawing part content
        // Take your pick ..
        // .. build it using code
//        drawingPart.setJaxbElement(
//        		buildDrawingPartContentUsingCode(imageRelID));
        // .. or build it from an XML string
        drawingPart.setJaxbElement(
        		buildDrawingPartContentFromXmlString(imageRelID));
		
		
        // Save the xlsx
		SaveToZipFile saver = new SaveToZipFile(pkg);
		saver.save(outputfilepath);
		System.out.println("\n\n done .. " + outputfilepath);	
	}
 
开发者ID:asposemarketplace,项目名称:Aspose_Java_for_Docx4j,代码行数:39,代码来源:AddImageXlsx4j.java


示例17: newImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
public P newImage(WordprocessingMLPackage wordMLPackage, byte[] bytes, String filenameHint, String altText, int id1, int id2, long cx) throws Exception {
	BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
	Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, false);
	// Now add the inline in w:p/w:r/w:drawing
	ObjectFactory factory = Context.getWmlObjectFactory();
	P p = createParagraph();	
	R run = factory.createR();
	p.getContent().add(run);
	Drawing drawing = factory.createDrawing();
	run.getContent().add(drawing);
	drawing.getAnchorOrInline().add(inline);
	return p;
}
 
开发者ID:nextreports,项目名称:nextreports-engine,代码行数:14,代码来源:DocxExporter.java


示例18: addBackgroundImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
private void addBackgroundImage() throws Exception {
	String image = bean.getReportLayout().getBackgroundImage();
	if (image != null) {						
		byte[] imageBytes = getImage(image);			
		MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
		BinaryPartAbstractImage imagePartBG = BinaryPartAbstractImage.createImagePart(wordMLPackage, imageBytes);      		            
           mdp.getContents().setBackground(createBackground(imagePartBG.getRelLast().getId())); 
	}
}
 
开发者ID:nextreports,项目名称:nextreports-engine,代码行数:10,代码来源:DocxExporter.java


示例19: replaceBookMarkWithImage

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
/**
 * 替换书签为图片
 *
 * @param wordMLPackage
 * @param documentPart
 * @param imageParameters
 * @throws Exception
 */
private static void replaceBookMarkWithImage(WordprocessingMLPackage wordMLPackage,
                                             MainDocumentPart documentPart,
                                             Map<String, String> imageParameters)
        throws Exception {
    Document wmlDoc = documentPart.getContents();
    Body body = wmlDoc.getBody();
    // 提取正文中所有段落
    List<Object> paragraphs = body.getContent();
    // 提取书签并创建书签的游标
    RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
    new TraversalUtil(paragraphs, rt);

    // 遍历书签
    for (CTBookmark bm : rt.getStarts()) {
        String bookmarkName = bm.getName();
        String imagePath = imageParameters.get(bookmarkName);
        if (imagePath != null) {
            File imageFile = new File(imagePath);
            InputStream imageStream = new FileInputStream(imageFile);
            // 读入图片并转化为字节数组,因为docx4j只能字节数组的方式插入图片
            byte[] bytes = IOUtils.toByteArray(imageStream);
            // 创建一个行内图片
            BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
            // createImageInline函数的前四个参数我都没有找到具体啥意思
            // 最后一个是限制图片的宽度,缩放的依据
            Inline inline = imagePart.createImageInline(null, null, 0, 1, false, 800);
            // 获取该书签的父级段落
            P p = (P) (bm.getParent());
            ObjectFactory factory = new ObjectFactory();
            // 新建一个Run
            R run = factory.createR();
            // drawing 画布
            Drawing drawing = factory.createDrawing();
            drawing.getAnchorOrInline()
                    .add(inline);
            run.getContent()
                    .add(drawing);
            p.getContent()
                    .add(run);
        }
    }
}
 
开发者ID:izhangzhihao,项目名称:OfficeProducer,代码行数:51,代码来源:DocxProducer.java


示例20: addDrawing

import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; //导入依赖的package包/类
void addDrawing(org.docx4j.wml.Drawing o,
			Object paraParent, Paragraph pdfParagraph) throws Exception {
	
		org.docx4j.wml.Drawing drawing = (org.docx4j.wml.Drawing) o;
		List<Object> list = drawing.getAnchorOrInline();
		if (list.size() != 1
			|| !(list.get(0) instanceof Inline)) {
			//There should not be an Anchor in 'list'
			//because it is not being supported and 
			//RunML.initChildren() prevents it from
			//being assigned to this InlineDrawingML object.
			//See: RunML.initChildren().
			throw new IllegalArgumentException("Unsupported Docx Object = " + o);			
		}
		
		Inline inline = (Inline) list.get(0);
//		if (inline.getExtent() != null) {
//			int cx = Long.valueOf(inline.getExtent().getCx()).intValue();
//			cx = StyleSheet.emuToPixels(cx);
//			int cy = Long.valueOf(inline.getExtent().getCy()).intValue();
//			cy = StyleSheet.emuToPixels(cy);
//			//this.extentInPixels = new Dimension(cx, cy);
//		}
		
		if (inline.getEffectExtent() != null) {
			//this.effectExtent = new CTEffectExtent(inline.getEffectExtent());
		}
		
		if (inline.getGraphic() != null) {
			
			byte[] imagedata = BinaryPartAbstractImage.getImage(wordMLPackage,
					inline.getGraphic() );
			Image img = Image.getInstance( imagedata );
			if (paraParent instanceof Document) {				
				((Document)paraParent).add(img);
			} else if (paraParent instanceof PdfPTable) {				
				((PdfPTable)paraParent).addCell(img);
			} else {
				log.error("Trying to add image to " + paraParent.getClass().getName() );
			}
			
		}
	}
 
开发者ID:plutext,项目名称:docx4j-export-FO,代码行数:44,代码来源:Conversion.java



注:本文中的org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java TitleBuffer类代码示例发布时间:2022-05-22
下一篇:
Java RuntimeFault类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap