How can I determine if a file is a PDF file?

后端 未结 13 906
暖寄归人
暖寄归人 2020-12-24 11:57

I am using PdfBox in Java to extract text from PDF files. Some of the input files provided are not valid and PDFTextStripper halts on these files. Is there a clean way to ch

13条回答
  •  执笔经年
    2020-12-24 12:29

    Since you use PDFBox you can simply do:

    PDDocument.load(file);
    

    It'll fail with an Exception if the PDF is corrupted etc.

    If it succeeds you can also check if the PDF is encrypted using .isEncrypted()

提交回复
热议问题