Java生成pdf
Java生成PDF:
package com.hundsun.gildata.irp.core.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.hundsun.gildata.irp.common.util.ItextUtils;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
public class MakePdfTest {
private String att;
public void makdPdf() throws IOException {
// final int headFontSize = 18;
// final int sectionFontSize = 12;
final int margin = 10;
String filePath = "d:/aaa/";
String fileName = "aa.pdf";
att = filePath + fileName;
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
Document document = null;
FileOutputStream fos = null;
PdfWriter pdf = null;
try {
document = new Document(PageSize.A4, margin, margin, margin, margin);
fos = new FileOutputStream(filePath + fileName);
pdf = PdfWriter.getInstance(document, fos);
document.open();
String summary = " this is a pdf made bycode\n 另起一行".replaceAll("\n", "<br/>").replaceAll(" ", " ");
document.add(ItextUtils.processViewpoint(summary));
att = filePath + fileName;
} catch (Exception e) {
return;
} finally {
if (document != null) {
document.close();
}
if (pdf != null) {
pdf.close();
}
if (fos != null) {
fos.close();
}
}
}
public static void main(String[] args) {
MakePdfTest testMake = new MakePdfTest();
try {
testMake.makdPdf();
System.out.println(testMake.att);
} catch (IOException e) {
e.printStackTrace();
}
}
}

浙公网安备 33010602011771号