aspose注册
static { try (InputStream is = AsposeUtil.class.getResourceAsStream("/license.xml")) { License aposeLic = new License(); aposeLic.setLicense(is); } catch (Exception e) { e.printStackTrace(); } }
license.xml
<?xml version="1.0" encoding="UTF-8" ?> <License> <Data> <Products> <Product>Aspose.Total for Java</Product> <Product>Aspose.Words for Java</Product> </Products> <EditionType>Enterprise</EditionType> <SubscriptionExpiry>20991231</SubscriptionExpiry> <LicenseExpiry>20991231</LicenseExpiry> <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber> </Data> <Signature> sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU= </Signature> </License>
基本操作
原文:https://blog.csdn.net/qq_38819293/article/details/126746338
1.创建word文档
//创建文档 Document doc = new Document(path); //创建空文档 Document doc = new Document(); //获取光标 DocumentBuilder builder = new DocumentBuilder(doc); //writeln 方法和 write 方法的区别在于, writeln 在文本之后插入了一个段落终止符 builder.writeln("Hello,Lain!"); //文件保存路径 doc.save(path);
2.设置段落格式
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello,Lain!"); builder.getFont().setColor(Color.decode("#66ccff"));//设置颜色 builder.getFont().setBold(true);//设置粗体 builder.getFont().setItalic(true);//设置斜体 builder.getFont().setName("微软雅黑");//设置字体 builder.getFont().setHighlightColor(Color.gray);//设置背景高亮 builder.getFont().setUnderlineColor(Color.RED);//设置字体颜色 builder.getFont().setUnderline(Underline.DOUBLE);//设置下划线 /* * 缩进 */ builder.getParagraphFormat().setFirstLineIndent(30);//首行缩进 builder.getParagraphFormat().setLeftIndent(30);//缩进:文本之前 builder.getParagraphFormat().setRightIndent(30);//缩进:文本之后 /* * 行距: * AT_LEAST 最小值 * EXACTLY 固定值 * MULTIPLE 多倍行距 * 注:多倍行距的值以12为基准,12为一倍行距,24为两倍,以此类推 */ builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.MULTIPLE); builder.getParagraphFormat().setLineSpacing(24); builder.getParagraphFormat().setSpaceBefore(10);//段前,单位磅 builder.getParagraphFormat().setSpaceAfter(10);//段后,单位磅 /* * 对齐方式 * CENTER 居中对齐 * DISTRIBUTED 分散对齐 * JUSTIFY 两端对齐 * LEFT 左对齐 * RIGHT 右对齐 */ builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT); builder.getParagraphFormat().setBidi(false);//方向:true 从右向左 builder.writeln("Present Day,Present Time!Ahhhhhhh"); doc.save(getTempFileName());
3.页面分隔
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("This is the 1st section,1st Page"); builder.insertBreak(BreakType.PAGE_BREAK);//分页符 builder.writeln("This is the 1st section,2nd Page"); builder.insertBreak(BreakType.LINE_BREAK);//换行符 builder.insertBreak(BreakType.COLUMN_BREAK);//分栏符 builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);//连续分节符 builder.writeln("This is the 3rd section,1nd Page"); builder.writeln("连续分节符"); builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);//偶数页分节符 builder.writeln("This is the 4th section,1nd Page"); builder.writeln("偶数页分节符"); builder.insertBreak(BreakType.SECTION_BREAK_ODD_PAGE);//奇数页分节符 builder.writeln("This is the 5th section,1nd Page"); builder.writeln("奇数页分节符"); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);//下一页分节符 builder.writeln("This is the 6th section,1nd Page"); builder.writeln("下一页分节符"); builder.insertBreak(BreakType.SECTION_BREAK_NEW_COLUMN);//节的结尾 builder.writeln("This is the 7th section,1nd Page"); builder.writeln("节的结尾"); doc.save(getTempFileName());
4.页面属性设置
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Present Day,Present Life!Ahhhhhhh"); builder.getPageSetup().setPaperSize(PaperSize.A4);//纸张大小 builder.getPageSetup().setLeftMargin(20);//设置边距 单位为磅 builder.getPageSetup().setRightMargin(10); builder.getPageSetup().setTopMargin(10); builder.getPageSetup().setBottomMargin(10); builder.getPageSetup().setTextOrientation(TextOrientation.DOWNWARD);//文字方向 builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);//横纵向 PORTRAIT 纵向 LANDSCAPE 横向 doc.save(getTempFileName());
5.设置页眉页脚
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Hello,Lain!"); builder.insertBreak(BreakType.LINE_BREAK); builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);//移动到页眉 builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); builder.writeln("Hello,Lain!"); builder.moveToDocumentEnd(); builder.writeln("Hello,Lain!"); builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);//移动到页脚 builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); builder.getFont().setName("微软雅黑"); builder.write("第"); builder.insertField("PAGE", "");//当前页数 builder.write("页"); builder.write(" 共"); builder.insertField("NUMPAGES", "");//总页数 builder.write("页"); builder.moveToDocumentEnd(); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); builder.write("Hello,Lain!"); doc.save(getTempFileName());
6.生成表格
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.startTable(); builder.insertCell(); builder.getCellFormat().setWidth(100); builder.getFont().setName("华文正楷"); builder.getFont().setColor(Color.decode("#66ccff")); builder.getParagraphFormat().setLineSpacing(24); builder.write("Name"); builder.insertCell(); builder.getCellFormat().setWidth(200); builder.write("describe"); builder.endRow(); builder.insertCell(); builder.getCellFormat().setWidth(100); builder.write("Lain"); builder.insertCell(); builder.getCellFormat().setWidth(200); builder.write("Make Me Sad"); builder.endRow(); builder.endTable(); doc.save(getTempFileName());
7.图片操作
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); builder.insertImage(image); doc.save(getTempFileName());
图片布满整个页面
private static void appendImgDocument(Document doc, String imgPath) throws Exception { if (StringUtils.hasLength(imgPath)) { //获取页面大小 PageInfo pageInfo = doc.getPageInfo(0); float heightInPoints = pageInfo.getHeightInPoints(); float widthInPoints = pageInfo.getWidthInPoints(); //创建新文档,防止影响到其他页面 Document imgDoc = new Document(); DocumentBuilder imgBuilder = new DocumentBuilder(imgDoc); //设置宽高 imgBuilder.getPageSetup().setPageHeight(heightInPoints); imgBuilder.getPageSetup().setPageWidth(widthInPoints); //页边距 imgBuilder.getPageSetup().setTopMargin(0); imgBuilder.getPageSetup().setBottomMargin(0); imgBuilder.getPageSetup().setLeftMargin(0); imgBuilder.getPageSetup().setRightMargin(0); //插入图片 imgBuilder.insertImage(imgPath, widthInPoints, heightInPoints); doc.appendDocument(imgDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); } }
8.大纲目录
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.insertTableOfContents("\\o \"1-9\" \\h \\z \\u");//参考微软官方office文档 builder.writeln(); builder.getParagraphFormat().setOutlineLevel(OutlineLevel.LEVEL_1); builder.writeln("一级"); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); builder.getParagraphFormat().setOutlineLevel(OutlineLevel.LEVEL_2); builder.writeln("二级"); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); builder.getParagraphFormat().setOutlineLevel(OutlineLevel.LEVEL_3); builder.writeln("三级"); builder.getParagraphFormat().setOutlineLevel(OutlineLevel.BODY_TEXT); builder.writeln("正文"); doc.updateFields(); doc.save(getTempFileName());
9.合并文档
doc.appendDocument(new Document(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
标签:DocumentBuilder,doc,builder,writeln,words,new,aspose,Document From: https://www.cnblogs.com/BambooLamp/p/17859607.html