本文整理汇总了Java中org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation类的典型用法代码示例。如果您正苦于以下问题:Java PDAnnotation类的具体用法?Java PDAnnotation怎么用?Java PDAnnotation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PDAnnotation类属于org.apache.pdfbox.pdmodel.interactive.annotation包,在下文中一共展示了PDAnnotation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRemoveLikeStephanImproved
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的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.interactive.annotation.PDAnnotation; //导入依赖的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.interactive.annotation.PDAnnotation; //导入依赖的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.interactive.annotation.PDAnnotation; //导入依赖的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.interactive.annotation.PDAnnotation; //导入依赖的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"));
}
}
示例6: createRollover
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的package包/类
void createRollover(PDAnnotation annotation, String filename) throws IOException, COSVisitorException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
List<PDAnnotation> annotations = page.getAnnotations();
float x = 100;
float y = 500;
String text = "PDFBox";
PDFont font = PDType1Font.HELVETICA_BOLD;
float textWidth = font.getStringWidth(text) / 1000 * 18;
PDPageContentStream contents = new PDPageContentStream(document, page);
contents.beginText();
contents.setFont(font, 18);
contents.moveTextPositionByAmount(x, y);
contents.drawString(text);
contents.endText();
contents.close();
PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
PDAppearanceStream normal = createAppearanceStream(document, textWidth, font, "0.5 0.5 0.5 rg");
PDAppearanceStream rollover = createAppearanceStream(document, textWidth, font, "1 0.7 0.5 rg");
PDAppearanceStream down = createAppearanceStream(document, textWidth, font, "0 0 0 rg");
appearanceDictionary.setNormalAppearance(normal);
appearanceDictionary.setRolloverAppearance(rollover);
appearanceDictionary.setDownAppearance(down);
annotation.setAppearance(appearanceDictionary);
PDRectangle position = new PDRectangle();
position.setLowerLeftX(x);
position.setLowerLeftY(y - 5);
position.setUpperRightX(x + textWidth);
position.setUpperRightY(y + 20);
annotation.setRectangle(position);
annotations.add(annotation);
document.save(new File(RESULT_FOLDER, filename));
document.close();
}
示例7: isSupportedAnnotationType
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的package包/类
private boolean isSupportedAnnotationType(PDAnnotation annotation) {
if (annotation.getSubtype() == null) {
return false;
}
if ("Link".equals(annotation.getSubtype()) || "Widget".equals(annotation.getSubtype())) {
LOGGER.debug(annotation.getSubtype() + " is excluded from the supported file annotations");
return false;
}
try {
if (!Arrays.asList(FileAnnotationType.values()).contains(FileAnnotationType.valueOf(annotation.getSubtype()))) {
return false;
}
} catch (IllegalArgumentException e) {
LOGGER.debug(String.format("Could not parse the FileAnnotation %s into any known FileAnnotationType. It was %s!", annotation, annotation.getSubtype()));
}
return true;
}
示例8: createMarkedAnnotations
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的package包/类
private FileAnnotation createMarkedAnnotations(int pageIndex, PDPage page, PDAnnotation annotation) {
FileAnnotation annotationBelongingToMarking = new FileAnnotation(
annotation.getDictionary().getString(COSName.T), FileAnnotation.extractModifiedTime(annotation.getModifiedDate()),
pageIndex + 1, annotation.getContents(), FileAnnotationType.valueOf(annotation.getSubtype().toUpperCase(Locale.ROOT)), Optional.empty());
if (annotationBelongingToMarking.getAnnotationType().isLinkedFileAnnotationType()) {
try {
COSArray boundingBoxes = (COSArray) annotation.getDictionary().getDictionaryObject(COSName.getPDFName("QuadPoints"));
annotation.setContents(new TextExtractor(page, boundingBoxes).extractMarkedText());
} catch (IOException e) {
annotation.setContents("JabRef: Could not extract any marked text!");
}
}
//Marked text that has a sticky note on it should be linked to the sticky note
return new FileAnnotation(annotation, pageIndex + 1, annotationBelongingToMarking);
}
示例9: nonBlockingAnnotations
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的package包/类
private List<PDAnnotation> nonBlockingAnnotations(List<PDAnnotation> annotations) {
//filters out annotations that pdfbox draws poorly so they don't blot the text out and
//make the images hard to see. This includes hightlight textMarkups and Popups
List<PDAnnotation> annotationsThatAreNotTextMarkupOrPopup = new ArrayList<>();
for(PDAnnotation annotation: annotations) {
if (annotation instanceof PDAnnotationTextMarkup) {
if (!PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT.equals(annotation.getSubtype())) {
annotationsThatAreNotTextMarkupOrPopup.add(annotation);
}
}
else if (annotation.getClass() == PDAnnotationMarkup.class || annotation.getClass() == PDAnnotationRubberStamp.class) {
annotationsThatAreNotTextMarkupOrPopup.add(annotation);
}
}
return annotationsThatAreNotTextMarkupOrPopup;
}
示例10: sanitizeAnnotation
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的package包/类
public void sanitizeAnnotation(BleachSession session, PDAnnotation annotation) {
if (annotation instanceof PDAnnotationLink) {
sanitizeLinkAnnotation(session, (PDAnnotationLink) annotation);
}
if (annotation instanceof PDAnnotationWidget) {
sanitizeWidgetAnnotation(session, (PDAnnotationWidget) annotation);
}
}
示例11: determineSafe
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的package包/类
int determineSafe(PDDocument document, PDAnnotationWidget widget) throws IOException
{
COSDictionary widgetObject = widget.getCOSObject();
PDPageTree pages = document.getPages();
for (int i = 0; i < pages.getCount(); i++)
{
for (PDAnnotation annotation : pages.get(i).getAnnotations())
{
COSDictionary annotationObject = annotation.getCOSObject();
if (annotationObject.equals(widgetObject))
return i;
}
}
return -1;
}
示例12: extractAnnotationImages
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的package包/类
public void extractAnnotationImages(PDPage page, String pageFormat) throws IOException
{
List<PDAnnotation> annotations = page.getAnnotations();
if (annotations == null)
return;
for (int i = 0; i < annotations.size(); i++)
{
PDAnnotation annotation = annotations.get(i);
String annotationFormat = annotation.getAnnotationName() != null && annotation.getAnnotationName().length() > 0
? String.format(pageFormat, "-" + annotation.getAnnotationName() + "%s", "%s")
: String.format(pageFormat, "-" + i + "%s", "%s");
extractAnnotationImages(annotation, annotationFormat);
}
}
示例13: showAnnotation
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的package包/类
/**
* Custom annotation rendering.
*/
@Override
public void showAnnotation(PDAnnotation annotation) throws IOException {
// save
saveGraphicsState();
// 35% alpha
getGraphicsState().setNonStrokeAlphaConstants(0.35);
super.showAnnotation(annotation);
// restore
restoreGraphicsState();
}
示例14: importAnnotations
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的package包/类
/**
* Imports the comments from a pdf specified by its path
*
* @param path a path to a pdf
* @return a list with the all the annotations found in the file of the path
*/
@Override
public List<FileAnnotation> importAnnotations(final Path path) {
if (!validatePath(path)) {
// Path could not be validated, return default result
return Collections.emptyList();
}
List<FileAnnotation> annotationsList = new LinkedList<>();
try (PDDocument document = PDDocument.load(path.toString())) {
List pdfPages = document.getDocumentCatalog().getAllPages();
for (int pageIndex = 0; pageIndex < pdfPages.size(); pageIndex++) {
PDPage page = (PDPage) pdfPages.get(pageIndex);
for (PDAnnotation annotation : page.getAnnotations()) {
if (!isSupportedAnnotationType(annotation)) {
continue;
}
if (FileAnnotationType.isMarkedFileAnnotationType(annotation.getSubtype())) {
annotationsList.add(createMarkedAnnotations(pageIndex, page, annotation));
} else {
FileAnnotation fileAnnotation = new FileAnnotation(annotation, pageIndex + 1);
if (fileAnnotation.getContent() != null && !fileAnnotation.getContent().isEmpty()) {
annotationsList.add(fileAnnotation);
}
}
}
}
} catch (IOException e) {
LOGGER.error(String.format("Failed to read file '%s'.", path), e);
}
return annotationsList;
}
示例15: getPDFComments
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; //导入依赖的package包/类
public List<PdfComment> getPDFComments() {
List<PdfComment> comments = new ArrayList<>();
@SuppressWarnings("unchecked")
List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
for (PDPage page : pages) {
try {
pageImage = null;
List<PDAnnotation> annotations = page.getAnnotations();
// erase highlight and popup annotations from page to avoid them blotting out text
page.setAnnotations(nonBlockingAnnotations(annotations));
int size = Math.round(DEFAULT_SIZE * SCALE_UP_FACTOR);
for (PDAnnotation anno : annotations) {
if (pageImage == null) {
pageImage = page.convertToImage(BufferedImage.TYPE_INT_RGB, size);
}
PdfComment pdfComment = turnAnnotationIntoPDFComment(anno);
if (pdfComment != null) {
comments.add(pdfComment);
}
}
// restore annotations
page.setAnnotations(annotations);
} catch (IOException e) {
e.printStackTrace();
}
}
return comments;
}