aspose-words基本操作

1.创建word文档

public void createDoc() {
  //授权方法
  getLicense();
  try
    {
    	//设置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)
	} catch (Exception e) {
			e.printStackTrace(); 
  }
  
}

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());

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());

你可能感兴趣的:(asp.net,C#,asp.net,c#,后端)