pdf417条形码开发
近期有做條碼開發的工作,只給了個模板,導出和模板一致的條碼。條碼如圖:
剛開始的時候因為對條碼不熟悉,加上長得條碼的樣子誤以為是一維條碼,后發現是二維條碼。
開發過程首先是word導出,底層條碼導出。word導出采用freemarker的模板進行導出,網上例子很多。只說一些難點:
1)導出過程中涉及字體等如加黑,加粗。模板改成xml格式,你可以看到許多wordxml標簽。
builder.append("<w:p>");
builder.append("<w:pPr>");
builder.append("<w:spacing w:before=“93” w:beforeLines=“30” w:line=“560” w:lineRule=“exact”/>");
//builder.append("<w:spacing w:before=“93” w:beforeLines=“30” w:line=“560” w:lineRule=“auto”/>");
builder.append("<w:ind w:firstLine=“640” w:firstLineChars=“200”/>");
builder.append("<w:rPr>");
builder.append("<w:rFonts w:hint=“eastAsia” w:ascii=“方正楷體簡體” w:eastAsia=“方正楷體簡體”/>");
builder.append("<w:sz w:val=“32”/>");
從模板中獲取相關wordxml信息,后臺組裝。并給占位符賦值。
換行等<w:br/>
2)為了生成樣式風格統一的條碼試了各種方案,barcode4j,zxing,itext。最后確定為itext。因條碼內容增加,條碼會隨著變化,為美觀并符合條碼規范,將長度固定,寬度自增。
BarcodePDF417 barcodePDF417 = new BarcodePDF417();
barcodePDF417.setOptions(BarcodePDF417.PDF417_FIXED_COLUMNS);
barcodePDF417.setCodeColumns(6);// 4.52
barcodePDF417.setText(msg.getBytes(“utf-8”));
barcodePDF417.setYHeight(2);
// 生成圖片
Image pdfImg = barcodePDF417.createAwtImage(Color.black, Color.white);
3)導出條碼長度固定,寬度自增。這樣需要修改模板。
StringBuilder builder = new StringBuilder();
if(imageTyle){//啟動動態image模板,動態設置image長寬
//1px=0.75pt
double imageWidth=Integer.parseInt(width)*0.75;
double imageHeight=Integer.parseInt(height)*0.75;
String style=“height:”+imageHeight+“pt”+";"+“width:”+imageWidth+“pt;”;
builder.append("<v:shape id="_x0000_i1025" o:spt=“75” type="#_x0000_t75" style=""+style+"" filled=“f” stroked=“f” coordsize=“21600,21600”>");
}else{
builder.append("<v:shape id="_x0000_i1025" o:spt=“75” type="#_x0000_t75" style=“height:48.5pt;width:128.5pt;” filled=“f” stroked=“f” coordsize=“21600,21600”>");
}
builder.append("<v:path/>");
builder.append("<v:fill on=“f” focussize=“0,0”/>");
builder.append("<v:stroke on=“f”/>");
builder.append("<v:imagedata r:id=“rId6” o:title=“1618815484140”/>");
builder.append("<o:lock v:ext=“edit” aspectratio=“t”/>");
builder.append("<w10:wrap type=“none”/>");
builder.append(“w10:anchorlock/”);
builder.append("</v:shape>");
return builder.toString();
這些wordxml都是從模板中所得
4)freemarker導出的word相對美觀,但是缺少相關API,復雜的涉及字體,圖片大小等需要向模板中動態添加wordxml標簽。
5)barcode4j生成條碼包含中文需要轉化字符集
String msg = new String(code.getBytes(“utf-8”), “Cp437”);
總結
以上是生活随笔為你收集整理的pdf417条形码开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于语雀知识库的二三事
- 下一篇: CAD 2022卸载方法,如何完全彻底卸