Search in sources :

Example 16 with Inline

use of org.docx4j.dml.wordprocessingDrawing.Inline in project Java-Tutorial by gpcodervn.

the class ImageAddTest method testSizeNoWidth.

@Test
public void testSizeNoWidth() throws Exception {
    Inline inline = getInline("<div><img src='" + PNG_IMAGE_DATA + "' height='20pt'  /></div>");
    Assert.assertTrue(Math.round(inline.getExtent().getCx() / 10) == // +/- a few EMU
    Math.round(inline.getExtent().getCy() / 10));
}
Also used : Inline(org.docx4j.dml.wordprocessingDrawing.Inline) Test(org.junit.Test)

Example 17 with Inline

use of org.docx4j.dml.wordprocessingDrawing.Inline in project Java-Tutorial by gpcodervn.

the class ImageAddTest method testSizeNoHeight.

@Test
public void testSizeNoHeight() throws Exception {
    Inline inline = getInline("<div><img src='" + PNG_IMAGE_DATA + "' width='20pt'  /></div>");
    Assert.assertTrue(Math.round(inline.getExtent().getCx() / 10) == // +/- a few EMU
    Math.round(inline.getExtent().getCy() / 10));
}
Also used : Inline(org.docx4j.dml.wordprocessingDrawing.Inline) Test(org.junit.Test)

Example 18 with Inline

use of org.docx4j.dml.wordprocessingDrawing.Inline in project Java-Tutorial by gpcodervn.

the class ImageResizeTest method testCmAgainstPx.

@Test
public void testCmAgainstPx() throws Exception {
    Inline inline1 = getInline("<div><img src='" + PNG_IMAGE_DATA + "' height='20px' width='40px'/></div>");
    Inline inline2 = getInline("<div><img src='" + PNG_IMAGE_DATA + "' height='20cm' width='40cm'/></div>");
    Assert.assertTrue(inline2.getExtent().getCx() / inline1.getExtent().getCx() > 10);
    Assert.assertTrue(inline2.getExtent().getCx() / inline1.getExtent().getCy() > 10);
}
Also used : Inline(org.docx4j.dml.wordprocessingDrawing.Inline) Test(org.junit.Test)

Example 19 with Inline

use of org.docx4j.dml.wordprocessingDrawing.Inline 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)

Example 20 with Inline

use of org.docx4j.dml.wordprocessingDrawing.Inline in project docx4j-template by vindell.

the class Docx4J_例子2 method newImage.

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;
}
Also used : P(org.docx4j.wml.P) Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) PBdr(org.docx4j.wml.PPrBase.PBdr) CTBorder(org.docx4j.wml.CTBorder) STVerticalJc(org.docx4j.wml.STVerticalJc) Jc(org.docx4j.wml.Jc) CTVerticalJc(org.docx4j.wml.CTVerticalJc) BigInteger(java.math.BigInteger) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Aggregations

Inline (org.docx4j.dml.wordprocessingDrawing.Inline)26 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)16 P (org.docx4j.wml.P)10 R (org.docx4j.wml.R)10 Test (org.junit.Test)10 Drawing (org.docx4j.wml.Drawing)9 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 File (java.io.File)3 BigInteger (java.math.BigInteger)2 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)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 InputStream (java.io.InputStream)1