From 1de529531bb58f492f82a09e6afa2049c3b3053a Mon Sep 17 00:00:00 2001 From: Macromogic Date: Sat, 22 May 2021 02:23:53 +0800 Subject: [PATCH] fix: issue #300: codewords padding for Macro PDF417 barcodes fixme: test case --- .../com/lowagie/text/pdf/BarcodePDF417.java | 3 +- .../text/pdf/BarcodeMacroPDF417Test.java | 80 ++++++++++++++++++ .../test/resources/barcode_macro_pdf_417.pdf | Bin 0 -> 2069 bytes 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 openpdf/src/test/java/com/lowagie/text/pdf/BarcodeMacroPDF417Test.java create mode 100644 openpdf/src/test/resources/barcode_macro_pdf_417.pdf 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 0000000000000000000000000000000000000000..724976039b982cef96a4ec2d1593c50d3e49205f GIT binary patch literal 2069 zcmbVNc~BEq92U4{}LsG}mIcu=F3fOcf8T0zh{DwR>&)Ip(dIizZj8TXIf_rCq+cfQ~E0u<4) zTs&0B3b@nZyvo864573uSmEJl6lJDtDl4G_Xf#-5(g9^$B&y2N(w|rXnvh8t02&WW z1|yB|rD&|lOamJlYbIzA4RjOMIU=9B=FW;~1w`V>AS`07cHxyd zwUK`ANnyKEa=colho3n$C(FIMw61hxp2&Tw(~+!M7Ge#|cDt`R@xq#yY<#*uVf*PF zdv31Q_tuCxR{7lJTm$sk(dde@+cS!fJQ&^JcP{&YrCyp3*}DA>mXmagYeOdZXRPF2 zS!kRK-Fb3bHCv<)|;qCVyx2sdBU{;+nR%ItlfWZbybyl#Qmfw6(3AA=ul#;9fm)*1eLQz00S` zC8?gDR$G<1wXq~;!}g@F`FD~y*;~A_m+u|Wjy)n;j-GQ zTRusR9nTLIr+5E?jSZBRdQ3jy^WzwgTZQ*>^L|?%SK{gntGNDsiuO$Ro{WlbjWIX8 zlWN{uC&fN@)X( OE_~Vz3>L9gDAXwmn@{;152Y=em9T^kJ$crw}^0)H2)uTv6i9 zvbs9%rKL583kTvWyL9adHyz({W7gLE=l;19vP05Zjz*No|6Jr!j7`ZWL&xnbn3-^Y znV=P?(ej7BC;BFFKPp(&{XQ~ddix05G3eAc^Y|=737g$j?;lz^o150sJF&~{*-N3@ z+b2OVd=ma)j0fix7xVr$oI9>GuyW^f#`ogK5`2tU}z31pw?d@(uPo%*RUq|XqNH!tK& zd1i3c&IbHuDp&Jz+Mzj#XQ!~Uu3mF_)!M|~Ve%=uzWllsUzb-t2u-=1ThgbTt2`8M zD(1~9-Jv!}eNs>wFzoPA9wzZUf@OHYQ;dG-Y3k`M0jxZ%q4k(s_ z5jG{TQ&~10utOb;g(T*KBRqyBlMR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