org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage Java Examples
The following examples show how to use
org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Docx4jTest.java From docx4j-template with Apache License 2.0 | 7 votes |
/** * 创建包含图片的内容 * * @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; }
Example #2
Source File: WatermarkPicture.java From kbase-doc with Apache License 2.0 | 6 votes |
public void addBackground(WordprocessingMLPackage wordMLPackage) throws Exception { image = this.getImage(); BinaryPartAbstractImage imagePartBG = BinaryPartAbstractImage.createImagePart(wordMLPackage, image); // wordMLPackage.getMainDocumentPart().getContents().setBackground(createBackground(imagePartBG.getRelLast().getId())); org.docx4j.wml.CTBackground background = wordMLPackage.getMainDocumentPart().getContents().getBackground(); if (background==null) { background = org.docx4j.jaxb.Context.getWmlObjectFactory().createCTBackground(); } // background.setColor("FF0000"); background.setColor("000000"); wordMLPackage.getMainDocumentPart().getContents().setBackground(background); String format = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"); File file2 = new File("E:\\ConvertTester\\docx\\docx4j_" + format + ".docx"); Docx4J.save(wordMLPackage, file2); }
Example #3
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * 插入图片 * @param wPackage * @param bm * @param file * @throws Exception */ public static void addImage(WordprocessingMLPackage wPackage, CTBookmark bm, String file) throws Exception { // 这儿可以对单个书签进行操作,也可以用一个map对所有的书签进行处理 // 获取该书签的父级段落 P p = (P) (bm.getParent()); // R对象是匿名的复杂类型,然而我并不知道具体啥意思,估计这个要好好去看看ooxml才知道 R run = factory.createR(); // 读入图片并转化为字节数组,因为docx4j只能字节数组的方式插入图片 byte[] bytes = null;//FileUtil.getByteFormBase64DataByImage(file); // InputStream is = new FileInputStream; // byte[] bytes = IOUtils.toByteArray(inputStream); // byte[] bytes = FileUtil.getByteFormBase64DataByImage(""); // 开始创建一个行内图片 BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wPackage, bytes); // createImageInline函数的前四个参数我都没有找到具体啥意思,,,, // 最有一个是限制图片的宽度,缩放的依据 Inline inline = imagePart.createImageInline(null, null, 0, 1, false, 0); // 获取该书签的父级段落 // drawing理解为画布? Drawing drawing = factory.createDrawing(); drawing.getAnchorOrInline().add(inline); run.getContent().add(drawing); p.getContent().add(run); }
Example #4
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 添加图片到段落 */ public static 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); }
Example #5
Source File: Docx4j_工具类_S3_Test.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @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); }
Example #6
Source File: Docx4j_SaveDocxImg_S3_Test.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @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(); } } }
Example #7
Source File: AbstractInliner.java From yarg with Apache License 2.0 | 6 votes |
@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); } }
Example #8
Source File: Docx4j_创建表格_S5_Test.java From docx4j-template with Apache License 2.0 | 6 votes |
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; }
Example #9
Source File: WatermarkExcelPicture.java From kbase-doc with Apache License 2.0 | 5 votes |
/** * 使用水印图片作为excel背景,达到水印效果<但打印时不会生效> * @param pkg * @param worksheet * @throws Exception * @throws IOException */ private void createBgPic(SpreadsheetMLPackage pkg, WorksheetPart worksheet) throws Exception, IOException { CTSheetBackgroundPicture ctSheetBackgroundPicture = org.xlsx4j.jaxb.Context.getsmlObjectFactory().createCTSheetBackgroundPicture(); worksheet.getContents().setPicture(ctSheetBackgroundPicture); BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(pkg, worksheet, FileUtils.readFileToByteArray(new File(outMarkPic))); Relationship sourceRelationship = imagePart.getSourceRelationships().get(0); String imageRelID = sourceRelationship.getId(); ctSheetBackgroundPicture.setId(imageRelID); }
Example #10
Source File: AbstractInliner.java From yarg with Apache License 2.0 | 5 votes |
@Override public void inlineToDocx(WordprocessingMLPackage wordPackage, Text text, Object paramValue, Matcher paramsMatcher) { try { Image image = new Image(paramValue, paramsMatcher); if (image.isValid()) { Part part = resolveTextPartForDOCX(text, wordPackage); BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, part, image.imageContent); int originalWidth = imagePart.getImageInfo().getSize().getWidthPx(); int originalHeight = imagePart.getImageInfo().getSize().getHeightPx(); double widthScale = (double) image.width / (double) originalWidth; double heightScale = (double) image.height / (double) originalHeight; double actualScale = Math.min(widthScale, heightScale); long targetWidth = Math.round(originalWidth * actualScale); long targetHeight = Math.round(originalHeight * actualScale); Inline inline = imagePart.createImageInline("", "", docxUniqueId1++, docxUniqueId2++, XlsxUtils.convertPxToEmu(targetWidth), XlsxUtils.convertPxToEmu(targetHeight), false); 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); } }
Example #11
Source File: AbstractInliner.java From yarg with Apache License 2.0 | 5 votes |
private void putImage(WorksheetPart worksheetPart, SpreadsheetMLPackage pkg, BinaryPartAbstractImage imagePart, CTOneCellAnchor anchor) throws Docx4JException { PartName drawingPart = new PartName(StringUtils.replaceIgnoreCase(worksheetPart.getPartName().getName(), "worksheets/sheet", "drawings/drawing")); String imagePartName = imagePart.getPartName().getName(); Part part = pkg.getParts().get(drawingPart); if (part != null && !(part instanceof Drawing)) throw new ReportFormattingException("Wrong Class: not a Drawing"); Drawing drawing = (Drawing) part; int currentId = 0; if (drawing == null) { drawing = new Drawing(drawingPart); drawing.setContents(new org.docx4j.dml.spreadsheetdrawing.CTDrawing()); Relationship relationship = worksheetPart.addTargetPart(drawing); org.xlsx4j.sml.CTDrawing smlDrawing = new org.xlsx4j.sml.CTDrawing(); smlDrawing.setId(relationship.getId()); smlDrawing.setParent(worksheetPart.getContents()); worksheetPart.getContents().setDrawing(smlDrawing); } else { currentId = drawing.getContents().getEGAnchor().size(); } CTPicture picture = new CTPicture(); CTBlipFillProperties blipFillProperties = new CTBlipFillProperties(); blipFillProperties.setStretch(new CTStretchInfoProperties()); blipFillProperties.getStretch().setFillRect(new CTRelativeRect()); blipFillProperties.setBlip(new CTBlip()); blipFillProperties.getBlip().setEmbed("rId" + (currentId + 1)); blipFillProperties.getBlip().setCstate(STBlipCompression.PRINT); picture.setBlipFill(blipFillProperties); CTNonVisualDrawingProps nonVisualDrawingProps = new CTNonVisualDrawingProps(); nonVisualDrawingProps.setId(currentId + 2); nonVisualDrawingProps.setName(imagePartName.substring(imagePartName.lastIndexOf("/") + 1)); nonVisualDrawingProps.setDescr(nonVisualDrawingProps.getName()); CTNonVisualPictureProperties nonVisualPictureProperties = new CTNonVisualPictureProperties(); nonVisualPictureProperties.setPicLocks(new CTPictureLocking()); nonVisualPictureProperties.getPicLocks().setNoChangeAspect(true); CTPictureNonVisual nonVisualPicture = new CTPictureNonVisual(); nonVisualPicture.setCNvPr(nonVisualDrawingProps); nonVisualPicture.setCNvPicPr(nonVisualPictureProperties); picture.setNvPicPr(nonVisualPicture); CTShapeProperties shapeProperties = new CTShapeProperties(); CTTransform2D transform2D = new CTTransform2D(); transform2D.setOff(new CTPoint2D()); transform2D.setExt(new CTPositiveSize2D()); shapeProperties.setXfrm(transform2D); shapeProperties.setPrstGeom(new CTPresetGeometry2D()); shapeProperties.getPrstGeom().setPrst(STShapeType.RECT); shapeProperties.getPrstGeom().setAvLst(new CTGeomGuideList()); picture.setSpPr(shapeProperties); anchor.setPic(picture); anchor.setClientData(new CTAnchorClientData()); drawing.getContents().getEGAnchor().add(anchor); Relationship rel = new Relationship(); rel.setId("rId" + (currentId + 1)); rel.setType(Namespaces.IMAGE); rel.setTarget(imagePartName); drawing.getRelationshipsPart().addRelationship(rel); RelationshipsPart relPart = drawing.getRelationshipsPart(); pkg.getParts().remove(relPart.getPartName()); pkg.getParts().put(relPart); pkg.getParts().remove(drawing.getPartName()); pkg.getParts().put(drawing); }
Example #12
Source File: ImageResolver.java From docx-stamper with MIT License | 5 votes |
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; }
Example #13
Source File: Docx4J_简单例子.java From docx4j-template with Apache License 2.0 | 5 votes |
public P newImage(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, Part sourcePart, byte[] bytes, String filenameHint, String altText, int id1, int id2, JcEnumeration jcEnumeration) throws Exception { BinaryPartAbstractImage imagePart = BinaryPartAbstractImage .createImagePart(wordMLPackage, sourcePart, bytes); Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false); P p = factory.createP(); R run = factory.createR(); p.getContent().add(run); Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); PPr pPr = p.getPPr(); if (pPr == null) { pPr = factory.createPPr(); } Jc jc = pPr.getJc(); if (jc == null) { jc = new Jc(); } jc.setVal(jcEnumeration); pPr.setJc(jc); p.setPPr(pPr); return p; }
Example #14
Source File: AddingAnInlineImageToTable.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * 使用给定的文件创建一个内联图片. * 跟前面例子中一样, 我们将文件转换成字节数组, 并用它创建一个内联图片. * * @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); }
Example #15
Source File: AddingAnInlineImage.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * 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); }
Example #16
Source File: Docx4J_简单例子2.java From docx4j-template with Apache License 2.0 | 5 votes |
public P newImage(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, Part sourcePart, byte[] bytes, String filenameHint, String altText, int id1, int id2, JcEnumeration jcEnumeration) throws Exception { BinaryPartAbstractImage imagePart = BinaryPartAbstractImage .createImagePart(wordMLPackage, sourcePart, bytes); Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false); P p = factory.createP(); R run = factory.createR(); p.getContent().add(run); Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); PPr pPr = p.getPPr(); if (pPr == null) { pPr = factory.createPPr(); } Jc jc = pPr.getJc(); if (jc == null) { jc = new Jc(); } jc.setVal(jcEnumeration); pPr.setJc(jc); p.setPPr(pPr); PBdr pBdr=pPr.getPBdr(); if(pBdr==null){ pBdr=factory.createPPrBasePBdr(); } CTBorder value=new CTBorder(); value.setVal(STBorder.SINGLE); value.setColor("000000"); value.setSpace(new BigInteger("0")); value.setSz(new BigInteger("3")); pBdr.setBetween(value); pPr.setPBdr(pBdr); setParagraphSpacing(factory, p, jcEnumeration, "0", "0"); return p; }
Example #17
Source File: Docx4J_简单例子2.java From docx4j-template with Apache License 2.0 | 5 votes |
public P createImageParagraph(WordprocessingMLPackage wordMLPackage, ObjectFactory factory,P p,String fileName, String content,byte[] bytes, JcEnumeration jcEnumeration) throws Exception { BinaryPartAbstractImage imagePart = BinaryPartAbstractImage .createImagePart(wordMLPackage, bytes); Inline inline = imagePart.createImageInline(fileName, "这是图片", 1, 2, false); Text text = factory.createText(); text.setValue(content); text.setSpace("preserve"); R run = factory.createR(); p.getContent().add(run); run.getContent().add(text); Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); PPr pPr = p.getPPr(); if (pPr == null) { pPr = factory.createPPr(); } Jc jc = pPr.getJc(); if (jc == null) { jc = new Jc(); } jc.setVal(jcEnumeration); pPr.setJc(jc); p.setPPr(pPr); setParagraphSpacing(factory, p, jcEnumeration, "0", "0"); return p; }
Example #18
Source File: Docx4J_例子2.java From docx4j-template with Apache License 2.0 | 5 votes |
public P createImageParagraph(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, P p, String fileName, String content, byte[] bytes, JcEnumeration jcEnumeration) throws Exception { BinaryPartAbstractImage imagePart = BinaryPartAbstractImage .createImagePart(wordMLPackage, bytes); Inline inline = imagePart.createImageInline(fileName, "这是图片", 1, 2, false); Text text = factory.createText(); text.setValue(content); text.setSpace("preserve"); R run = factory.createR(); p.getContent().add(run); run.getContent().add(text); Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); PPr pPr = p.getPPr(); if (pPr == null) { pPr = factory.createPPr(); } Jc jc = pPr.getJc(); if (jc == null) { jc = new Jc(); } jc.setVal(jcEnumeration); pPr.setJc(jc); p.setPPr(pPr); setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO); return p; }
Example #19
Source File: WordprocessingMLPackageRender.java From docx4j-template with Apache License 2.0 | 5 votes |
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); }
Example #20
Source File: DocxProducer.java From OfficeProducer with Apache License 2.0 | 4 votes |
/** * 替换书签为图片 * * @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); } } }
Example #21
Source File: Conversion.java From docx4j-export-FO with Apache License 2.0 | 4 votes |
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() ); } } }
Example #22
Source File: Docx4J_例子2.java From docx4j-template with Apache License 2.0 | 4 votes |
public P newImage(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, Part sourcePart, byte[] bytes, String filenameHint, String altText, int id1, int id2, boolean isUnderLine, String underLineSize, JcEnumeration jcEnumeration) throws Exception { BinaryPartAbstractImage imagePart = BinaryPartAbstractImage .createImagePart(wordMLPackage, sourcePart, bytes); Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false); P p = factory.createP(); R run = factory.createR(); p.getContent().add(run); Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); PPr pPr = p.getPPr(); if (pPr == null) { pPr = factory.createPPr(); } Jc jc = pPr.getJc(); if (jc == null) { jc = new Jc(); } jc.setVal(jcEnumeration); pPr.setJc(jc); p.setPPr(pPr); if (isUnderLine) { PBdr pBdr = pPr.getPBdr(); if (pBdr == null) { pBdr = factory.createPPrBasePBdr(); } CTBorder value = new CTBorder(); value.setVal(STBorder.SINGLE); value.setColor("000000"); value.setSpace(new BigInteger("0")); value.setSz(new BigInteger(underLineSize)); pBdr.setBetween(value); pPr.setPBdr(pBdr); } setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO); return p; }
Example #23
Source File: BackgroundImage.java From kbase-doc with Apache License 2.0 | 4 votes |
public void addBackground() throws Exception { image = this.getImage(); System.out.println("ekozhan" + image.length); wordMLPackage = WordprocessingMLPackage.createPackage(); BinaryPartAbstractImage imagePartBG = BinaryPartAbstractImage.createImagePart(wordMLPackage, image); wordMLPackage.getMainDocumentPart().getJaxbElement().setBackground( createBackground( imagePartBG.getRelLast().getId())); wordMLPackage.getMainDocumentPart().addParagraphOfText("Ekozhan"); File f = new File(DOCX_OUT); wordMLPackage.save(f); }
Example #24
Source File: Docx4jExample.java From tutorials with MIT License | 4 votes |
void createDocumentPackage(String outputPath, String imagePath) throws Exception { WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage(); MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart(); mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!"); mainDocumentPart.addParagraphOfText("Welcome To Baeldung!"); ObjectFactory factory = Context.getWmlObjectFactory(); P p = factory.createP(); R r = factory.createR(); Text t = factory.createText(); t.setValue("Welcome To Baeldung"); r.getContent().add(t); p.getContent().add(r); RPr rpr = factory.createRPr(); BooleanDefaultTrue b = new BooleanDefaultTrue(); rpr.setB(b); rpr.setI(b); rpr.setCaps(b); Color red = factory.createColor(); red.setVal("green"); rpr.setColor(red); r.setRPr(rpr); mainDocumentPart.getContent().add(p); File image = new File(imagePath); byte[] fileContent = Files.readAllBytes(image.toPath()); BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, fileContent); Inline inline = imagePart.createImageInline("Baeldung Image", "Alt Text", 1, 2, false); P Imageparagraph = addImageToParagraph(inline); mainDocumentPart.getContent().add(Imageparagraph); int writableWidthTwips = wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips(); int columnNumber = 3; Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips / columnNumber); List<Object> rows = tbl.getContent(); for (Object row : rows) { Tr tr = (Tr) row; List<Object> cells = tr.getContent(); for (Object cell : cells) { Tc td = (Tc) cell; td.getContent().add(p); } } mainDocumentPart.getContent().add(tbl); File exportFile = new File(outputPath); wordPackage.save(exportFile); }