Search in sources :

Example 6 with BinaryPartAbstractImage

use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project docx4j-template by vindell.

the class AddingAnInlineImage method addImageToPackage.

/**
 *  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);
}
Also used : P(org.docx4j.wml.P) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 7 with BinaryPartAbstractImage

use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project Aspose.Cells-for-Java by aspose-cells.

the class Xlsx4jAddImage method main.

public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(Xlsx4jAddImage.class);
    String outputfilepath = dataDir + "AddImage-Xlsx4j.xlsx";
    String imagefilePath = dataDir + "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);
}
Also used : CTDrawing(org.docx4j.dml.spreadsheetdrawing.CTDrawing) Drawing(org.docx4j.openpackaging.parts.DrawingML.Drawing) SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) Relationship(org.docx4j.relationships.Relationship) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) File(java.io.File)

Example 8 with BinaryPartAbstractImage

use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method newImage.

public R newImage(final DocxRendererContext docx, byte[] bytes, String filenameHint, Attributes attributes, int id1, int id2) {
    try {
        BinaryPartAbstractImage imagePart = null;
        imagePart = BinaryPartAbstractImage.createImagePart(docx.getPackage(), docx.getContainerPart(), bytes);
        Inline inline = null;
        String altText = attributes.contains("alt") ? attributes.getValue("alt") : "";
        List<SectionWrapper> sections = docx.getPackage().getDocumentModel().getSections();
        PageDimensions page = sections.get(sections.size() - 1).getPageDimensions();
        double writableWidthTwips = page.getWritableWidthTwips();
        long cx = getSizeInfo(attributes, "width", page.getWritableWidthTwips());
        long cy = cx > 0 ? getSizeInfo(attributes, "height", page.getWritableHeightTwips()) : -1;
        // kludge: normally there is no max-width attribute but we can fake it
        long longMaxWidth = getSizeInfo(attributes, "max-width", page.getWritableWidthTwips());
        int maxWidth = longMaxWidth > 0 && longMaxWidth <= Integer.MAX_VALUE ? (int) longMaxWidth : -1;
        if (cx > 0 && cy > 0) {
            // here we need cx & cy in emu which needs conversion from twips
            cx = UnitsOfMeasurement.twipToEMU(cx);
            cy = UnitsOfMeasurement.twipToEMU(cy);
            inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, cy, false);
        } else {
            if (cx > 0) {
                inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, false);
            } else {
                if (maxWidth > 0) {
                    inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false, maxWidth);
                } else {
                    inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false);
                }
            }
        }
        // Now add the inline in w:p/w:r/w:drawing
        org.docx4j.wml.R run = docx.createR();
        org.docx4j.wml.Drawing drawing = docx.getFactory().createDrawing();
        run.getContent().add(drawing);
        drawing.getAnchorOrInline().add(inline);
        return run;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SectionWrapper(org.docx4j.model.structure.SectionWrapper) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) URISyntaxException(java.net.URISyntaxException) TocException(org.docx4j.toc.TocException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) PageDimensions(org.docx4j.model.structure.PageDimensions) org.docx4j.wml(org.docx4j.wml) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 9 with BinaryPartAbstractImage

use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project mdw-designer by CenturyLinkCloud.

the class DocxBuilder method addImage.

public void addImage(byte[] imageBytes) throws Exception {
    BinaryPartAbstractImage.setDensity(600);
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, imageBytes);
    Inline inline = imagePart.createImageInline(null, null, 0, 1, false);
    ObjectFactory factory = Context.getWmlObjectFactory();
    P imgP = factory.createP();
    R imgRun = factory.createR();
    imgP.getContent().add(imgRun);
    Drawing drawing = factory.createDrawing();
    imgRun.getContent().add(drawing);
    drawing.getAnchorOrInline().add(inline);
    getMdp().addObject(imgP);
}
Also used : P(org.docx4j.wml.P) Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) ObjectFactory(org.docx4j.wml.ObjectFactory) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 10 with BinaryPartAbstractImage

use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project tutorials by eugenp.

the class Docx4jExample method createDocumentPackage.

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);
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Color(org.docx4j.wml.Color) Text(org.docx4j.wml.Text) BooleanDefaultTrue(org.docx4j.wml.BooleanDefaultTrue) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Tc(org.docx4j.wml.Tc) P(org.docx4j.wml.P) R(org.docx4j.wml.R) ObjectFactory(org.docx4j.wml.ObjectFactory) RPr(org.docx4j.wml.RPr) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) File(java.io.File) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Aggregations

BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)19 Inline (org.docx4j.dml.wordprocessingDrawing.Inline)16 P (org.docx4j.wml.P)10 R (org.docx4j.wml.R)10 Drawing (org.docx4j.wml.Drawing)9 File (java.io.File)5 Jc (org.docx4j.wml.Jc)5 PPr (org.docx4j.wml.PPr)5 CTVerticalJc (org.docx4j.wml.CTVerticalJc)4 ObjectFactory (org.docx4j.wml.ObjectFactory)4 STVerticalJc (org.docx4j.wml.STVerticalJc)4 Text (org.docx4j.wml.Text)4 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)3 BigInteger (java.math.BigInteger)2 PartName (org.docx4j.openpackaging.parts.PartName)2 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)2 CTBorder (org.docx4j.wml.CTBorder)2 PBdr (org.docx4j.wml.PPrBase.PBdr)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1