pom
< dependency>
< groupId> org.apache.pdfbox</ groupId>
< artifactId> pdfbox</ artifactId>
< version> 2.0.24</ version>
</ dependency>
读取属性
import org. apache. pdfbox. pdmodel. PDDocument ;
import org. apache. pdfbox. pdmodel. PDDocumentInformation ;
import java. io. File ;
import java. io. IOException ;
public class ReadPDFProperties {
public static void main ( String [ ] args) {
try {
File file = new File ( "example.pdf" ) ;
PDDocument document = PDDocument . load ( file) ;
PDDocumentInformation info = document. getDocumentInformation ( ) ;
String title = info. getTitle ( ) ;
String author = info. getAuthor ( ) ;
String subject = info. getSubject ( ) ;
String keywords = info. getKeywords ( ) ;
String creator = info. getCreator ( ) ;
String producer = info. getProducer ( ) ;
String creationDate = info. getCreationDate ( ) . toString ( ) ;
String modificationDate = info. getModificationDate ( ) . toString ( ) ;
String trapped = info. getTrapped ( ) ;
System . out. println ( "Title: " + title) ;
System . out. println ( "Author: " + author) ;
System . out. println ( "Subject: " + subject) ;
System . out. println ( "Keywords: " + keywords) ;
System . out. println ( "Creator: " + creator) ;
System . out. println ( "Producer: " + producer) ;
System . out. println ( "Creation Date: " + creationDate) ;
System . out. println ( "Modification Date: " + modificationDate) ;
System . out. println ( "Trapped: " + trapped) ;
document. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
设置属性
import org. apache. pdfbox. pdmodel. PDDocument ;
import org. apache. pdfbox. pdmodel. PDDocumentInformation ;
import java. io. File ;
import java. io. IOException ;
public class PDFPropertiesExample {
public static void main ( String [ ] args) {
try ( PDDocument document = PDDocument . load ( new File ( "example.pdf" ) ) ) {
PDDocumentInformation info = document. getDocumentInformation ( ) ;
info. setAuthor ( "作者姓名" ) ;
info. setTitle ( "文档标题" ) ;
info. setSubject ( "文档主题" ) ;
document. setDocumentInformation ( info) ;
document. save ( "updated_example.pdf" ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}