Document doc = new Document("C:\\Temp\\in.docx");
PdfSaveOptions opt = new PdfSaveOptions();
opt.setUpdateFields(false);
doc.save("C:\\Temp\\out.pdf", opt);
禁用更新某些类型的字段:
Document doc = new Document("C:\\Temp\\in.docx");
for (Field f : doc.getRange().getFields())
{
if (f.getType() == FieldType.FIELD_DOC_PROPERTY)
f.isLocked(true);
}
doc.save("C:\\Temp\\out.pdf");
将遗漏的属性添加到文档中:
Document doc = new Document("C:\\Temp\\in.docx");
for (Field f : doc.getRange().getFields())
{
if (f.getType() == FieldType.FIELD_DOC_PROPERTY)
{
String fieldCode = f.getFieldCode();
String[] fieldCodeParts = fieldCode.split("\\s+");
String propName = fieldCodeParts[1];
if (!doc.getBuiltInDocumentProperties().contains(propName) &&
!doc.getCustomDocumentProperties().contains(propName))
{
doc.getCustomDocumentProperties().add(propName, "");
}
}
}
doc.save("C:\\Temp\\out.pdf");