如何通过PDFBox直接将PDF注释导出为可导入的FDF格式?
Export PDF Annotations to FDF in One Step with PDFBox
Hey Hubert, I totally get where you're coming from—using the official ExportFDF.java tool can be frustrating because it's built primarily for exporting form fields, not annotations. That's exactly why your exported FDF doesn't include those comment notes. But good news: you don't need that two-step TXT workaround. You can directly extract annotations and generate a valid FDF file in one go using PDFBox's core APIs.
Here's a complete, working code example:
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationText; import org.apache.pdfbox.fdf.FDFDocument; import org.apache.pdfbox.fdf.FDFPage; import org.apache.pdfbox.fdf.FDFAnnotation; import java.io.File; import java.io.IOException; public class ExportAnnotationsToFDF { public static void main(String[] args) { String pdfPath = "path/to/your/input.pdf"; String fdfPath = "path/to/your/output.fdf"; try (PDDocument pdfDoc = PDDocument.load(new File(pdfPath)); FDFDocument fdfDoc = new FDFDocument()) { // Iterate over each page in the PDF for (PDPage page : pdfDoc.getPages()) { FDFPage fdfPage = new FDFPage(); fdfPage.setPageIndex(pdfDoc.getPages().indexOf(page)); // Extract all annotations from the current page for (PDAnnotation annotation : page.getAnnotations()) { // Handle text comments first (extend to other types as needed) if (annotation instanceof PDAnnotationText) { PDAnnotationText textAnnot = (PDAnnotationText) annotation; FDFAnnotation fdfAnnot = new FDFAnnotation(); // Map core annotation properties to FDF fdfAnnot.setType("Text"); fdfAnnot.setRectangle(textAnnot.getRectangle().getBounds2D()); fdfAnnot.setContents(textAnnot.getContents()); fdfAnnot.setPageIndex(pdfDoc.getPages().indexOf(page)); // Add optional properties if available if (textAnnot.getAuthor() != null) { fdfAnnot.setAuthor(textAnnot.getAuthor()); } fdfPage.getAnnotations().add(fdfAnnot); } // Add support for other annotation types here (e.g., highlights) // else if (annotation instanceof PDAnnotationHighlight) { // PDAnnotationHighlight highlight = (PDAnnotationHighlight) annotation; // FDFAnnotation fdfAnnot = new FDFAnnotation(); // fdfAnnot.setType("Highlight"); // // Map additional properties like quad points // } } if (!fdfPage.getAnnotations().isEmpty()) { fdfDoc.getCatalog().getPages().add(fdfPage); } } // Save the final FDF document fdfDoc.save(new File(fdfPath)); System.out.println("FDF with annotations exported successfully!"); } catch (IOException e) { e.printStackTrace(); } } }
Key Details to Know:
- Why this works: Unlike the
ExportFDFtool, this code directly accesses the PDF's annotation layer viaPDPage.getAnnotations(), then maps each annotation to anFDFAnnotationobject that fits the FDF specification. - Extend to other annotation types: The example focuses on text comments, but you can easily add handling for highlights, underlines, stamps, etc., by checking the annotation class and setting the corresponding
TypeinFDFAnnotation. - Compatibility: The generated FDF will work with most PDF readers (like Adobe Acrobat) that support importing FDF annotations, since it adheres to standard FDF structure rules.
Why this beats your two-step approach:
- Eliminates intermediate TXT files, so you avoid potential data loss or formatting glitches.
- Directly maps annotation properties to FDF's native format, ensuring accuracy.
- Easier to maintain and expand for different annotation types down the line.
Give this code a shot—you’ll get a fully populated FDF with all your annotations in one clean step!
内容的提问来源于stack exchange,提问作者HubertWu




