首页 > 编程语言 >aspose-words

aspose-words

时间:2023-11-27 16:26:14浏览次数:41  
标签:DocumentBuilder doc builder writeln words new aspose Document

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

相关文章

  • com.aspose.words word 转pdf问题
    在讲word转pdf的时候推荐使用以下代码publicstaticvoidmain(String[]args)throwsException{//加载要转换的Word文档。Documentdoc=newDocument("C:\\Temp\\input.doc");//要保存输出的PDF文件的位置。StringoutputFilenam......
  • Aspose.Words使用word模板中的书签/域插入信息并导出
    首先我大概叙述一下我对这个东西的理解毕竟我也只是记录一下,确保下次自己在看的时候可以看懂,所以写的比较详细且傻瓜首先这个word文档不是凭空生成的,是你事先就把文档创建好的,里边的内容,格式都是实现创建好的只留下一些需要插入数据的地方,当然这些需要插入数据的地方也不是空着的......
  • Aspose.Words解除word转pdf的页数限制
    参考文章Aspose.Words破解版用法示例Aspose.Words中文文档本篇文章可以实现解除word转pdf的页数限制,并去除水印。引入dll根据Aspose.Words破解版用法示例这篇文章中提供的dll以及证书,进行注册。右键项目添加项目引用,点击浏览选择15.7的dll进行添加在程序启动时注册......
  • 《AN IMAGE IS WORTH 16X16 WORDS: TRANSFORMERS FOR IMAGE RECOGNITION AT SCALE》阅
    论文标题《ANIMAGEISWORTH16X16WORDS:TRANSFORMERSFORIMAGERECOGNITIONATSCALE》谷歌论文起名越来越写意了,“一幅图像值16X16个单词”是什么玩意儿。ATSCALE:说明适合大规模的图片识别,也许小规模的不好使作者来自GoogleResearch的Brain团队,经典的同等贡献......
  • Aspose.Words for Java21.11去除水印和数量限制
    前言工欲善其事,必先利其器目前用到了word转pdf,然后开始研究大佬的文章,用于个人学习,首先感谢!源码分析1.下载Aspose.WordsforJava21.11官方jar包2.开始分析调用授权方法InputStreamis=newFileInputStream("..license.xml");Licenselicense=newLicense();license.setLi......
  • Aspose.Words 自动编号段落提取编号内容
    https://blog.csdn.net/u010187918/article/details/127729231如果提取不成功请确定该段落是否确实具有自动编号,如果确定,那么请在Aspose.Words.Document类对象doc使用doc.UpdateListLabels()接口后再看是否提取成功。  ///<summary>///获取List编号内容///</summary>/......
  • 使用Aspose.Cell控件实现Excel高难度报表的生成(二)
    继续在上篇《使用Aspose.Cell控件实现Excel高难度报表的生成(一)》随笔基础上,研究探讨基于模板的Aspose.cell报表实现,其中提到了下面两种报表的界面,如下所示: 或者这样的报表格式  首先来分析第一种报表,这个其实还是比较固定的二维表,我们只要绑定相关的信息即可,设计模板如下......
  • aspose替换word关键字
       privatevoidbutton3_Click(objectsender,EventArgse){stringsourcePath=Environment.CurrentDirectory+"/temple/"+"2.doc"; newAspose.Words.License().SetLicense(newMemoryStream(Convert.FromBase64Strin......
  • 531_平台屏蔽太敏感?不如试试WordsAway
    这是一篇原发布于2020-06-1913:30:00得益小站的文章,备份在此处。前言本文提到的工具仅用于帮助发送正常的内容,只能避开机器检测,若有人举报,人工审核后可能遭至更严重的处罚!发布言论在公共平台时请注意自己的一言一行!虽然这是个好东西但是切勿滥用,不然到时候某一天算法可以识别......
  • Compress Words
    CompressWords本人蒟蒻,请看更详细的题解CF1200ECompressWords题解重点是利用KMP计算最长前后缀,注意几个点:长度、越界。点击查看代码#include<bits/stdc++.h>usingnamespacestd;constintN=1e6+10;intne[N];voidkmp(stringss){ intn=ss.length(); ss......