- Code: Select all
//实例化Document类的对象
Document document = new Document();
//加载Word
document.loadFromFile("D://_tmp//");
//定位到指定书签位置末尾标签位置,插入文本
//插入书签
BookmarksNavigator bookmarksNavigator2 = new BookmarksNavigator(document);
bookmarksNavigator2.moveToBookmark("FILE_NUMBER", false, true);
ITextRange range = bookmarksNavigator2.insertText("R-EO-YⅠ0010-001");
range.getCharacterFormat().setFontName("黑体");
range.getCharacterFormat().setFontSize(14f);
bookmarksNavigator2.moveToBookmark("GMP_NAME", false, true);
ITextRange range1 = bookmarksNavigator2.insertText("生化分析仪试剂更换、定标及质控品比对记录 Record of the reagent replacement, calibration and quality control comparison for biochemical analyzer of Drug Substance Workshop I");
range1.getCharacterFormat().setFontName("黑体");
range1.getCharacterFormat().setFontSize(14f);
bookmarksNavigator2.moveToBookmark("VERSION", false, true);
ITextRange range2 = bookmarksNavigator2.insertText("1.0");
range2.getCharacterFormat().setFontName("黑体");
range2.getCharacterFormat().setFontSize(14f);
//获取最大流水号
String maxNumber = "0000000001"
//获取文档第一节
Section section = document.getSections().get(0);
//调用自定义方法 insertHeaderAndFooter() 来插入页眉和页脚流水号
HeaderFooter head = document.getSections().get(0).getHeadersFooters().getHeader();
Paragraph newPara = new Paragraph(document);
newPara.setText(maxNumber);
head.getParagraphs().insert(0,newPara);
//insertHeaderAndFooter(section, maxNumber);
//插入二维码
Section tempSection = document.addSection();
DocPicture docPicture = tempSection.addParagraph().appendPicture("D://_tmp//_printlog//qrR-EO-YⅠ0010-001-00000010.png");//生成的二维码图片地址
docPicture.setTextWrappingStyle(TextWrappingStyle.Inline);
ParagraphBase replacementFirstItem = (ParagraphBase) tempSection.getParagraphs().get(0).getItems().getFirstItem();
ParagraphBase replacementLastItem = (ParagraphBase)tempSection.getParagraphs().get(tempSection.getParagraphs().getCount()- 1).getItems().getLastItem();
TextBodySelection selection = new TextBodySelection(replacementFirstItem, replacementLastItem);
TextBodyPart part = new TextBodyPart(selection);
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.moveToBookmark("gao", true, false);
bookmarkNavigator.replaceBookmarkContent(part);
document.getSections().remove(tempSection);
//保存
if("doc".equals(extention)){
document.saveToFile(newFilePath, FileFormat.Doc);
}else{
document.saveToFile(newFilePath, FileFormat.Docx_2013);
}
document.dispose();
上面是我的代码,我这边出现了二种情况
1、因为这边有俩种格式的文档,再使用上面的方法填充二维码时,B文档模板中的二维码展示不出来;
2、在页眉中插入流水号的时候,A文档模板的二维码特别靠上,打印时会打印不全;
3、(特殊需求)有没有可以设置页眉中 “题目Title” 书签的中文和英文两种文字字体。中文为“黑体”,英文为“新罗马”。例子:页眉“题目Title”里的书签插入内容为“生化分析仪试剂更换、定标及质控品比对记录
Record of the reagent replacement, calibration and quality control comparison for biochemical analyzer of Drug Substance Workshop I
”
感激帮助!!!!