Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue #300: codewords padding for Macro PDF417 barcodes #546

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.