Skip to content

Commit

Permalink
fix: issue #300: codewords padding for Macro PDF417 barcodes
Browse files Browse the repository at this point in the history
fixme: test case
  • Loading branch information
macromogic authored and asturio committed May 24, 2021
1 parent 8aa2a3b commit 1de5295
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Binary file not shown.

0 comments on commit 1de5295

Please sign in to comment.