use of org.apache.poi.xwpf.usermodel.XWPFRun in project java-learning by HouariZegai.
the class WordAPIDemo method main.
public static void main(String[] args) throws InvalidFormatException {
try {
String machineName = System.getProperty("user.name");
FileOutputStream file = new FileOutputStream("C:\\Users\\" + machineName + "\\Downloads\\Documents\\wordFromJava.docx");
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("I'm Houari ZEGAI coming from Java !");
run.addBreak();
run.setFontFamily("Comic Sans MS");
run.setBold(true);
XWPFRun run2 = paragraph.createRun();
run2.setText("Second paragraph");
run2.setColor("2196f3");
run2.setFontSize(24);
run2.setUnderline(UnderlinePatterns.DASHED_HEAVY);
// Adding image
XWPFRun run3 = paragraph.createRun();
run3.addPicture(WordAPIDemo.class.getResourceAsStream("/images/dark.jpg"), XWPFDocument.PICTURE_TYPE_JPEG, "houari", Units.toEMU(400), Units.toEMU(200));
// Adding table
XWPFTable table = document.createTable();
table.setCellMargins(20, 200, 20, 200);
XWPFTableRow row0 = table.getRow(0);
row0.getCell(0).setText("N°Dos");
row0.createCell().setText("Nom");
row0.createCell().setText("Prenom");
row0.createCell().setText("Date Naissance");
row0.createCell().setText("Sexe");
row0.createCell().setText("Club");
row0.createCell().setText("Code Wilaya");
row0.createCell().setText("Eq/Ind");
row0.getCell(0).setColor("2196f3");
row0.getCell(1).setColor("2196f3");
row0.getCell(2).setColor("2196f3");
row0.getCell(3).setColor("2196f3");
row0.getCell(4).setColor("2196f3");
row0.getCell(5).setColor("2196f3");
row0.getCell(6).setColor("2196f3");
row0.getCell(7).setColor("2196f3");
XWPFTableRow row1 = table.createRow();
row1.getCell(0).setText("001");
row1.getCell(1).setText("Houari");
row1.getCell(2).setText("ZEGAI");
row1.getCell(3).setText("01/01/1996");
row1.getCell(4).setText("Homme");
row1.getCell(5).setText("CAAT");
row1.getCell(6).setText("TIARET");
row1.getCell(7).setText("E");
XWPFTableRow row2 = table.createRow();
row2.getCell(0).setText("002");
row2.getCell(1).setText("Khaled");
row2.getCell(2).setText("ZEGAI");
row2.getCell(3).setText("23/09/1968");
row2.getCell(4).setText("Homme");
row2.getCell(5).setText("CSST");
row2.getCell(6).setText("TIARET");
row2.getCell(7).setText("I");
paragraph.setAlignment(ParagraphAlignment.CENTER);
document.write(file);
file.close();
System.out.println("Done !");
} catch (FileNotFoundException e) {
System.out.println("Error msg: (FileNotFoundException) " + e.getMessage());
} catch (IOException ex) {
System.out.println("Error msg: (IOException) " + ex.getMessage());
}
}
use of org.apache.poi.xwpf.usermodel.XWPFRun in project hutool by looly.
the class Word07Writer method addPicture.
/**
* 增加图片,单独成段落,增加后图片流关闭
*
* @param in 图片流
* @param picType 图片类型,见Document.PICTURE_TYPE_XXX
* @param fileName 文件名
* @param width 宽度
* @param height 高度
* @param align 图片的对齐方式
* @return this
* @since 5.2.4
*/
public Word07Writer addPicture(InputStream in, PicType picType, String fileName, int width, int height, ParagraphAlignment align) {
final XWPFParagraph paragraph = doc.createParagraph();
paragraph.setAlignment(align);
final XWPFRun run = paragraph.createRun();
try {
run.addPicture(in, picType.getValue(), fileName, Units.toEMU(width), Units.toEMU(height));
} catch (InvalidFormatException e) {
throw new POIException(e);
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(in);
}
return this;
}
use of org.apache.poi.xwpf.usermodel.XWPFRun in project pmph by BCSquad.
the class BookChooseTrackWordHelper method fromDeclarationEtcBOList.
/**
* 填充 word 数据
* @param word_name
* @return
*/
private HashMap<String, XWPFDocument> fromDeclarationEtcBOList(String word_name, Book bo) {
InputStream is;
XWPFDocument document;
String path = this.getClass().getClassLoader().getResource("BookChooseTrackTemplate.docx").getPath();
HashMap<String, XWPFDocument> map = new HashMap<>(1);
int decSequese = 0;
Object object = (Object) bo;
try {
is = new FileInputStream(path);
document = new XWPFDocument(is);
} catch (IOException ex) {
logger.error("读取Word模板文件'ClicResumeTemplate.docx'时出现错误:{}", ex.getMessage());
throw new CheckedServiceException(CheckedExceptionBusiness.CLINICAL_DECISION, CheckedExceptionResult.FILE_CREATION_FAILED, "未找到模板文件");
}
if (StringUtil.notEmpty(word_name)) {
List<XWPFRun> runs = document.getParagraphs().get(0).getRuns();
runs.get(0).setText(word_name.concat("图书纠错"), 0);
}
List<XWPFParagraph> xwpfParagraphs = document.getParagraphs();
List<XWPFTable> tables = document.getTables();
String filename = generateFileName(bo, (++decSequese) + ".");
XWPFTable xwpfTable = tables.get(0);
xwpfTable.getRow(0).getTableCells().get(1).setText(bo.getIsbn());
xwpfTable.getRow(0).getCell(3).setText(bo.getBookname() == null ? "" : bo.getBookname());
xwpfTable.getRow(0).getCell(5).setText(bo.getIsbn() == null ? "" : bo.getIsbn().toString());
/* map.put(filename, removeEmptyTables(document, filter));*/
map.put(filename, document);
return map;
}
use of org.apache.poi.xwpf.usermodel.XWPFRun in project poi by apache.
the class SimpleImages method main.
public static void main(String[] args) throws IOException, InvalidFormatException {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p = doc.createParagraph();
XWPFRun r = p.createRun();
for (String imgFile : args) {
int format;
if (imgFile.endsWith(".emf"))
format = XWPFDocument.PICTURE_TYPE_EMF;
else if (imgFile.endsWith(".wmf"))
format = XWPFDocument.PICTURE_TYPE_WMF;
else if (imgFile.endsWith(".pict"))
format = XWPFDocument.PICTURE_TYPE_PICT;
else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
format = XWPFDocument.PICTURE_TYPE_JPEG;
else if (imgFile.endsWith(".png"))
format = XWPFDocument.PICTURE_TYPE_PNG;
else if (imgFile.endsWith(".dib"))
format = XWPFDocument.PICTURE_TYPE_DIB;
else if (imgFile.endsWith(".gif"))
format = XWPFDocument.PICTURE_TYPE_GIF;
else if (imgFile.endsWith(".tiff"))
format = XWPFDocument.PICTURE_TYPE_TIFF;
else if (imgFile.endsWith(".eps"))
format = XWPFDocument.PICTURE_TYPE_EPS;
else if (imgFile.endsWith(".bmp"))
format = XWPFDocument.PICTURE_TYPE_BMP;
else if (imgFile.endsWith(".wpg"))
format = XWPFDocument.PICTURE_TYPE_WPG;
else {
System.err.println("Unsupported picture: " + imgFile + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
continue;
}
r.setText(imgFile);
r.addBreak();
// 200x200 pixels
r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200));
r.addBreak(BreakType.PAGE);
}
FileOutputStream out = new FileOutputStream("images.docx");
doc.write(out);
out.close();
doc.close();
}
use of org.apache.poi.xwpf.usermodel.XWPFRun in project poi by apache.
the class SimpleTable method createSimpleTable.
public static void createSimpleTable() throws Exception {
XWPFDocument doc = new XWPFDocument();
try {
XWPFTable table = doc.createTable(3, 3);
table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
// table cells have a list of paragraphs; there is an initial
// paragraph created when the cell is created. If you create a
// paragraph in the document to put in the cell, it will also
// appear in the document following the table, which is probably
// not the desired result.
XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
XWPFRun r1 = p1.createRun();
r1.setBold(true);
r1.setText("The quick brown fox");
r1.setItalic(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
r1.setTextPosition(100);
table.getRow(2).getCell(2).setText("only text");
OutputStream out = new FileOutputStream("simpleTable.docx");
try {
doc.write(out);
} finally {
out.close();
}
} finally {
doc.close();
}
}
Aggregations