Skip to content

Commit

Permalink
close #397 SetMetadata: batch support, add an option that doesn't upd…
Browse files Browse the repository at this point in the history
…ate creator/producer/modified date, to support scenarios such as "add extra field to a batch of files" without side-effects
  • Loading branch information
ediweissmann committed Mar 15, 2021
1 parent 8e6aaa6 commit 41b0621
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@
<jdepend.version>2.9.1</jdepend.version>
<hibernate-validator.version>4.2.0.Final</hibernate-validator.version>
<hamcrest.version>1.3</hamcrest.version>
<sambox.version>2.2.13</sambox.version>
<sambox.version>2.2.15</sambox.version>
<sejda.io.version>2.1.3</sejda.io.version>
<bouncycastle.version>1.66</bouncycastle.version>
<twelvemonkeys.version>3.4.2</twelvemonkeys.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ public void accept(PDDocument document) {
}
});
}

@Test
public void doesNotChangeProducerAndModificationDate() throws IOException {
SetMetadataParameters parameters = new SetMetadataParameters();
parameters.put(PdfMetadataFields.AUTHOR, "test_author");
parameters.addSource(mediumInput());

parameters.setUpdateCreatorProducerModifiedDate(false);

parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE);
testContext.directoryOutputTo(parameters);
execute(parameters);

testContext.assertTaskCompleted();
testContext.forEachPdfOutput(new Consumer<PDDocument>() {
@Override
public void accept(PDDocument document) {
PDDocumentInformation info = document.getDocumentInformation();
assertEquals("iText 2.1.7 by 1T3XT", info.getProducer());
assertNull(info.getCreator());
assertEquals(DateConverter.toCalendar("D:20111010235709+02'00'"), info.getModificationDate());
}
});
}

private void doExecute() throws IOException {
testContext.directoryOutputTo(parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.sejda.commons.collection.NullSafeSet;
import org.sejda.model.parameter.base.MultiplePdfSourceMultipleOutputParameters;
import org.sejda.model.parameter.base.SinglePdfSourceSingleOutputParameters;
import org.sejda.model.validation.constraint.NotEmpty;
import org.sejda.model.validation.constraint.SingleOutputAllowedExtensions;

import java.util.*;

Expand All @@ -41,6 +39,7 @@ public final class SetMetadataParameters extends MultiplePdfSourceMultipleOutput
@NotEmpty
private final Map<String, String> metadata = new HashMap<String, String>();
private final Set<String> toRemove = new NullSafeSet<>();
private Boolean updateCreatorProducerModifiedDate = true;

public Map<String, String> getMetadata() {
return metadata;
Expand All @@ -66,9 +65,18 @@ public Set<String> getToRemove() {
return toRemove;
}

public Boolean isUpdateProducerModifiedDate() {
return updateCreatorProducerModifiedDate;
}

public void setUpdateCreatorProducerModifiedDate(Boolean updateCreatorProducerModifiedDate) {
this.updateCreatorProducerModifiedDate = updateCreatorProducerModifiedDate;
}

@Override
public int hashCode() {
return new HashCodeBuilder().appendSuper(super.hashCode()).append(metadata).append(toRemove).toHashCode();
return new HashCodeBuilder().appendSuper(super.hashCode()).append(metadata).append(toRemove)
.append(updateCreatorProducerModifiedDate).toHashCode();
}

@Override
Expand All @@ -82,6 +90,7 @@ public boolean equals(Object other) {
SetMetadataParameters parameter = (SetMetadataParameters) other;
return new EqualsBuilder().appendSuper(super.equals(other)).append(metadata, parameter.metadata)
.append(toRemove, parameter.toRemove)
.append(updateCreatorProducerModifiedDate, parameter.updateCreatorProducerModifiedDate)
.isEquals();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import org.sejda.core.support.io.MultipleOutputWriter;
import org.sejda.core.support.io.OutputWriters;
import org.sejda.core.support.io.SingleOutputWriter;
import org.sejda.impl.sambox.component.DefaultPdfSourceOpener;
import org.sejda.impl.sambox.component.PDDocumentHandler;
import org.sejda.model.exception.TaskException;
Expand Down Expand Up @@ -79,7 +78,10 @@ public void execute(SetMetadataParameters parameters) throws TaskException {
LOG.debug("Opening {}", source);

documentHandler = source.open(documentLoader);
documentHandler.setCreatorOnPDDocument();
documentHandler.setUpdateProducerModifiedDate(parameters.isUpdateProducerModifiedDate());
if(parameters.isUpdateProducerModifiedDate()) {
documentHandler.setCreatorOnPDDocument();
}

File tmpFile = createTemporaryBuffer(parameters.getOutput());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class PDDocumentHandler implements Closeable {
private PDDocument document;
private PDDocumentAccessPermission permissions;
private Set<WriteOption> writeOptions = new HashSet<>();
private boolean updateProducerModifiedDate = true;

/**
* Creates a new handler using the given document as underlying {@link PDDocument}.
Expand Down Expand Up @@ -262,6 +263,10 @@ public void savePDDocument(File file, StandardSecurity security, EncryptionAtRes
if (Boolean.getBoolean(SAMBOX_USE_ASYNC_WRITER)) {
this.addWriteOption(WriteOption.ASYNC_BODY_WRITE);
}
if(!updateProducerModifiedDate) {
this.addWriteOption(WriteOption.NO_METADATA_PRODUCER_MODIFIED_DATE_UPDATE);
}

if (encryptionAtRestSecurity instanceof NoEncryptionAtRest) {
LOG.trace("Saving document to {} using options {}", file, writeOptions);
document.writeTo(file, security, writeOptions.stream().toArray(WriteOption[]::new));
Expand Down Expand Up @@ -436,4 +441,12 @@ public PDPage addBlankPageBefore(int pageNumber) {
document.getPages().insertBefore(result, target);
return result;
}

public boolean isUpdateProducerModifiedDate() {
return updateProducerModifiedDate;
}

public void setUpdateProducerModifiedDate(boolean updateProducerModifiedDate) {
this.updateProducerModifiedDate = updateProducerModifiedDate;
}
}

0 comments on commit 41b0621

Please sign in to comment.