//方法一,去除所有格式转换
FileOutputStream os = new FileOutputStream(targetFile);
Document doc = new Document(sourceFile);
ParagraphFormat pf = doc.getStyles().getDefaultParagraphFormat();
pf.clearFormatting();
doc.save(os, SaveFormat.PDF);
//方法二,重新新增样式模板再导出
FileOutputStream os = new FileOutputStream(targetFile);
Document doc = new Document(sourceFile);
DocumentBuilder builder = new DocumentBuilder(doc);
//新建再复制样式
Document document = new Document();
document.removeAllChildren();
document.appendDocument(doc,ImportFormatMode.USE_DESTINATION_STYLES;
document.save(os, SaveFormat.PDF);
//方法三,对word文档主体设置各个参数,具体可以看官方api查看
FileOutputStream os = new FileOutputStream(targetFile);
Document doc = new Document(sourceFile);
DocumentBuilder builder = new DocumentBuilder(doc);
//文档主体内容设置段后和行距
builder.moveToDocumentStart();
builder.getParagraphFormat().setLeftIndent(12);
builder.getParagraphFormat().setSpaceAfter(0);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getParagraphFormat().setLineSpacing(12);
builder.getParagraphFormat().setSpaceAfter(0);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getParagraphFormat().setLineSpacing(12);
builder.getParagraphFormat().setSpaceAfter(0);
builder.moveToDocumentStart();
document.save(os, SaveFormat.PDF);
//方法四,网上查的自定义宽高,以及取消隐藏数据的方法
FileOutputStream os = new FileOutputStream(targetFile);
Document doc = new Document(sourceFile);
NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
for (Table table : (Iterable<Table>) tables) {
double tableWidth = 0;
double tableHeight = 0;
Section section = (Section) table.getAncestor(NodeType.SECTION);
for (Row row : table.getRows()) {
double rowWidth = 0;
boolean composite = table.isComposite();
if (!composite){
table.remove();
continue;
}
double height = row.getRowFormat().getHeight();
tableHeight += height;
for (Cell cell : row.getCells()) {
for(Run run : (Iterable<Run>) cell.getChildNodes(NodeType.RUN, true))
{
String text = run.getText();
String name = run.getFont().getName();
boolean hidden = run.getFont().getHidden();
if (hidden){
run.setText("");
}
int color = run.getFont().getColor().getRGB();
int backgroundPattern = run.getFont().getShading().getBackgroundPatternColor().getRGB();
if (color == backgroundPattern){
// run.setText("");
}
}
rowWidth += cell.getCellFormat().getWidth();
cell.getCellFormat().setFitText(true);
}
if (rowWidth > tableWidth) {
tableWidth = rowWidth;
}
}
//Calculate the width of the page
double pageWidth = section.getPageSetup().getPageWidth() - (section.getPageSetup().getLeftMargin() + section.getPageSetup().getRightMargin());
//In the second run set each cell in the row proportionally to the width of the page
for(Row row : table.getRows()) {
for(Cell cell : row.getCells())
{
//Calculate the ratio of each cell to the row width and then translate this ratio to the page width.
double cellRatio = cell.getCellFormat().getWidth() / tableWidth;
cell.getCellFormat().setWidth(cellRatio * pageWidth);
}
if (row.getRowFormat().getHeight() > tableHeight ){
continue;
}
double height = row.getRowFormat().getHeight() / tableHeight;
row.getRowFormat().setHeight(height * tableHeight);
}
}
document.save(os, SaveFormat.PDF);