package org.example.demoword.demo1;
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.ShapeObject;
import com.spire.doc.fields.TextBox;
import com.spire.doc.fields.TextRange;
import java.awt.*;
/**
* 无模版生成指定样式的Word文档
*/
public class demoTest1 {
public static void main(String[] args) throws Exception {
//创建Word文档
Document doc = new Document();
//添加一个section
Section sec = doc.addSection();
//设置页眉
setHeaderStyle(sec);
//设置主体标题
setTitleStyle(sec, doc);
//设置主体表格
//医院信息
setTable1(sec);
//保存文档
doc.saveToFile("/Users/luojiwen/Desktop/bgi_temp/test2.docx");
doc.close();
}
/**
* 设置主体表格1
* @param sec
*/
private static void setTable1(Section sec) {
Table table1 = sec.addTable(true);
table1.resetCells(3, 4);
////调用方法纵向合并第1列中的第2、3个单元格
table1.applyHorizontalMerge(0,0,3);
Paragraph paragraph = table1.get(0, 0).addParagraph();
TextRange textRange = paragraph.appendText("医院信息");
textRange.getCharacterFormat().setFontName("宋体");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
// paragraph.getFormat().setBackColor(Color.CYAN);
paragraph = table1.get(1, 0).addParagraph();
textRange = paragraph.appendText("送检医院");
textRange.getCharacterFormat().setFontName("宋体");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
paragraph = table1.get(1, 1).addParagraph();
textRange = paragraph.appendText("送检医生");
textRange.getCharacterFormat().setFontName("宋体");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
paragraph = table1.get(1, 2).addParagraph();
textRange = paragraph.appendText("门诊号/住院号");
textRange.getCharacterFormat().setFontName("宋体");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
paragraph = table1.get(1, 3).addParagraph();
textRange = paragraph.appendText("科室");
textRange.getCharacterFormat().setFontName("宋体");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
paragraph = table1.get(2, 0).addParagraph();
textRange = paragraph.appendText("中国医学科学院北京协和医院");
textRange.getCharacterFormat().setFontName("宋体");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
paragraph = table1.get(2, 1).addParagraph();
textRange = paragraph.appendText("潘慧");
textRange.getCharacterFormat().setFontName("宋体");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
paragraph = table1.get(2, 2).addParagraph();
textRange = paragraph.appendText("-");
textRange.getCharacterFormat().setFontName("宋体");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
paragraph = table1.get(2, 3).addParagraph();
textRange = paragraph.appendText("/");
textRange.getCharacterFormat().setFontName("宋体");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//设置表格水平对齐方式
table1.getTableFormat().setHorizontalAlignment(RowAlignment.Right);
table1.get(0,0).getCellFormat().setBackColor(new Color(182, 221, 232));
//表格自适应页面宽度
table1.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);
}
/**
* 设置主体标题
* @param sec
* @param doc
*/
private static void setTitleStyle(Section sec, Document doc) {
//设置主体内容
//设置文章标题
Paragraph titleParagraph = sec.addParagraph();
//设置主题内容
TextRange textRange = titleParagraph.appendText("全基因组测序检测报告(单人)");
//将第一段作为标题,设置标题格式
ParagraphStyle style1 = new ParagraphStyle(doc);
style1.setName("titleStyle");
style1.getCharacterFormat().setBold(true);
style1.getCharacterFormat().setTextColor(Color.black);
style1.getCharacterFormat().setFontName("宋体");
style1.getCharacterFormat().setFontSize(18f);
doc.getStyles().add(style1);
titleParagraph.applyStyle("titleStyle");
//设置第一个段落的对齐方式
titleParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//设置段落后间距
titleParagraph.getFormat().setAfterSpacing(15f);
}
/**
* 设置页眉,包含图片,文本框,文字
* @param sec
*/
private static void setHeaderStyle(Section sec) {
//==================================页眉图片=============================================
//设置奇偶页页眉页脚不同
sec.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);
sec.getPageSetup().setHeaderDistance(10f);
//设置奇数页页眉页脚
HeaderFooter firstHeader = sec.getHeadersFooters().getOddHeader();
Paragraph headerParagraph = firstHeader.addParagraph();
// String imgUrl = "https://img2.baidu.com/it/u=256816879,771155532&fm=253&fmt=auto&app=120&f=JPEG?w=1204&h=800";
//
// URL url = new URL(imgUrl);
//
// InputStream inputStream = url.openStream();
DocPicture picture = headerParagraph.appendPicture("/Users/luojiwen/Desktop/bgi_temp/bbfe.jpg");
//设置图片大小
// picture.setWidth(200f);
picture.setHeight(30f);
//设置图片环绕方式为四周型
picture.setTextWrappingStyle(TextWrappingStyle.Square);
picture.setTextWrappingType(TextWrappingType.Both);
//==============================================页眉文本框=====================================================================
//添加文本框
TextBox tb = headerParagraph.appendTextBox(100f, 20f);
tb.getFormat().setTextWrappingStyle(TextWrappingStyle.Square);
//设置文本框的位置
tb.getFormat().setHorizontalOrigin(HorizontalOrigin.Right_Margin_Area);
tb.getFormat().setHorizontalPosition(-100f);
tb.getFormat().setVerticalOrigin(VerticalOrigin.Page);
tb.getFormat().setVerticalPosition(10f);
//设置文本框的边框颜色和填充颜色
tb.getFormat().setLineColor(Color.black);
//设置填充颜色
// tb.getFormat().setFillColor(new Color(203,234,253) );
//在文本框中插入
Paragraph para = tb.getBody().addParagraph();
TextRange textRange = para.appendText("样本编号:130U030319");
//设置文本框中的文字居中
para.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
//设置字体
textRange.getCharacterFormat().setFontName("等线");
textRange.getCharacterFormat().setFontSize(8f);
// textRange.getCharacterFormat().setItalic(true);
//======================================页眉右边下面的文字=====================================================================
//添加页眉右边下面的文字:DX-WGSP-B37 V1.0
TextBox tb1 = headerParagraph.appendTextBox(100f, 20f);
tb1.getFormat().setHorizontalOrigin(HorizontalOrigin.Inner_Margin_Area);
//设置位置
tb1.getFormat().setHorizontalPosition(-100f);
tb1.getFormat().setVerticalOrigin(VerticalOrigin.Page);
tb1.getFormat().setVerticalPosition(30f);
tb1.getFormat().setNoLine(true);
//添加文字�
没有合适的资源?快使用搜索试试~ 我知道了~
Spire.Doc for Java 11.4.2和aspose-words-24.01全功能无限制使用,无水印,亲测可用
共66个文件
sample:13个
xml:8个
java:5个
需积分: 0 0 下载量 32 浏览量
2024-05-15
14:14:34
上传
评论
收藏 37.34MB ZIP 举报
温馨提示
内涵java demo测试代码,直接main方法直接运行,亲测可用,可直接查看使用演示,验证真假, Jar所在地址src/main/resources/lib,若需要更高版本或者最新版本和有任何问题,可联系博主
资源推荐
资源详情
资源评论
收起资源包目录
demo-word.zip (66个子文件)
demo-word
HELP.md 461B
mvnw.cmd 7KB
pom.xml 3KB
src
test
java
org
example
demoword
DemoWordApplicationTests.java 223B
main
resources
lib
aspose-words-24.01-jdk17-jie.jar 13.32MB
application.properties 34B
templates
static
java
org
example
demoword
demo2
demoTest1.java 4KB
B.docx 12KB
A.docx 13KB
demo1
demoTest1.java 9KB
bbfe.docx 46KB
demoTest2.java 3KB
DemoWordApplication.java 329B
.mvn
wrapper
maven-wrapper.properties 233B
maven-wrapper.jar 61KB
.git
index 2KB
HEAD 23B
refs
heads
tags
objects
df
cf27cbf8e79afe1bbbbdd059037f84df72c92a 169B
e7
0e7bc8d68e955b549770fd646db8b1851c5577 128B
7f
af6acf111108fd00816c11d466d55b8bdf494c 531B
66
df2854281f4cb6869e4830dd1a7abd1e946c18 4KB
76
801a3cb432cf637c643fab309c1de99c8cd5ee 72B
1d
80d98310a5b1933994fa594ea5747799f5ee7e 176B
95
ba6f54ac526de46248af840bab26f33f946b93 3KB
0e
f0af8b7b8697880b0e34609e8357192f6b9d02 11KB
f2
4b259a44b28eca9aacd360558dff7df6965492 50B
9c
e5eab03306e7e1732feb84eb5373d6492d8257 72B
pack
54
9e00a2a96fa9d7c5dbc9859664a78d980158c2 249B
info
52
5b5e79d5485f6e0f0becc5fc09aef857fbcbb5 11KB
a0
ce84b76ac00c68bd60e6ac6b7ab64546e07981 45KB
98
161b0149c41905d81e70a3c6781a49fbcd5430 72B
cb
28b0e37c7d206feb564310fdeec0927af4123a 55KB
24
fdd93926b9d5834c22d1040a1f4891c785a09b 189B
e9
77ca7a10f9415389bb18b570fe8445c9b48944 12.38MB
description 73B
info
exclude 250B
hooks
post-update.sample 189B
prepare-commit-msg.sample 1KB
commit-msg.sample 896B
pre-receive.sample 544B
update.sample 4KB
pre-commit.sample 2KB
pre-rebase.sample 5KB
applypatch-msg.sample 478B
fsmonitor-watchman.sample 5KB
push-to-checkout.sample 3KB
pre-applypatch.sample 424B
pre-push.sample 1KB
pre-merge-commit.sample 416B
config 137B
branches
.idea
jarRepositories.xml 1KB
uiDesigner.xml 9KB
vcs.xml 180B
workspace.xml 8KB
misc.xml 521B
compiler.xml 698B
.gitignore 0B
encodings.xml 186B
target
classes
lib
aspose-words-24.01-jdk17-jie.jar 13.32MB
application.properties 34B
org
example
demoword
DemoWordApplication.class 753B
demo2
demoTest1.class 3KB
demo1
demoTest1.class 7KB
demoTest2.class 3KB
generated-sources
annotations
mvnw 11KB
.gitignore 395B
共 66 条
- 1
资源评论
luolearn
- 粉丝: 31
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功