Skip to content

Commit

Permalink
Fixes form flattening when text field font is missing or cannot be lo…
Browse files Browse the repository at this point in the history
…oked up.

Adds test for basic form flattening.
  • Loading branch information
ediweissmann committed Jun 16, 2016
1 parent 06657ab commit 9d14acd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
23 changes: 23 additions & 0 deletions sejda-core/src/test/java/org/sejda/core/service/MergeTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
*/
package org.sejda.core.service;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -36,7 +39,9 @@
import org.sejda.model.pdf.page.PageRange;
import org.sejda.model.toc.ToCPolicy;
import org.sejda.sambox.pdmodel.PDDocument;
import org.sejda.sambox.pdmodel.PDPage;
import org.sejda.sambox.pdmodel.common.PDRectangle;
import org.sejda.sambox.text.PDFTextStripperByArea;

/**
* Test for the merge task
Expand Down Expand Up @@ -297,4 +302,22 @@ public void testExecuteMergeRangesWithBlankPage() throws IOException {
assertEquals(document.getPage(6).getCropBox().getHeight(), document.getPage(7).getCropBox().getHeight(), 0);

}

@Test
public void testExecuteMergeRangesWithFlattenForms() throws IOException {
List<PdfMergeInput> inputs = new ArrayList<PdfMergeInput>();
inputs.add(new PdfMergeInput(customInput("pdf/forms/simple_form_with_values.pdf")));
MergeParameters parameters = setUpParameters(inputs);
parameters.setAcroFormPolicy(AcroFormPolicy.FLATTEN);
testContext.pdfOutputTo(parameters);
execute(parameters);
PDDocument document = testContext.assertTaskCompleted();
PDPage page = document.getPage(0);
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.addRegion("completePage", new Rectangle((int) page.getCropBox().getWidth(), (int) page.getCropBox().getHeight()));
stripper.extractRegions(page);
String pageText = stripper.getTextForRegion("completePage");

assertThat(pageText, containsString("TextFieldValue"));
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@
import static org.sejda.sambox.cos.COSName.V;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -323,7 +321,13 @@ public boolean hasForm() {

private void flatten() {
try {
form.flatten();
List<PDField> fields = new ArrayList<>();

for (PDField field : form.getFieldTree()) {
fields.add(field);
}

form.flatten(fields, true);
} catch(IOException ex) {
LOG.warn("Failed to flatten form", ex);
}
Expand Down

0 comments on commit 9d14acd

Please sign in to comment.