需要j所有jar包,笔者这里是将jar包下载后进行了jar包导入。写了一个简易教程。mvn工程的自己需要根据jar包名自己去配置pom文件,笔者后续有空再补充。
commons-collections4-4.4.jar
commons-compress-1.26.1.jar
commons-io-2.20.0.jar
commons-logging-1.3.5.jar
fontbox-3.0.6.jar
itextpdf-5.5.13.3.jar
log4j-api-2.25.1.jar
log4j-core-2.25.1.jar
pdfbox-3.0.6.jar
pdfbox-io-3.0.6.jar
poi-5.4.1.jar
poi-ooxml-5.4.1.jar
poi-ooxml-lite-5.2.4.jar
poi-scratchpad-5.4.1.jar
xmlbeans-5.3.0.jar
SourceHanSansCN-Normal.ttf//字体工具 C:/Windows/Fonts/目录下可以找到放到自己的工程目录下进行加载即可
代码如下
需要编写word文档读取的每行内容的实体类对象
public class TextLine {
private int lineNumber;
private String text;
private String lineType; // 段落、表格行等
private String style;
public TextLine(int lineNumber, String text, String lineType) {
this.lineNumber = lineNumber;
this.text = text;
this.lineType = lineType;
}
// Getter和Setter
public int getLineNumber() { return lineNumber; }
public void setLineNumber(int lineNumber) { this.lineNumber = lineNumber; }
public String getText() { return text; }
public void setText(String text) { this.text = text; }
public String getLineType() { return lineType; }
public void setLineType(String lineType) { this.lineType = lineType; }
public String getStyle() { return style; }
public void setStyle(String style) { this.style = style; }
@Override
public String toString() {
return String.format("行%d [%s]: %s", lineNumber, lineType, text);
}
}
读取word文档工具类
public class TestWordRead {
public static void main(String[] args) throws IOException {
List list= readDocxLinesWithInfo("D://22222.docx");
for (Object obj:list) {
TextLine text=(TextLine)obj;
System.out.println(text.getLineType());
System.out.println(text.getLineNumber());
}
}
/**
* 读取Word文档并返回带行号的文本行
*/
public static List<TextLine> readLinesWithLineNumbers(String filePath) throws IOException {
File file = new File(filePath);
String fileName = file.getName().toLowerCase();
if (fileName.endsWith(".docx")) {
return readDocxLinesWithInfo(filePath);
} else if (fileName.endsWith(".doc")) {
return readDocLinesWithInfo(filePath);
} else {
throw new IllegalArgumentException("不支持的文件格式");
}
}
/**
* 读取.docx文件并获取详细信息
*/
static List<TextLine> readDocxLinesWithInfo(String filePath) throws IOException {
List<TextLine> textLines = new ArrayList<>();
try (FileInputStream fis = new FileInputStream(filePath);
XWPFDocument document = new XWPFDocument(fis)) {
int lineNumber = 1;
// 处理段落
for (XWPFParagraph paragraph : document.getParagraphs()) {
String text = paragraph.getText().trim();
if (!text.isEmpty()) {
TextLine textLine = new TextLine(lineNumber++, text, "段落");
textLine.setStyle(getParagraphStyle(paragraph));
textLines.add(textLine);
}
}
// 处理表格
for (XWPFTable table : document.getTables()) {
int a=0;
for (XWPFTableRow row : table.getRows()) {
StringBuilder rowText = new StringBuilder();
for (XWPFTableCell cell : row.getTableCells()) {
String cellText = cell.getText().trim();
if (!cellText.isEmpty()) {
rowText.append(cellText).append(" | ");
}
}
if (rowText.length() > 0) {
String text = rowText.substring(0, rowText.length() - 3);
a=a+1;
String str="表格行";
if(a==1){
str="start表格行";
}
if(a==table.getNumberOfRows()){
str="end表格行";
}
TextLine textLine = new TextLine(lineNumber++, text, str);
textLines.add(textLine);
}
}
}
}
return textLines;
}
/**
* 读取.doc文件并获取详细信息
*/
private static List<TextLine> readDocLinesWithInfo(String filePath) throws IOException {
List<TextLine> textLines = new ArrayList<>();
try (FileInputStream fis = new FileInputStream(filePath);
HWPFDocument document = new HWPFDocument(fis)) {
Range range = document.getRange();
int numParagraphs = range.numParagraphs();
int lineNumber = 1;
for (int i = 0; i < numParagraphs; i++) {
Paragraph paragraph = range.getParagraph(i);
String text = paragraph.text().trim();
if (!text.isEmpty()) {
TextLine textLine = new TextLine(lineNumber++, text, "段落");
textLine.setStyle(getParagraphStyle(paragraph));
textLines.add(textLine);
}
}
}
return textLines;
}
private static String getParagraphStyle(XWPFParagraph paragraph) {
List<String> styles = new ArrayList<>();
if (paragraph.getStyle() != null) {
styles.add("样式:" + paragraph.getStyle());
}
if (paragraph.getAlignment() != null) {
styles.add("对齐:" + paragraph.getAlignment());
}
return String.join(", ", styles);
}
/**
* 获取.doc段落样式信息
*/
private static String getParagraphStyle(Paragraph paragraph) {
List<String> styles = new ArrayList<>();
styles.add("字符数:" + paragraph.text().length());
return String.join(", ", styles);
}
}
需要编写字体加载工具类AdobeFontManager,中文的话需要中文字体
public class AdobeFontManager {
private Map<String, BaseFont> fontCache = new HashMap<>();
private static AdobeFontManager instance;
private AdobeFontManager() {
initializeStandardFonts();
}
public static AdobeFontManager getInstance() {
if (instance == null) {
instance = new AdobeFontManager();
}
return instance;
}
/**
* 初始化标准Adobe字体
*/
private void initializeStandardFonts() {
try {
// Adobe标准14字体
fontCache.put("Helvetica",
BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED));
fontCache.put("Helvetica-Bold",
BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED));
fontCache.put("Times-Roman",
BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED));
fontCache.put("Courier",
BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED));
fontCache.put("Symbol",
BaseFont.createFont(BaseFont.SYMBOL, BaseFont.WINANSI, BaseFont.EMBEDDED));
fontCache.put("ZapfDingbats",
BaseFont.createFont(BaseFont.ZAPFDINGBATS, BaseFont.WINANSI, BaseFont.EMBEDDED));
} catch (Exception e) {
throw new RuntimeException("初始化Adobe标准字体失败", e);
}
}
/**
* 注册自定义Adobe字体
*/
public void registerFont(String fontName, String fontPath) {
try {
BaseFont font = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
fontCache.put(fontName, font);
} catch (Exception e) {
throw new RuntimeException("注册Adobe字体失败: " + fontName, e);
}
}
/**
* 获取字体
*/
public BaseFont getFont(String fontName) {
BaseFont font = fontCache.get(fontName);
if (font == null) {
throw new IllegalArgumentException("字体未注册: " + fontName);
}
return font;
}
/**
* 获取字体列表
*/
public String[] getAvailableFonts() {
return fontCache.keySet().toArray(new String[0]);
}
/**
* 预加载常用Adobe字体
*/
public void preloadCommonAdobeFonts() {
String[] commonFonts = {
"Minion Pro", "Myriad Pro", "Garamond Premier",
"Adobe Caslon", "Chaparral Pro", "Source Sans Pro"
};
for (String fontName : commonFonts) {
try {
autoDetectAndRegister(fontName);
} catch (Exception e) {
System.out.println("警告: 无法加载字体 " + fontName + " - " + e.getMessage());
}
}
}
private void autoDetectAndRegister(String fontName) {
// 简化的自动检测逻辑
String fontFile = fontName.replace(" ", "") + "-Regular.otf";
AdobeFontFileLoader loader = new AdobeFontFileLoader();
try {
BaseFont font = loader.autoDetectAdobeFont(fontFile);
fontCache.put(fontName, font);
} catch (Exception e) {
// 忽略加载失败
}
}
}
AdobeFontFileLoader类
public class AdobeFontFileLoader {
/**
* 加载Adobe字体文件
*/
public BaseFont loadAdobeFont(String fontPath) {
try {
return BaseFont.createFont(
fontPath,
BaseFont.IDENTITY_H,
BaseFont.EMBEDDED
);
} catch (Exception e) {
throw new RuntimeException("加载Adobe字体失败: " + fontPath, e);
}
}
/**
* 常用Adobe字体路径映射
*/
public static class AdobeFontPaths {
// Windows系统Adobe字体路径
public static final String WINDOWS_FONT_DIR = "C:/Windows/Fonts/";
public static final String ADOBE_FONT_DIR = "C:/Program Files/Adobe/";
// 常见Adobe字体文件名
public static final String MINION_PRO = "MinionPro-Regular.otf";
public static final String MYRIAD_PRO = "MyriadPro-Regular.otf";
public static final String GARAMOND_PREMIER = "GaramondPremrPro.otf";
public static final String ADOBE_CASLON = "ACaslonPro-Regular.otf";
public static final String CHAPARRAL_PRO = "ChaparralPro-Regular.otf";
}
/**
* 自动检测系统Adobe字体
*/
public BaseFont autoDetectAdobeFont(String fontName) {
String[] possiblePaths = {
AdobeFontPaths.WINDOWS_FONT_DIR + fontName,
"/Library/Fonts/" + fontName, // macOS
"/usr/share/fonts/" + fontName, // Linux
"fonts/adobe/" + fontName // 项目内字体目录
};
for (String path : possiblePaths) {
File fontFile = new File(path);
if (fontFile.exists()) {
return loadAdobeFont(path);
}
}
throw new RuntimeException("未找到Adobe字体: " + fontName);
}
}
主类ProfessionalPdfWithAdobeFonts在这里完成pdf编写
public class ProfessionalPdfWithAdobeFonts {
public static void main(String[] args) {
createProfessionalDocument();
}
public static void createProfessionalDocument() {
try {
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("D://testpdf.pdf"));
document.open();
// 初始化字体管理器
AdobeFontManager fontManager = AdobeFontManager.getInstance();
fontManager.preloadCommonAdobeFonts();
URL resource = ProfessionalPdfWithAdobeFonts.class.getClassLoader().getResource("");
String classpathRoot="";
if (resource != null) {
int index=resource.getPath().indexOf("/out");
classpathRoot=resource.getPath().substring(0,index);
classpathRoot =classpathRoot+ File.separator+"src/lib"+File.separator+"SourceHanSansCN-Normal.ttf";
System.out.println("Classpath根目录: " + classpathRoot);
}
fontManager.registerFont("SourceHanSansCN",classpathRoot);
// 创建标题(使用Myriad Pro)
BaseFont titleFont = fontManager.getFont("SourceHanSansCN");
Font title = new Font(titleFont, 24, Font.BOLD, BaseColor.DARK_GRAY);
Paragraph titlePara = new Paragraph("专业文档标题", title);
titlePara.setAlignment(Element.ALIGN_CENTER);
titlePara.setSpacingAfter(20);
document.add(titlePara);
// 创建正文(使用Minion Pro)
BaseFont bodyFont = fontManager.getFont("SourceHanSansCN");
Font normal = new Font(bodyFont, 12);
Font italic = new Font(bodyFont, 12, Font.ITALIC);
Paragraph content = new Paragraph();
content.add(new Chunk("这是使用Adobe Minion Pro字体的正文内容。", normal));
content.add(new Chunk(" 这里是斜体文本。", italic));
content.setSpacingAfter(15);
document.add(content);
// 创建表格(使用等宽字体)
BaseFont monoFont = fontManager.getFont("SourceHanSansCN");
Font mono = new Font(monoFont, 10);
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
// 表头
table.addCell(createCell("项目", mono, true));
table.addCell(createCell("数量", mono, true));
table.addCell(createCell("价格", mono, true));
// 表格内容
table.addCell(createCell("产品A", mono, false));
table.addCell(createCell("10", mono, false));
table.addCell(createCell("$100.00", mono, false));
document.add(table);
document.add(new Paragraph(" ")); // 空行作为间距
Paragraph contentdoc = new Paragraph();
List tablelist =new ArrayList();
List list= TestWordRead.readDocxLinesWithInfo("D://22222.docx");
for ( Object textLine:list ){
TextLine text= (TextLine) textLine;
contentdoc.add(new Chunk(text.getText()+"\n", normal));
if(text.getLineType().indexOf("表格行")>-1){
tablelist.add(text.getText());
}
if(text.getLineType().equals("end表格行")){
if(tablelist.size()>0){
String [] headstr=tablelist.get(0).toString().split("\\|");
PdfPTable ftable = new PdfPTable(headstr.length);
for(int a=0;a<headstr.length;a++){
ftable.addCell(createCell(headstr[a], mono, true));
}
for (int i=1;i<tablelist.size();i++){
String [] rowstr=tablelist.get(i).toString().split("\\|");
for(int j=0;j<rowstr.length;j++){
ftable.addCell(createCell(rowstr[j], mono, false));
}
}
ftable.setWidthPercentage(100);
document.add(ftable);
document.add(new Paragraph(" ")); // 空行作为间距
tablelist.clear();
}
}
}
document.add(contentdoc);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static PdfPCell createCell(String text, Font font, boolean isHeader) {
PdfPCell cell = new PdfPCell(new Phrase(text, font));
cell.setPadding(5);
if (isHeader) {
cell.setBackgroundColor(new BaseColor(240, 240, 240));
}
return cell;
}
}
1万+

被折叠的 条评论
为什么被折叠?



