Skip to content

Commit

Permalink
code updated for support PDFBox3, #311
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelligent2013 committed Dec 24, 2024
1 parent 5306fb1 commit dfc285b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
16 changes: 12 additions & 4 deletions src/main/java/org/metanorma/fop/annotations/Annotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,17 @@ private void fixAnnotationTags(COSArray oArray, COSObject parentObject, int leve

if (oArray != null) {
for(int i = 0; i < oArray.size(); i++) {
COSObject oArrayItem = (COSObject) oArray.get(i);
COSBase oBaseItem = oArrayItem.getObject();
COSDictionary dArrayItem = (COSDictionary) oBaseItem;
COSObject oArrayItem = null;
COSDictionary dArrayItem;
if (oArray.get(i) instanceof COSDictionary) {
//oArrayItem = (COSObject) oArray.get(i).getCOSObject();
dArrayItem = (COSDictionary) oArray.get(i);
} else {
oArrayItem = (COSObject) oArray.get(i);
COSBase oBaseItem = oArrayItem.getObject();
dArrayItem = (COSDictionary) oBaseItem;
}

COSName cName = (COSName) dArrayItem.getItem(COSName.S);

if (cName != null) {
Expand Down Expand Up @@ -466,7 +474,7 @@ private void fixAnnotationTags(COSArray oArray, COSObject parentObject, int leve
COSArray oA_K = (COSArray) dArrayItem.getItem(COSName.K);
fixAnnotationTags(oA_K, oArrayItem, ++level);
} catch (Exception e) {
//
//System.out.println(e.toString());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ public void process(File pdf) throws IOException {
}


/*finally {
if( document != null ) {
finally {
/*if( document != null ) {
document.close();
}
}*/
}*/
Files.deleteIfExists(pdf_tmp);
}

}

Expand Down
11 changes: 5 additions & 6 deletions src/test/java/org/metanorma/fop/mn2pdfTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.cos.COSName;

import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
import org.apache.pdfbox.pdmodel.common.filespecification.PDFileSpecification;
Expand Down Expand Up @@ -264,7 +263,7 @@ public void checkResultedPDF() throws ParseException {
String PDFkeywords = "";

//PDDocument doc;
try (PDDocument doc = Loader.loadPDF(new RandomAccessReadBufferedFile(pdf.toFile().getAbsoluteFile()))) {
try (PDDocument doc = Loader.loadPDF(pdf.toFile())) {
//doc = PDDocument.load(pdf.toFile());

PDPageTree pages = doc.getDocumentCatalog().getPages();
Expand Down Expand Up @@ -328,12 +327,12 @@ public void checkResultedEncryptedPDF() throws ParseException {
boolean encryptMetadata = false;

//PDDocument doc;
try (PDDocument doc = Loader.loadPDF(new RandomAccessReadBufferedFile(pdf.toFile().getAbsoluteFile()), "userpass")) {
try (PDDocument doc = Loader.loadPDF(pdf.toFile(), "userpass")) {
//doc = PDDocument.load(pdf.toFile(), "userpass");

AccessPermission ap = doc.getCurrentAccessPermission();
allowPrint = ap.canPrint();
allowPrintHQ = ap.canPrintFaithful();// canPrintDegraded() in 2.0.27;
allowPrintHQ = ap.canPrintFaithful();// ap.canPrintDegraded();
allowCopyContent = ap.canExtractContent();
allowEditContent = ap.canModify();
allowEditAnnotations = ap.canModifyAnnotations();
Expand Down Expand Up @@ -401,7 +400,7 @@ public void checkSpacesInPDF() throws ParseException {

String pdftext = "";
//PDDocument doc;
try (PDDocument doc = Loader.loadPDF(new RandomAccessReadBufferedFile(pdf.toFile().getAbsoluteFile()))) {
try (PDDocument doc = Loader.loadPDF(pdf.toFile())) {
//doc = PDDocument.load(pdf.toFile());
pdftext = new PDFTextStripper().getText(doc);
} catch (IOException ex) {
Expand All @@ -428,7 +427,7 @@ public void checkAttachments() throws ParseException {
//PDDocument doc;
int countFileAttachmentAnnotation = 0;
int countFileAttachmentEmbedded = 0;
try (PDDocument doc = Loader.loadPDF(new RandomAccessReadBufferedFile(pdf.toFile().getAbsoluteFile()))) {
try (PDDocument doc = Loader.loadPDF(pdf.toFile())) {
//doc = PDDocument.load(pdf.toFile());

int numberOfPages = doc.getNumberOfPages();
Expand Down

0 comments on commit dfc285b

Please sign in to comment.