main
package com.tom.tag;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.tom.tag.code.MBarcode;
import com.tom.tag.code.MBarcode128;
import com.tom.tag.constant.Constant;
import com.tom.tag.entry.Bounds;
import com.tom.tag.shape.draw.BarCodeShape;
import com.tom.tag.utils.BarCodeUtil;
import com.tom.tag.utils.ReadAndWriteFile;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;
public class 无名 {
//英文字体
public static String enlfont = "C:\\Users\\Administrator\\Desktop\\HelveticaNeue-Medium.ttf";
//微软雅黑
public static String hzlfont = "C:\\Users\\Administrator\\Desktop\\msyh.ttf";
//日期文件
public static String riqi = "C:\\Users\\Administrator\\Desktop\\日期.pdf";
//每个文字的宽度
public static String fontPeiZhi = "C:\\Users\\Administrator\\Desktop\\1.txt";
//背景文件
public static String bgPath = "C:\\Users\\Administrator\\Desktop\\bg2real.pdf";
//输出目录
public static String outD = "C:\\Users\\Administrator\\Desktop";
public static float pt = 72f / 25.4f;
public static float TEXT_HEIGHT = 0.2535f;
public static void main(String[] args) {
work("c:\\Users\\Administrator\\Desktop\\耐克门店-37855.txt");
}
public static void work(String shuju) {
Map<String, Float> map = new HashMap<>();
List<String> list = ReadAndWriteFile.readtFile(fontPeiZhi, "utf-8");
for (String str : list) {
if (str.indexOf(",") != -1) {
String[] arr = str.split(",");
map.put(arr[0].split("=")[1], Float.valueOf(arr[1].split("=")[1].substring(0, 5)));
}
}
List<String> list1 = ReadAndWriteFile.readtFile(shuju, "gbk");
List<Map<String, String>> mapList = new ArrayList<>();
for (String str : list1) {
if (str.length() > 0 && str.indexOf("\t") != -1) {
try {
Map<String, String> map1 = new HashMap<>();
map1.put("A", str.split("\t")[0]);
map1.put("B", str.split("\t")[1]);
map1.put("C", str.split("\t")[2]);
map1.put("D", str.split("\t")[3]);
map1.put("E", str.split("\t")[4]);
map1.put("F", str.split("\t")[5]);
mapList.add(map1);
} catch (Exception e) {
}
}
}
String outname = new File(shuju).getName();
outname = outname.substring(0, outname.lastIndexOf("."));
createPdf(mapList, map, outname);
}
public static void createPdf(List<Map<String, String>> mapList, Map<String, Float> map, String outname) {
try {
String outPath = outD + "\\" + outname + ".pdf";
PdfReader reader = new PdfReader(bgPath);
PdfReader readerRiQi = new PdfReader(riqi);
Rectangle cropBox = reader.getCropBox(1);
float w = cropBox.getWidth();
float h = cropBox.getHeight();
Document document = new Document(new Rectangle(w, h));//新建一个文档并且设置页面大小
FileOutputStream outputStream = new FileOutputStream(outPath);//新建一个pdf文档;
PdfWriter writer = PdfWriter.getInstance(document, outputStream);//把新建的pdf 赋值给 document
writer.setPdfVersion(PdfWriter.VERSION_1_5);
document.open();//打开 document文档
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage importedPage = writer.getImportedPage(reader, 1);
float[] floats = {0, 0f, 0, 1f};
for (Map<String, String> map1 : mapList) {
document.newPage();
cb.addTemplate(importedPage, 0, 0);
CMYKColor cmykColor = new CMYKColor(0f, 0f, 0, 1f);
String e = map1.get("E");
Code128 code128 = new Code128(new Bounds(84.518f, 4.885f, 32.629f, 17.364f), cmykColor, e, String.valueOf(Barcode.CODE128));
code128.draw(cb);
cb.saveState();
cb.setColorFill(new CMYKColor(0, 0, 0, 0f));
cb.rectangle(87.347f * pt, 4.149f * pt, 28.699f * pt, 4.017f * pt);
cb.fill();
cb.restoreState();
TextInfo textInfoe = new TextInfo();
textInfoe.setColorArr(floats);
textInfoe.setContent(e);
textInfoe.setFontName(enlfont);
textInfoe.setFontSize(9.48f);
textInfoe.setRotate(0);
textInfoe.setX(32.628f / 2 + 84.519f);
textInfoe.setY(5.5f);
textInfoe.setAli(PdfContentByte.ALIGN_CENTER);
addText(cb, textInfoe);
String a = map1.get("A");
TextInfo textInfoa = new TextInfo();
textInfoa.setColorArr(floats);
textInfoa.setContent("No." + a);
textInfoa.setFontName(enlfont);
textInfoa.setFontSize(6.5f);
textInfoa.setRotate(0);
textInfoa.setX(8.965f);
textInfoa.setY(20.495f);
textInfoa.setAli(PdfContentByte.ALIGN_LEFT);
addText(cb, textInfoa);
String d = map1.get("D");
float width = getWidth(map, d);
if (8.965f + 3.8f + 1.15f * (a.length() + 1) + width > 48.756) {
TextInfo textInfod = new TextInfo();
textInfod.setColorArr(floats);
textInfod.setContent(d);
textInfod.setFontName(enlfont);
textInfod.setFontSize(6.5f);
textInfod.setRotate(0);
textInfod.setX(8.965f + 3.8f + 1.15f * (a.length() + 1));
textInfod.setY(20.495f);
textInfod.setAli(PdfContentByte.ALIGN_LEFT);
addText(cb, textInfod);
String c = map1.get("C");
TextInfo textInfoc = new TextInfo();
textInfoc.setColorArr(floats);
textInfoc.setContent(c);
textInfoc.setFontName(hzlfont);
textInfoc.setFontSize(6.5f);
textInfoc.setRotate(0);
textInfoc.setX(8.965f);
textInfoc.setY(15.5f);
textInfoc.setAli(PdfContentByte.ALIGN_LEFT);
addText(cb, textInfoc);
String b = map1.get("B");
TextInfo textInfob = new TextInfo();
textInfob.setColorArr(floats);
textInfob.setContent(b);
textInfob.setFontName(enlfont);
textInfob.setFontSize(6.5f);
textInfob.setRotate(0);
textInfob.setX(8.965f + 3.8f + 1.15f * (a.length() + 1) + width);
textInfob.setY(15.5f);
textInfob.setAli(PdfContentByte.ALIGN_RIGHT);
addText(cb, textInfob);
TextInfo textInfoyxq = new TextInfo();
textInfoyxq.setColorArr(floats);
textInfoyxq.setContent("有效期:");
textInfoyxq.setFontName(hzlfont);
textInfoyxq.setFontSize(6.5f);
textInfoyxq.setRotate(0);
textInfoyxq.setX(8.965f);
textInfoyxq.setY(10.6f);
textInfoyxq.setAli(PdfContentByte.ALIGN_LEFT);
addText(cb, textInfoyxq);
TextInfo textInfofk = new TextInfo();
textInfofk.setColorArr(floats);
textInfofk.setContent("访客姓名:");
textInfofk.setFontName(hzlfont);
textInfofk.setFontSize(6.5f);
textInfofk.setRotate(0);
textInfofk.setX(8.965f);
textInfofk.setY(5.8f);
textInfofk.setAli(PdfContentByte.ALIGN_LEFT);
addText(cb, textInfofk);
List<Position> positions = new ArrayList<>();
positions.add(new Position(19.239f, 5.707f));
positions.add(new Position(8.965f + 3.8f + 1.15f * (a.length() + 1) + width, 5.707f));
addLine(cb, positions);
PdfImportedPage importedPage1 = writer.getImportedPage(readerRiQi, 1);
cb.addTemplate(importedPage1, 17.914f * pt, (10.6f - 0.1926f) * pt);
PdfImportedPage importedPage3 = writer.getImportedPage(readerRiQi, 3);
cb.addTemplate(importedPage3, (8.965f + 3.8f + 1.15f * (a.length() + 1) + width - importedPage3.getWidth() / pt) * pt, (10.6f - 0.1926f) * pt);
PdfImportedPage importedPage2 = writer.getImportedPage(readerRiQi, 2);
cb.addTemplate(importedPage2, 17.914f*pt +importedPage1.getWidth()+
(8.965f + 3.8f + 1.15f * (a.length() + 1) + width-17.914f-importedPage1.getWidth()/pt-importedPage3.getWidth()/pt)*pt/2-importedPage2.getWidth()/2, 10.6f * pt);
} else {
float wmax = 48.756f;
TextInfo textInfod = new TextInfo();
textInfod.setColorArr(floats);
textInfod.setContent(d);
textInfod.setFontName(enlfont);
textInfod.setFontSize(6.5f);
textInfod.setRotate(0);
textInfod.setX(wmax);
textInfod.setY(20.495f);
textInfod.setAli(PdfContentByte.ALIGN_RIGHT);
addText(cb, textInfod);
String c = map1.get("C");
TextInfo textInfoc = new TextInfo();
textInfoc.setColorArr(floats);
textInfoc.setContent(c);
textInfoc.setFontName(hzlfont);
textInfoc.setFontSize(6.5f);
textInfoc.setRotate(0);
textInfoc.setX(8.965f);
textInfoc.setY(15.5f);
textInfoc.setAli(PdfContentByte.ALIGN_LEFT);
addText(cb, textInfoc);
String b = map1.get("B");
TextInfo textInfob = new TextInfo();
textInfob.setColorArr(floats);
textInfob.setContent(b);
textInfob.setFontName(enlfont);
textInfob.setFontSize(6.5f);
textInfob.setRotate(0);
textInfob.setX(wmax);
textInfob.setY(15.5f);
textInfob.setAli(PdfContentByte.ALIGN_RIGHT);
addText(cb, textInfob);
TextInfo textInfoyxq = new TextInfo();
textInfoyxq.setColorArr(floats);
textInfoyxq.setContent("有效期:");
textInfoyxq.setFontName(hzlfont);
textInfoyxq.setFontSize(6.5f);
textInfoyxq.setRotate(0);
textInfoyxq.setX(8.965f);
textInfoyxq.setY(10.6f);
textInfoyxq.setAli(PdfContentByte.ALIGN_LEFT);
addText(cb, textInfoyxq);
TextInfo textInfofk = new TextInfo();
textInfofk.setColorArr(floats);
textInfofk.setContent("访客姓名:");
textInfofk.setFontName(hzlfont);
textInfofk.setFontSize(6.5f);
textInfofk.setRotate(0);
textInfofk.setX(8.965f);
textInfofk.setY(5.8f);
textInfofk.setAli(PdfContentByte.ALIGN_LEFT);
addText(cb, textInfofk);
List<Position> positions = new ArrayList<>();
positions.add(new Position(19.239f, 5.707f));
positions.add(new Position(wmax, 5.707f));
addLine(cb, positions);
PdfImportedPage importedPage1 = writer.getImportedPage(readerRiQi, 1);
float pt = 72f / 25.4f;
cb.addTemplate(importedPage1, 17.914f * pt, (10.6f - 0.1926f) * pt);
PdfImportedPage importedPage2 = writer.getImportedPage(readerRiQi, 2);
cb.addTemplate(importedPage2, (17.914f + (wmax - 17.914f) / 2) * pt - importedPage2.getWidth() / 2 - 0.6445f * pt, 10.6f * pt);
PdfImportedPage importedPage3 = writer.getImportedPage(readerRiQi, 3);
cb.addTemplate(importedPage3, (wmax - importedPage3.getWidth() / pt) * pt, (10.6f - 0.1926f) * pt);
}
}
outputStream.flush();//关闭文件
document.close();//关闭文件
outputStream.close();//关闭文件
reader.close();
readerRiQi.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static float getWidth(Map<String, Float> map, String text) {
float pt = 72 / 25.4f;
map.put(" ", 0.35f * pt);
float w = 0;
if (text.trim().length() > 0) {
for (String s : text.split("")) {
w += map.get(s) / pt + 0.27f;
}
return w;
} else {
return 0;
}
}
public static int getCode128Length(String code){
String bCode = Barcode128.getRawText(code, 9 == 10, Barcode128.Barcode128CodeSet.AUTO);
byte bars[] = Barcode128.getBarsCode128Raw(bCode);
int barStartX =0;
for (int k = 0; k < bars.length; ++k) {
float w = bars[k] ;
barStartX += w;
}
return barStartX;
}
/**
* 添加文本
*
* @param cb
* @param textInfo 文本的信息对象
*/
public static void addText(PdfContentByte cb, TextInfo textInfo) {
if (null != textInfo.getContent()) {
try {
float pt = 72f / 25.4f;
float[] colorArr = textInfo.getColorArr();
ExtendedColor baseColor = new CMYKColor(colorArr[0], colorArr[1], colorArr[2], colorArr[3]);
cb.setColorFill(baseColor);
BaseFont baseFont = BaseFont.createFont(textInfo.getFontName(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(baseFont, textInfo.getFontSize());
cb.showTextAligned(textInfo.getAli(), textInfo.getContent(), textInfo.getX() * pt, textInfo.getY() * pt, textInfo.getRotate());
cb.endText();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void addLine(PdfContentByte cb, List<Position> listPosition) {
try {
float pt = 72f / 25.4f;
cb.saveState();
cb.setLineWidth(0.15f * pt);
cb.setColorStroke(new CMYKColor(0, 0f, 0, 1f));
int k = 0;
for (Position position : listPosition) {
if (k == 0) {
cb.moveTo(position.getX() * pt, position.getY() * pt);
} else {
cb.lineTo(position.getX() * pt, position.getY() * pt);
}
k++;
}
cb.closePathStroke();
cb.restoreState();
} catch (Exception e) {
e.printStackTrace();
}
}
}
code128
package com.tom.tag;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.Barcode;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.tom.tag.code.MBarcode;
import com.tom.tag.code.MBarcode128;
import com.tom.tag.entry.Bounds;
import com.tom.tag.shape.Shape;
public class Code128 implements Shape {
private Bounds bounds;
private Object color;
private String content;
private String code_type;
// 专色的对比度
public static float TEXT_HEIGHT = 0.2535f;
private float contrast = 1;
private static float pt = 72/25.4f;
public Code128() {
}
public Code128(Bounds bounds, Object color, String content, String code_type) {
this.bounds = bounds;
this.color = color;
this.content = content;
this.code_type = code_type;
}
public Code128(Bounds bounds, Object color, String content, String code_type, float contrast) {
this.bounds = bounds;
this.color = color;
this.content = content;
this.code_type = code_type;
this.contrast = contrast;
}
public void draw(PdfContentByte cb) throws DocumentException {
MBarcode code= getBarCode(content,code_type,bounds.getW()*pt,bounds.getH()*pt,(int)(11.6f*bounds.getW()/40f));
Image codeImage = code.createImageWithBarcode(cb, this.color, this.color,this.contrast);
cb.addImage(codeImage,this.bounds.getW()*pt,0,0,this.bounds.getH()*pt,this.bounds.getX()*pt,this.bounds.getY()*pt);
}
@Override
public void draw(PdfContentByte cb, PdfWriter writer) throws Exception {
}
public static MBarcode getBarCode(String content, String type, float w, float h, float fontSize){
MBarcode code = new MBarcode128();
code.setX(w/getCode128Length(content));
code.setCodeType(Barcode.CODE128);
code.setBaseline(fontSize*TEXT_HEIGHT*pt/0.75f);
code.setBarHeight(h-code.getBaseline());
code.setSize(fontSize);
code.setAltText(content);
code.setCode(content);
return code;
}
public static int getCode128Length(String code){
String bCode = Barcode128.getRawText(code, 9 == 10, Barcode128.Barcode128CodeSet.AUTO);
byte bars[] = Barcode128.getBarsCode128Raw(bCode);
int barStartX =0;
for (int k = 0; k < bars.length; ++k) {
float w = bars[k] ;
barStartX += w;
}
return barStartX;
}
}
textInfo
package com.tom.tag;
public class TextInfo {
private float x;
private float y;
private String content;
private float fontSize;
private String fontName;
private float[] colorArr;
private int rotate;
private int ali;
public int getAli() {
return ali;
}
public void setAli(int ali) {
this.ali = ali;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public float getFontSize() {
return fontSize;
}
public void setFontSize(float fontSize) {
this.fontSize = fontSize;
}
public String getFontName() {
return fontName;
}
public void setFontName(String fontName) {
this.fontName = fontName;
}
public float[] getColorArr() {
return colorArr;
}
public void setColorArr(float[] colorArr) {
this.colorArr = colorArr;
}
public int getRotate() {
return rotate;
}
public void setRotate(int rotate) {
this.rotate = rotate;
}
}
Position
package com.tom.tag;
public class Position {
private float x;
private float y;
public Position() {
}
public Position(float x, float y) {
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
}
Bounds
package com.tom.tag.entry;
public class Bounds {
private float x;
private float y;
private float w;
private float h;
public Bounds() {
}
public Bounds(float x, float y, float w, float h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getW() {
return w;
}
public void setW(float w) {
this.w = w;
}
public float getH() {
return h;
}
public void setH(float h) {
this.h = h;
}
}
读写txt类
package com.tom.tag.utils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ReadAndWriteFile {
/**
*
* @param path
* @param charsetName utf-8 gbk
* @return
*/
public static List<String> readtFile(String path,String charsetName) {
List<String> list = new ArrayList<>();
InputStreamReader read = null;// 考虑到编码格式
try {
read = new InputStreamReader(new FileInputStream(path), charsetName);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = bufferedReader.readLine();
while ( lineTxt!=null){
list.add(lineTxt);
lineTxt = bufferedReader.readLine();
}
read.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
*
* @param path
* @param content utf-8 gbk
* @param charsetName
*/
public static void writeInFile(String path, String content,String charsetName) {
BufferedWriter writer = null;
StringBuilder outputString = new StringBuilder();
try {
outputString.append(content );
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),charsetName));
writer.write(outputString.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}