jasperReport 批量处理pdf乱码

本文介绍了一种利用开源IReport工具及其源代码来批量处理jrxml文件并生成PDF报告的方法。通过Java程序实现了目录的选择、文件的遍历、字体设置及PDF编译等功能。

1 下载开源 IReport 利用其源代码

2 创建 report

3 具体源代码

public class PdfDeal {

/**
* @param args
*/

public static void main(String[] args) {

File dir = getSelDir();

if (dir == null)
return;

System.out.println("start ........");

List<String> files = getFiles(dir);

for (int i = 0; i < files.size(); ++i) {
String file = files.get(i);

System.out.println(file + " 正在创建........");
Report report = null;
try {
report = new Report(file);
} catch (Exception e) {
e.printStackTrace();
System.out.println(file + " 创建失败,请检查........");
continue;
}

System.out.println(file + " 正在处理........");
boolean isDo = doReport(report, file);

if (isDo) {
System.out.println(file + " 正在保存........");

report.saveXMLFile(file);

try {

System.out.println(file + " 正在重新编译........");

JasperCompileManager.compileReportToFile(file); // 重新编译

} catch (JRException e) {
e.printStackTrace();
System.out.println(file + " 编译失败,请检查........");
}
}
}
System.out.println("end ........");
}

private static boolean doReport(Report report, String file) {

boolean isDo = false;

Vector reportElements = report.getElements();

for (int j = 0; j < reportElements.size(); ++j) {
ReportElement reportElement = (ReportElement) reportElements
.elementAt(j);

if (reportElement instanceof TextReportElement) {
TextReportElement textElement = (TextReportElement) reportElement;

int fontSize = textElement.getFontSize();
String fontName = textElement.getFontName();
boolean bold = textElement.isBold();
textElement.setReportFont(null);
doPdf(textElement);

if ("Arial Narrow".equals(fontName) || "宋体".equals(fontName)) {
textElement.setFontName(fontName);
} else
textElement.setFontName("宋体");

textElement.setFontSize(fontSize);
textElement.setBold(bold);

isDo = true;

}

}
return isDo;
}


private static void doPdf(TextReportElement textElement) {
textElement.setPDFFontName("STSong-Light");
textElement.setPdfEmbedded(true);
textElement.setPdfEncoding("UniGB-UCS2-H");

}

/**
* 得到选择的目录
*
* @return
*/
private static File getSelDir() {

JFileChooser jfc = new JFileChooser(
"/reports");

jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
return jfc.getSelectedFile();

}
return null;
}

/**
* 得到指定目录下(包含子目录下)所有的jrxml 文件
*
* @param dir
* @return
*/
private static List<String> getFiles(File dir) {

List<String> files = new ArrayList<String>();
File[] fileList = dir.listFiles();

for (int i = 0; i < fileList.length; ++i) {

File file = fileList[i];
if (file.isDirectory()) {
files.addAll(getFiles(file));
} else {
if (file.getName().endsWith(".jrxml")) {
files.add(file.getAbsolutePath());
}
}

}
return files;
}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值