diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodePDF417.java b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodePDF417.java index 6178c94a8..fd21c0f80 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/BarcodePDF417.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/BarcodePDF417.java @@ -862,7 +862,8 @@ else if (codeRows > 90) { pad = tot - lenErr - lenCodewords; if ((options & PDF417_USE_MACRO) != 0) { // the padding comes before the control block - System.arraycopy(codewords, macroIndex, codewords, macroIndex + pad, pad); + int lenCodewordsAdjusted = lenCodewords = macroIndex; + System.arraycopy(codewords, macroIndex, codewords, macroIndex + pad, lenCodewordsAdjusted); cwPtr = lenCodewords + pad; while (pad-- != 0) codewords[macroIndex++] = TEXT_MODE; diff --git a/openpdf/src/test/java/com/lowagie/text/pdf/BarcodeMacroPDF417Test.java b/openpdf/src/test/java/com/lowagie/text/pdf/BarcodeMacroPDF417Test.java new file mode 100644 index 000000000..a025c7465 --- /dev/null +++ b/openpdf/src/test/java/com/lowagie/text/pdf/BarcodeMacroPDF417Test.java @@ -0,0 +1,80 @@ +package com.lowagie.text.pdf; + +import com.lowagie.text.Document; +import com.lowagie.text.Image; +import com.lowagie.text.Paragraph; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class BarcodeMacroPDF417Test { + private static final Path OUTPUT_DIR = Paths.get(".", "target", "test-classes"); + private static final Path COMP_DIR = Paths.get(".", "src", "test", "resources"); + private static final String FILENAME = "barcode_macro_pdf_417.pdf"; + private static final int SEGMENT_COUNT = 2; + + @BeforeAll + static void setup() { + OUTPUT_DIR.toFile().mkdirs(); + COMP_DIR.toFile().mkdirs(); + } + + @Test + public void testBarcode() throws IOException { + generatePdf(); + Assertions.assertTrue(comparePdf()); + } + + private void generatePdf() throws IOException { + Document document = new Document(); + OutputStream out = new FileOutputStream(OUTPUT_DIR.resolve(FILENAME).toFile()); + PdfWriter.getInstance(document, out); + document.open(); + + String[] testTexts = {"Test PDF417 Segment 0", "Test PDF417 Segment 1"}; + + document.add(new Paragraph(testTexts[0])); + document.add(getBarcode(testTexts[0], 0)); + + for (int i = 0; i < 10; i++) { + document.add(new Paragraph(String.format("Test paragraph #%d", i))); + } + + document.add(new Paragraph(testTexts[1])); + document.add(getBarcode(testTexts[1], 1)); + + document.close(); + } + + private boolean comparePdf() throws IOException { + PdfReader outReader = new PdfReader(OUTPUT_DIR.resolve(FILENAME).toString()); + PdfReader cmpReader = new PdfReader(COMP_DIR.resolve(FILENAME).toString()); + PdfDictionary outDict = outReader.getPageN(1); + PdfDictionary cmpDict = cmpReader.getPageN(1); + if (!outDict.getKeys().equals(cmpDict.getKeys())) { + return false; + } + for (PdfName name : outDict.getKeys()) { + if (!outDict.get(name).toString().equals(cmpDict.get(name).toString())) { + return false; + } + } + return true; + } + + private Image getBarcode(String text, int segId) { + BarcodePDF417 bp = new BarcodePDF417(); + bp.setOptions(BarcodePDF417.PDF417_USE_MACRO); + bp.setMacroFileId("12"); + bp.setMacroSegmentCount(SEGMENT_COUNT); + bp.setMacroSegmentId(segId); + bp.setText(text); + return bp.getImage(); + } +} diff --git a/openpdf/src/test/resources/barcode_macro_pdf_417.pdf b/openpdf/src/test/resources/barcode_macro_pdf_417.pdf new file mode 100644 index 000000000..724976039 Binary files /dev/null and b/openpdf/src/test/resources/barcode_macro_pdf_417.pdf differ