Skip to content

Commit

Permalink
pcorlessGH-233 document rewrite (pcorless#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcorless committed Jul 23, 2023
1 parent dac27f1 commit d1cbfb2
Show file tree
Hide file tree
Showing 49 changed files with 1,286 additions and 370 deletions.
18 changes: 14 additions & 4 deletions core/core-awt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ dependencies {
implementation 'org.bouncycastle:bcprov-ext-jdk15on:' + "${BOUNCY_VERSION}"
implementation 'org.bouncycastle:bcpkix-jdk15on:' + "${BOUNCY_VERSION}"
// tiff, jpeg2000 and jbig decoding
implementation 'com.twelvemonkeys.imageio:imageio-tiff:' + "${MONKEY_VERSION}"
implementation 'com.twelvemonkeys.imageio:imageio-tiff:' + "${MONKEY_VERSION}"
implementation 'org.apache.pdfbox:jbig2-imageio:' + "${JBIG2_VERSION}"
implementation 'org.apache.pdfbox:fontbox:' + "${FONT_BOX_VERSION}"
implementation 'commons-logging:commons-logging:1.2'
runtimeOnly 'com.github.jai-imageio:jai-imageio-jpeg2000:' + "${JAI_VERSION}"
// tests
testImplementation(platform("org.junit:junit-bom:${JUNIT_BOM_VERSION}"))
testImplementation('org.junit.jupiter:junit-jupiter')
}

// generatePomFileForCoreJarPublication
Expand Down Expand Up @@ -99,18 +102,25 @@ compileJava {
def jenkinsBuild = System.getenv("BUILD_NUMBER") ?: "0"
def jenkinsRevision = System.getenv("SVN_REVISION") ?: "0"

copy{
copy {
from 'src/main/resources/org/icepdf/core/application/ProductInfo.java'
into 'src/main/java/org/icepdf/core/application/'
filter(org.apache.tools.ant.filters.ReplaceTokens,
tokens: [company: COMPANY, product: PRODUCT, version: VERSION, type: RELEASE_TYPE,
build: jenkinsBuild, revision: jenkinsRevision])
build : jenkinsBuild, revision: jenkinsRevision])
}
}
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}

clean {
doLast{
doLast {
file("src/main/java/org/icepdf/core/application/ProductInfo.java").delete()
}
}
12 changes: 12 additions & 0 deletions core/core-awt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -70,6 +75,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ public int getInt(Name key) {
return library.getInt(entries, key);
}

public Reference getReference(Name key) {
Object o = entries.get(key);
if (o instanceof Reference) {
return (Reference) o;
}
return null;
}

public List getList(Name key) {
Object o = getObject(key);
if (o instanceof List) {
Expand Down
76 changes: 54 additions & 22 deletions core/core-awt/src/main/java/org/icepdf/core/pobjects/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@
import org.icepdf.core.pobjects.structure.Trailer;
import org.icepdf.core.util.Defs;
import org.icepdf.core.util.Library;
import org.icepdf.core.util.updater.IncrementalUpdater;
import org.icepdf.core.util.updater.DocumentBuilder;
import org.icepdf.core.util.updater.WriteMode;
import org.icepdf.core.util.updater.modifiers.ModifierFactory;
import org.icepdf.core.util.updater.modifiers.PageRemovalModifier;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
Expand Down Expand Up @@ -119,7 +120,7 @@ public static String getLibraryVersion() {
// sets if file caching is enabled or disabled.
isCachingEnabled =
Defs.sysPropertyBoolean("org.icepdf.core.streamcache.enabled",
false);
true);
}

/**
Expand Down Expand Up @@ -346,7 +347,7 @@ private void setInputStream(ByteBuffer input)
input = header.parseHeader(input);

library.setDocumentByteBuffer(input);
library.setFileVersion(header.getVersion());
library.setFileHeader(header);

// create instance of CrossReferenceRoot as we may need it the trailer can't be correctly decoded.
crossReferenceRoot = new CrossReferenceRoot(library);
Expand All @@ -365,13 +366,13 @@ private void setInputStream(ByteBuffer input)
crossReferenceRoot.initialize(input);
library.setCrossReferenceRoot(crossReferenceRoot);
} catch (Exception e) {
crossReferenceRoot.setLazyInitializationFailed(true);
crossReferenceRoot.setInitializationFailed(true);
logger.log(Level.WARNING, "Cross reference loading failed, reindexing file.", e);
}
}

// linear traversal of file.
if (trailer.isLazyInitializationFailed() || crossReferenceRoot.isLazyInitializationFailed()) {
if (trailer.isLazyInitializationFailed() || crossReferenceRoot.isInitializationFailed()) {
crossReferenceRoot = library.rebuildCrossReferenceTable();
library.setCrossReferenceRoot(crossReferenceRoot);
}
Expand Down Expand Up @@ -558,23 +559,32 @@ public void dispose() {
* @throws IOException if there is some problem reading or writing the PDF data
*/
public long writeToOutputStream(OutputStream out) throws IOException {
return writeToOutputStream(out, WriteMode.INCREMENT_UPDATE);
}

/**
* Takes the internal PDF data, which may be in a file or in RAM,
* and write it to the provided OutputStream.
* The OutputStream is not flushed or closed, in case this method's
* caller requires otherwise.
*
* @param out OutputStream to which the PDF file bytes are written.
* @return The length of the PDF file copied
* @throws IOException if there is some problem reading or writing the PDF data
*/
public long writeToOutputStream(OutputStream out, WriteMode writeMode) throws IOException {
if (documentFileChannel != null) {
synchronized (library.getMappedFileByteBufferLock()) {
ByteBuffer documentByteBuffer = library.getMappedFileByteBuffer();
documentByteBuffer.position(0);
int documentLength = documentByteBuffer.remaining();
long appendedLength;
try (WritableByteChannel channel = Channels.newChannel(out)) {
channel.write(documentByteBuffer);
appendedLength = new IncrementalUpdater().appendIncrementalUpdate(
this,
out,
documentLength);
} catch (IOException e) {
logger.log(Level.FINE, "Error writing PDF output stream.", e);
throw e;
}
return documentLength + appendedLength;
long newDocumentLength = new DocumentBuilder().createDocument(
writeMode,
this,
library.getMappedFileByteBuffer(),
out,
documentLength);
return newDocumentLength;
}
} else {
return 0;
Expand All @@ -583,15 +593,27 @@ public long writeToOutputStream(OutputStream out) throws IOException {

/**
* Copies the pre-existing PDF file, and appends an incremental update for
* any edits, to the specified OutputStream. For the pre-existing PDF
* content copying, writeToOutputStream(OutputStream out) is used.
* any edits, to the specified OutputStream.
*
* @param out OutputStream to which the PDF file bytes are written.
* @return The length of the PDF file saved
* @throws IOException if there is some problem reading or writing the PDF data
*/
public long saveToOutputStream(OutputStream out) throws IOException {
return writeToOutputStream(out);
return writeToOutputStream(out, WriteMode.INCREMENT_UPDATE);
}

/**
* Copies the pre-existing PDF file, and applies any updates to the specified OutputStream using the specified
* write model.
*
* @param out OutputStream to which the PDF file bytes are written.
* @param writeMode write mode used to update the file with changes.
* @return The length of the PDF file saved
* @throws IOException if there is some problem reading or writing the PDF data
*/
public long saveToOutputStream(OutputStream out, WriteMode writeMode) throws IOException {
return writeToOutputStream(out, writeMode);
}

/**
Expand Down Expand Up @@ -713,6 +735,16 @@ public PInfo getInfo() {
return pTrailer.getInfo();
}

public void deletePage(Page page) {
if (page == null) {
throw new IllegalStateException("Page must not be null");
}
PageRemovalModifier pageRemovalModifier = (PageRemovalModifier) ModifierFactory.getModifier(catalog, page);
if (pageRemovalModifier != null) {
pageRemovalModifier.modify(page);
}
}

/**
* Returns the document's information or create and set it if it doesn't exist
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public PObject(Object object, Reference objectReference) {
* Gets the reference information for this PDF object.
*
* @return Reference object which contains the PDF objects
* number and generation data
* number and generation data
*/
public Reference getReference() {
return objectReference;
Expand Down Expand Up @@ -117,6 +117,6 @@ public void setLinearTraversalOffset(int linearTraversalOffset) {
* @return string representation of this object
*/
public String toString() {
return objectReference.toString() + " " + object.toString();
return objectReference.toString() + " " + (object != null ? object.toString() : null);
}
}
Loading

0 comments on commit d1cbfb2

Please sign in to comment.