美文网首页
java获取pdf目录

java获取pdf目录

作者: 10676 | 来源:发表于2021-11-10 10:03 被阅读0次

方式一:

    public static void main ( String [] args ) throws Exception {
        String filePath = "E://opt//upFiles//《中国武术通史》上册.pdf";
        PdfReader reader = new PdfReader ( filePath) ;
        List list = SimpleBookmark.getBookmark ( reader ) ;
        for ( Iterator i = list.iterator () ; i.hasNext () ; ) {
            showBookmark (( Map ) i.next ()) ;
        }
    }

    private static void showBookmark ( Map bookmark ) {
        System.out.println(bookmark.get("Title"));
        System.out.println(bookmark);
        ArrayList kids = (ArrayList) bookmark.get("Kids");
        if (kids == null){
            return;
        }
        for ( Iterator i = kids.iterator () ; i.hasNext () ; ) {
            showBookmark (( Map ) i.next ()) ;
        }
    }

方式二:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;

public class Pdf {
    
    public void printBookmarks(PDOutlineNode bookmark,String indentation) throws IOException{
        PDOutlineItem current = bookmark.getFirstChild();
        while(current != null){
            int pages = 0;
            if(current.getDestination() instanceof PDPageDestination){
                PDPageDestination pd = (PDPageDestination) current.getDestination();
                pages = pd.retrievePageNumber() + 1;
            }
            if (current.getAction()  instanceof PDActionGoTo) {
                PDActionGoTo gta = (PDActionGoTo) current.getAction();
                if (gta.getDestination() instanceof PDPageDestination) {
                    PDPageDestination pd = (PDPageDestination) gta.getDestination();
                    pages = pd.retrievePageNumber() + 1;
                }
            }
            if (pages == 0) {
                System.out.println(indentation+current.getTitle());
            }else{
                System.out.println(indentation+current.getTitle()+"  "+pages);
            }
            printBookmarks( current, indentation + "    " );
            current = current.getNextSibling();
        }
    }
    public static void main(String[] args) throws IOException {
        File file = new File("/Users/Desktop/pdf/CB.pdf");
        PDDocument doc = null;
        FileInputStream  fis = null;
        try {
            fis = new FileInputStream(file);
            PDFParser parser = new PDFParser(new RandomAccessBuffer(fis));
            parser.parse();
            doc = parser.getPDDocument();
            PDDocumentOutline outline = doc.getDocumentCatalog().getDocumentOutline();
            Pdf pdf = new Pdf();
            if (outline != null) {
                pdf.printBookmarks(outline, "");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
    }
}
原文链接:https://blog.csdn.net/yangyang_yangqi/article/details/84833720

都可以,方式一清楚明了,方式二给我看的有点迷糊

包:

       <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>

或者:

        <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.4.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>jempbox</artifactId>
            <version>1.8.16</version>
        </dependency>

相关文章

网友评论

      本文标题:java获取pdf目录

      本文链接:https://www.haomeiwen.com/subject/ufubzltx.html