生成pdf文件

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();
        }
    }*/

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不讲理的胖子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值