2016-07-26 37 views
1

Ich versuche, die Hyperlink-Informationen aus einer PDF-Datei mit PDFBox zu extrahieren, aber ich bin nicht sicher, wieWie Hyperlink-Informationen extrahieren PDFBox

for(Object p : pages) { 
    PDPage page = (PDPage)p; 

    List<?> annotations = page.getAnnotations(); 
    for(Object a : annotations) { 
     PDAnnotation annotation = (PDAnnotation)a; 

     if(annotation instanceof PDAnnotationLink) { 
      PDAnnotationLink link = (PDAnnotationLink)annotation; 
      System.out.println(link.toString()); 
      System.out.println(link.getDestination()); 

     } 
    } 

} 

bekommen ich die URL des Hyperlinks Ziel extrahieren möchten und den Text der Hyperlink. Wie kann man das machen?

Dank

Antwort

2

Verwenden Sie diesen Code aus dem PrintURLs sample code aus dem Download-Quellcode:

for(PDPage page : doc.getPages()) 
{ 
    pageNum++; 
    PDFTextStripperByArea stripper = new PDFTextStripperByArea(); 
    List<PDAnnotation> annotations = page.getAnnotations(); 
    //first setup text extraction regions 
    for(int j=0; j<annotations.size(); j++) 
    { 
     PDAnnotation annot = annotations.get(j); 
     if(annot instanceof PDAnnotationLink) 
     { 
      PDAnnotationLink link = (PDAnnotationLink)annot; 
      PDRectangle rect = link.getRectangle(); 
      //need to reposition link rectangle to match text space 
      float x = rect.getLowerLeftX(); 
      float y = rect.getUpperRightY(); 
      float width = rect.getWidth(); 
      float height = rect.getHeight(); 
      int rotation = page.getRotation(); 
      if(rotation == 0) 
      { 
       PDRectangle pageSize = page.getMediaBox(); 
       y = pageSize.getHeight() - y; 
      } 
      else if(rotation == 90) 
      { 
       //do nothing 
      } 

      Rectangle2D.Float awtRect = new Rectangle2D.Float(x,y,width,height); 
      stripper.addRegion("" + j, awtRect); 
     } 
    } 

    stripper.extractRegions(page); 

    for(int j=0; j<annotations.size(); j++) 
    { 
     PDAnnotation annot = annotations.get(j); 
     if(annot instanceof PDAnnotationLink) 
     { 
      PDAnnotationLink link = (PDAnnotationLink)annot; 
      PDAction action = link.getAction(); 
      String urlText = stripper.getTextForRegion("" + j); 
      if(action instanceof PDActionURI) 
      { 
       PDActionURI uri = (PDActionURI)action; 
       System.out.println("Page " + pageNum +":'" + urlText.trim() + "'=" + uri.getURI()); 
      } 
     } 
    } 
} 

Es in zwei Teilen arbeitet, wird man die URL bekommen, die einfach ist, wird der andere die URL Text bekommen , was mit einer Textextraktion im Rechteck der Annotation geschieht.

+0

Dieses Stück Code schön extrahiert die externen Links auf der PDF. Aber es scheint, als würde es keine Links zu internen Seiten extrahieren. Als Beispiel, auf der dritten Seite meines PDFs, enthält es einen Link zu Seite 10. Ich muss diese Information auch bekommen. Irgendeine Idee, wie es geht? –

+0

@ShiranSEkanayake Bitte schauen Sie sich die andere Antwort an. Der untere Teil (mit PDPageDestination) sollte tun, was Sie wollen. Ich habe es nicht getestet, aber es sieht gut aus. –

+0

Danke. Es klappt!! –

1

Wir müssen Hyperlink-Informationen und interner Link (z. B. Seite verschieben ....). Ich benutze Code unten:

int pageNum = 0; 
      for (PDPage page : originalPDF.getPages()) { 
       pageNum++; 
       List<PDAnnotation> annotations = page.getAnnotations(); 
       for (PDAnnotation annot : annotations) { 
        if (annot instanceof PDAnnotationLink) { 
         // get dimension of annottations 
         PDAnnotationLink link = (PDAnnotationLink) annot; 
         // get link action include link url and internal link 
         PDAction action = link.getAction(); 
         // get link internal some case specal 
         PDDestination pDestination = link.getDestination(); 

         if (action != null) { 
          if (action instanceof PDActionURI || action instanceof PDActionGoTo) { 
           if (action instanceof PDActionURI) { 
            // get uri link 
            PDActionURI uri = (PDActionURI) action; 
            System.out.println("uri link:" + uri.getURI()); 
           } else { 
            if (action instanceof PDActionGoTo) { 
             // get internal link 
             PDDestination destination = ((PDActionGoTo) action).getDestination(); 
             PDPageDestination pageDestination; 
             if (destination instanceof PDPageDestination) { 
              pageDestination = (PDPageDestination) destination; 
             } else { 
              if (destination instanceof PDNamedDestination) { 
               pageDestination = originalPDF.getDocumentCatalog().findNamedDestinationPage((PDNamedDestination) destination); 
              } else { 
               // error handling 
               break; 
              } 
             } 

             if (pageDestination != null) { 
              System.out.println("page move: " + (pageDestination.retrievePageNumber() + 1)); 
             } 
            } 
           } 
          } 
         } else { 
          if (pDestination != null) { 
           PDPageDestination pageDestination; 
           if (pDestination instanceof PDPageDestination) { 
            pageDestination = (PDPageDestination) pDestination; 
           } else { 
            if (pDestination instanceof PDNamedDestination) { 
             pageDestination = originalPDF.getDocumentCatalog().findNamedDestinationPage((PDNamedDestination) pDestination); 
            } else { 
             // error handling 
             break; 
            } 
           } 

           if (pageDestination != null) { 
            System.out.println("page move: " + (pageDestination.retrievePageNumber() + 1)); 
           } 
          } else { 
           //  
          } 
         } 
        } 
       } 

      }