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);
}
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);
}
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;
}
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);
}
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);
}
Aggregations