使用iText生成doc

来源:互联网 发布:黄惠康 知乎 编辑:程序博客网 时间:2024/05/18 16:54
public void generateDoc()
    {
        Document document = new Document(PageSize.A4);
        try
        {
            RtfWriter2.getInstance(document, new FileOutputStream("e:/1.doc"));
        }
        catch (FileNotFoundException e1)
        {
            e1.printStackTrace();
        }
        document.open();


        // 添加页眉
        HeaderFooter header = new HeaderFooter(new Phrase("header"), false);
        header.setAlignment(Rectangle.ALIGN_CENTER);
        document.setHeader(header);
        // 添加页脚
        HeaderFooter footer = new HeaderFooter(new Phrase("footer"), false);
        footer.setAlignment(Rectangle.ALIGN_CENTER);
        document.setFooter(footer);
        // 设置中文字体
        BaseFont bfChinese = null;
        try
        {
            bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);
        }
        catch (DocumentException | IOException e)
        {
            e.printStackTrace();
        }
        Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
        Paragraph context = new Paragraph("");
        context.setFont(contextFont);
        // Image png = Image.getInstance("D:/busy.gif");
        // png.setAbsolutePosition(0, 0);
        // png.setAlignment(Image.TEXTWRAP);


        context.add("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
        // context.add(new Phrase(new Chunk(png, 0, 0, true)));
        context.add("bbbbbbbbbbbbbbbbbbbbbbbbbbb");
        // context.add(new Phrase(new Chunk(png, 0, 0, true)));
        context.add("ccccccccccccccccccccccccccc");
        try
        {
            document.add(context);
        }
        catch (DocumentException e)
        {
            e.printStackTrace();
        }
        document.close();
    }
0 0
原创粉丝点击