本文整理汇总了Java中org.apache.pdfbox.pdmodel.PDPage.getAnnotations方法的典型用法代码示例。如果您正苦于以下问题:Java PDPage.getAnnotations方法的具体用法?Java PDPage.getAnnotations怎么用?Java PDPage.getAnnotations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.PDPage
的用法示例。
在下文中一共展示了PDPage.getAnnotations方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRemoveLikeStephanImproved
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
/**
* <a href="https://stackoverflow.com/questions/45812696/pdfbox-delete-comment-maintain-strikethrough">
* PDFBox delete comment maintain strikethrough
* </a>
* <br/>
* <a href="https://expirebox.com/files/3d955e6df4ca5874c38dbf92fc43b5af.pdf">
* only_fields.pdf
* </a>
* <a href="https://file.io/DTvqhC">
* (alternative download)
* </a>
* <p>
* The OP only wanted the comment removed, not the strike-through. Thus, we must
* not remove the annotation but merely the comment building attributes.
* </p>
*/
@Test
public void testRemoveLikeStephanImproved() throws IOException {
final COSName POPUP = COSName.getPDFName("Popup");
try (InputStream resource = getClass().getResourceAsStream("only_fields.pdf")) {
PDDocument document = PDDocument.load(resource);
List<PDAnnotation> annotations = new ArrayList<>();
PDPageTree allPages = document.getDocumentCatalog().getPages();
List<COSObjectable> objectsToRemove = new ArrayList<>();
for (int i = 0; i < allPages.getCount(); i++) {
PDPage page = allPages.get(i);
annotations = page.getAnnotations();
for (PDAnnotation annotation : annotations) {
if ("StrikeOut".equals(annotation.getSubtype()))
{
COSDictionary annotationDict = annotation.getCOSObject();
COSBase popup = annotationDict.getItem(POPUP);
annotationDict.removeItem(POPUP);
annotationDict.removeItem(COSName.CONTENTS); // plain text comment
annotationDict.removeItem(COSName.RC); // rich text comment
annotationDict.removeItem(COSName.T); // author
if (popup != null)
objectsToRemove.add(popup);
}
}
annotations.removeAll(objectsToRemove);
}
document.save(new File(RESULT_FOLDER, "only_fields-removeImproved.pdf"));
}
}
示例2: build
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
public byte[] build() throws IOException {
this.acroForm.setNeedAppearances(false);
// Fix annotations
for (PDPage page : this.pdfDocument.getPages()) {
for (PDAnnotation annot : page.getAnnotations()) {
annot.setPage(page);
}
}
// Define font resources names used in PDF template
final PDResources dr = new PDResources();
dr.put(COSName.getPDFName("Helv"), PDType1Font.HELVETICA);
dr.put(COSName.getPDFName("HeBo"), PDType1Font.HELVETICA_BOLD);
this.acroForm.setDefaultResources(dr);
// Convert form fields to text
this.acroForm.flatten();
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
pdfDocument.save(bos);
pdfDocument.close();
return bos.toByteArray();
}
示例3: extractAttachments
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
public static void extractAttachments(String pdfPath, String extractPath) throws IOException {
PDDocument document = null;
try {
File input = new File(pdfPath);
String filePath = input.getParent() + System.getProperty("file.separator");
document = PDDocument.load(input);
PDDocumentNameDictionary namesDictionary =
new PDDocumentNameDictionary( document.getDocumentCatalog() );
PDEmbeddedFilesNameTreeNode efTree = namesDictionary.getEmbeddedFiles();
if (efTree != null) {
Map<String, PDComplexFileSpecification> names = efTree.getNames();
if (names != null) {
extractFiles(names, filePath);
} else {
List<PDNameTreeNode<PDComplexFileSpecification>> kids = efTree.getKids();
for (PDNameTreeNode<PDComplexFileSpecification> node : kids) {
names = node.getNames();
extractFiles(names, filePath);
};
};
};
for (PDPage page : document.getPages()) {
for (PDAnnotation annotation : page.getAnnotations()) {
if (annotation instanceof PDAnnotationFileAttachment) {
PDAnnotationFileAttachment annotationFileAttachment = (PDAnnotationFileAttachment) annotation;
PDComplexFileSpecification fileSpec = (PDComplexFileSpecification) annotationFileAttachment.getFile();
PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
extractFile(filePath, fileSpec.getFilename(), embeddedFile);
};
};
};
} finally {
};
}
示例4: removeWidgets
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
void removeWidgets(PDField targetField) throws IOException {
if (targetField instanceof PDTerminalField) {
List<PDAnnotationWidget> widgets = ((PDTerminalField)targetField).getWidgets();
for (PDAnnotationWidget widget : widgets) {
PDPage page = widget.getPage();
if (page != null) {
List<PDAnnotation> annotations = page.getAnnotations();
boolean removed = false;
for (PDAnnotation annotation : annotations) {
if (annotation.getCOSObject().equals(widget.getCOSObject()))
{
removed = annotations.remove(annotation);
break;
}
}
if (!removed)
System.out.println("Inconsistent annotation definition: Page annotations do not include the target widget.");
} else {
System.out.println("Widget annotation does not have an associated page; cannot remove widget.");
// TODO: In this case iterate all pages and try to find and remove widget in all of them
}
}
} else if (targetField instanceof PDNonTerminalField) {
List<PDField> childFields = ((PDNonTerminalField)targetField).getChildren();
for (PDField field : childFields)
removeWidgets(field);
} else {
System.out.println("Target field is neither terminal nor non-terminal; cannot remove widgets.");
}
}
示例5: testRemoveLikeStephan
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
/**
* <a href="https://stackoverflow.com/questions/45812696/pdfbox-delete-comment-maintain-strikethrough">
* PDFBox delete comment maintain strikethrough
* </a>
* <br/>
* <a href="https://expirebox.com/files/3d955e6df4ca5874c38dbf92fc43b5af.pdf">
* only_fields.pdf
* </a>
* <a href="https://file.io/DTvqhC">
* (alternative download)
* </a>
* <p>
* Due to a bug in the <code>COSArrayList</code> usage for page annotations,
* the indirect reference to the annotation in question is not removed from
* the actual page annotations array.
* </p>
*/
@Test
public void testRemoveLikeStephan() throws IOException {
try (InputStream resource = getClass().getResourceAsStream("only_fields.pdf")) {
PDDocument document = PDDocument.load(resource);
List<PDAnnotation> annotations = new ArrayList<>();
PDPageTree allPages = document.getDocumentCatalog().getPages();
for (int i = 0; i < allPages.getCount(); i++) {
PDPage page = allPages.get(i);
annotations = page.getAnnotations();
List<PDAnnotation> annotationToRemove = new ArrayList<PDAnnotation>();
if (annotations.size() < 1)
continue;
else {
for (PDAnnotation annotation : annotations) {
if (annotation.getContents() != null
&& annotation.getContents().equals("Sample Strikethrough")) {
annotationToRemove.add(annotation);
}
}
annotations.removeAll(annotationToRemove);
}
}
document.save(new File(RESULT_FOLDER, "only_fields-removeLikeStephan.pdf"));
}
}