这个简短的教程指导如何在 Java 中从 PDF 中删除元数据。它包含运行应用程序所需资源的参考、编程步骤列表以及在 Java 中剥离 PDF 元数据的可运行示例代码。如果需要,您将练习单独删除默认或自定义元数据的方法。
使用 Java 清理 PDF 元数据的步骤
- 将 IDE 设置为使用 Aspose.PDF for Java 删除元数据
- 使用 Document 类对象加载目标 PDF 文件以剥离元数据
- 使用 DocumentInfo 类对象获取文档元数据
- 调用clear()方法删除默认元数据
- 使用clearCustomData()方法删除自定义元数据(如果有)
- 删除元数据后保存输出 PDF 文件
这些步骤总结了在 Java 中删除 PDF 元数据的过程。首先,加载源 PDF 文件并提取其元数据。接下来,调用clear()和clearCustomData()方法来清除元数据。
Java 中删除 PDF 元数据的代码
import com.aspose.pdf.*; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Remove meta data in Java | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Load the PDF | |
Document pdfForMeta = new Document("sample.pdf"); | |
// Fetch document info | |
DocumentInfo info = new DocumentInfo(pdfForMeta); | |
// Clear all metadata from the PDF | |
info.clear(); | |
info.clearCustomData(); | |
// Save the output document with no metadata | |
pdfForMeta.save("PDFWithoutMetadata.pdf"); | |
System.out.println("Done"); | |
} | |
} |
此示例代码演示了如何在 Java 中清理 PDF 元数据。它演示了分别使用clear()和clearCustomData()方法删除默认和自定义属性的内置方法。如果您只想删除选定的元数据,您可以清除 DocumentInfo 对象中所需的属性,或者根据需要调用 DocumentInfo 类中的remove() 或removeItemByKey() 方法。
此示例代码演示了使用 Java 从 PDF 中删除元数据的过程。如果您想从 PDF 中删除水印,请参阅 Java去除PDF文件水印的方法 上的文章。