package cn.com.bmsoft.util;
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
/**
* 生成pdf文件
* @author lizhiyong
* @version $Id: GeneratePdf.java, v 0.1
2015-6-6 下午2:40:22 1111 Exp $
*/
public class GeneratePdf {
@SuppressWarnings("unused")
private void handleText(PdfWriter writer, String content, String color, float x, float y,
float z) {
PdfContentByte canvas = writer.getDirectContent();
Phrase phrase = new Phrase(content);
if (color != null) {
phrase = new Phrase(content, FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL,
new Color(255, 0, 0)));
}
ColumnText.showTextAligned(canvas, Element.ALIGN_UNDEFINED, phrase, x, y, z);
}
public void Pdf(String imagePath, String mOutputPdfFileName, String fileName) {
Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
try {
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
doc.open();
doc.newPage();
Image png1 = Image.getInstance(imagePath);
float heigth = png1.getHeight();
float width = png1.getWidth();
//int percent = this.getPercent2(heigth, width);
png1.setAlignment(Image.MIDDLE);
png1.setAlignment(Image.TEXTWRAP);
// png1.scalePercent(120);
png1.setAbsolutePosition(200, 600);
doc.add(png1);
//doc.newPage();
/*BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font font = new Font(baseFont, 12, Font.NORMAL, Color.BLACK);
Paragraph pf = new Paragraph(fileName, font);
doc.add(pf);*/
doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File mOutputPdfFile = new File(mOutputPdfFileName);
if (!mOutputPdfFile.exists()) {
mOutputPdfFile.deleteOnExit();
}
}
public int getPercent1(float h, float w) {
int p = 0;
float p2 = 0.0f;
if (h > w) {
p2 = 297 / h * 100;
} else {
p2 = 210 / w * 100;
}
p = Math.round(p2);
return p;
}
private int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.round(p2);
return p;
}
/* public void apped() throws Exception, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("E:\\bms\\pdf2.pdf"));
document.open();
PdfReader reader = new PdfReader("E:\\bms\\pdf.pdf");
int n = reader.getNumberOfPages();
PdfImportedPage page;
for (int i = 1; i <= n; i++) {
page = writer.getImportedPage(reader, i);
Image image = Image.getInstance(page);
image.setAbsolutePosition(0, 0);
document.add(image);
document.newPage();
}
document.close();
}*/
/*public static void main(String[] args) throws Exception {
GeneratePdf gp = new GeneratePdf();
String pdfUrl = "E:\\bms\\pdf.pdf";
File file = gp.Pdf("E:\\bms\\r01.png", pdfUrl);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
gp.apppppp();
//p.apped();
}*/
/*@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws Exception {
List imageList = new ArrayList();
imageList.add(0, "E:\\bms\\r.png");
imageList.add(1, "E:\\bms\\r01.png");
imageList.add(2, "E:\\bms\\r0101.png");
imageList.add(3, "E:\\bms\\r010101.png");
GeneratePdf generatePdf = new GeneratePdf();
PdfReader reader = null;
PdfStamper stamp = null;
int num = 1;
String pint1 = null;
System.out.println("总长度:" + imageList.size());
for (int i = 0; i < imageList.size(); i++) {
System.out.println("次数:" + imageList.get(i));
String pdfPath = "E:\\bms\\imagePdf" + num + ".pdf";
if (i == 0) {
generatePdf.Pdf((String) imageList.get(i), pdfPath, (String) imageList.get(i));
reader = new PdfReader(pdfPath);
stamp = new PdfStamper(reader, new FileOutputStream("E:\\bms\\stamped.pdf"));
pint1 = "E:\\bms\\stamped.pdf";
}
if (i > 0 && num <= imageList.size()) {
reader = new PdfReader(pint1);
stamp = new PdfStamper(reader, new FileOutputStream(pint1 + num + ".pdf"));
pint1 = pint1 + num + ".pdf";
}
if (num > imageList.size()) {
break;
} else {
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
PdfContentByte over = null;
if (i == 0) {
// 拿到第一页的内容
over = stamp.getOverContent(i + 1);
// 增加内容
over.beginText();
over.setFontAndSize(baseFont, 12);
//over.showText("page1");
over.showTextAligned(Element.ALIGN_LEFT, (String) imageList.get(i), 200, 580, 0);
over.endText();
} else {
stamp.insertPage(num, PageSize.A4);
// 获取第num页的内容
over = stamp.getOverContent(num);
//开始写内容
over.beginText();
over.setFontAndSize(baseFont, 12);
Image png1 = Image.getInstance((String) imageList.get(i));
System.out.println("图片:" + imageList.get(i));
png1.setAbsolutePosition(200, 600);
over.addImage(png1);
over.showTextAligned(Element.ALIGN_LEFT, (String) imageList.get(i), 200, 580,
0);
over.endText();
//写内容结束
}
num++;
}
stamp.close();
}
}*/
}
生成pdf文件
最新推荐文章于 2022-06-13 09:44:15 发布