In dit korte how-to-artikel leert u hoe u links uit PDF in Java kunt extraheren. Het bevat de IDE-instellingen, een lijst met stappen en een voorbeeldcode om hyperlinks uit PDF in Java te extraheren. U leert hoe u linktype-annotaties kunt ophalen en deze kunt transformeren naar URIAction om de URI op te halen.
Stappen om URL uit PDF te extraheren in Java
- Stel de IDE in om Aspose.PDF for Java te gebruiken om links te extraheren
- Laad de bron-PDF file, doorloop alle pagina’s en maak een annotatieselector voor de pagina
- Haal alle aantekeningen van de pagina en sla ze op in de geselecteerde verzameling
- Loop door alle annotaties en typeer elke annotatie naar de GoToURIAction
- Roep de getURI()-methode aan om toegang te krijgen tot de link en deze op de console weer te geven
Deze handleiding heeft laten zien hoe u alle links uit PDF in Java kunt extraheren. Laad het bron-PDF-bestand, open de doelpagina’s en maak een annotatieselector voor elke pagina. Roep de accept()-methode aan met de gedefinieerde selector, haal de lijst met linkannotaties op en haal de URI op door deze te typecasten naar de klasse GoToURIAction.
Code om hyperlink uit PDF te extraheren in Java
import com.aspose.pdf.*; | |
import java.util.List; | |
public class Main { | |
public static void main(String[] args) throws Exception {//main() method for fetching URI | |
License license = new License();//Initialize the PDF license | |
license.setLicense("license.lic");//Apply the license | |
Document pdfDocument = new Document("PdfWithLinks.pdf");// Load hyperlinks PDF | |
// Iterate all the pages | |
for (int pageNumber = 1; pageNumber <= pdfDocument.getPages().size(); pageNumber++) { | |
System.out.println("Processing Page " + pageNumber);// Display the current page number | |
Page pdfPage = pdfDocument.getPages().get_Item(pageNumber);// Get the current page | |
// Create an annotation selector to find link annotations on the page | |
AnnotationSelector linkSelector = new AnnotationSelector(new LinkAnnotation(pdfPage, Rectangle.getTrivial())); | |
// Extract all annotations from the current page | |
pdfPage.accept(linkSelector); | |
// Retrieve the list of selected link annotations | |
List<Annotation> linkAnnotations = linkSelector.getSelected(); | |
// Iterate through each link annotation | |
for (Annotation annotation : linkAnnotations) { | |
// Check if the annotation is a LinkAnnotation and has actions | |
if (annotation instanceof LinkAnnotation) { | |
LinkAnnotation linkAnnotation = (LinkAnnotation) annotation; | |
// Check if the LinkAnnotation has any associated actions | |
if (linkAnnotation.getAction() instanceof GoToURIAction) { | |
// Cast the action to a GoToURIAction to access the URI | |
GoToURIAction uriAction = (GoToURIAction) linkAnnotation.getAction(); | |
// Display the extracted URI | |
System.out.println("Found URI: " + uriAction.getURI()); | |
} | |
} | |
} | |
} | |
// Indicate that the process is complete | |
System.out.println("URI extraction completed."); | |
} | |
} |
De bovenstaande code heeft een PDF-link-extractor in Java gedemonstreerd. U kunt een pagina overslaan of selecteren door de inhoud ervan te analyseren met behulp van het Page-klasseobject terwijl u door de pagina’s in de PDF itereert. De getAction()-methode wordt gebruikt om de URIAction op te halen die de URI van de link bevat.
In dit artikel hebben we het proces van het ophalen van hyperlinks uit een PDF geleerd. Om hyperlinks in een PDF te maken, raadpleeg het artikel over Hoe maak je een hyperlink in PDF met behulp van Java.