设为首页 加入收藏

TOP

xml解析生成pdf,word文档(四)
2012-11-30 12:26:45 来源: 作者: 【 】 浏览:1539
Tags:xml 解析 生成 pdf word 文档

 

    * 初始化

    */

    public void initPdfDocument(Object filePath)

    {

    String fileSrc=(String)filePath;

    document = new Document(PageSize.A4);

    File file = new File(fileSrc);

    try

    {

    PdfWriter.getInstance(document, new FileOutputStream(file));

    }

    catch (FileNotFoundException e)

    {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    catch (DocumentException e)

    {

    // TODO Auto-generated catch block c://wangjin/qixiang/qixiang.jpg

    e.printStackTrace();

    }

    }

    public void openPdf()

    {

    document.open();

    }

    /**

    * 添加图片

    */

    public boolean addImage(Object imagePath)

    {

    String imagepath = (String)imagePath;

    try

    {

    Image img = Image.getInstance(imagepath);

    img.setAlignment(Image.LEFT);// 设置图片显示位置

    img.scalePercent(50);// 表示显示的大小为原尺寸的80%

    try

    {

    if (document != null)

    {

    document.add(img);

    }

    }

    catch (DocumentException e)

    {

    e.printStackTrace();

    }

    return true;

    }

    catch (Exception e)

    {

    e.printStackTrace();

    return false;

    }

    }

    /**

    * 添加条目

    */

    public boolean addItem(Object values,Font font)

    {

    try

    {

    ArrayList< > listValues = null;

    if (values != null && values != "")

    {

    listValues = (ArrayList< >)values;

    List list = new List(true, 20);// 创建列表

    ListItem item = null;

    for (int i = 0; i < listValues.size(); i++)

    {

    item = new ListItem(listValues.get(i)。toString(), font);// 创建列表项

    list.add(item);

    }

    document.add(list);

    return true;

    }

    else

    {

    return false;

    }

    }

    catch (DocumentException e)

    {

    // TODO Auto-generated catch block

    e.printStackTrace();

    return false;

    }

    }

    /**

    * 添加表格

    */

    @SuppressWarnings("unchecked")

    public boolean addTable(Object values, Object row, Object cell,Font font)

    {

    int tablecell = (Integer)cell;

    System.out.println(tablecell);

    PdfPTable table = new PdfPTable(tablecell);

    ArrayList tableValue=(ArrayList)values;

    table.setWidthPercentage(100);

    if (tableValue != null)

    {

    for (int i = 0; i < tableValue.size(); i++)

    {

    table.addCell(new Paragraph(tableValue.get(i)。toString(), font));

    }

    try

    {

    document.add(table);

    }

    catch (DocumentException e)

    {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    return true;

    }

    else

    {

    return false;

    }

    }

    /**

    * 添加文本

    */

    public boolean addText(Object values,Font font)

    {

    String text = (String)values;

    try

    {

    if (text != null && text != "")

    {

    Paragraph graph = new Paragraph(text,font);// 创建一个段落

    document.add(graph);

    return true;

    }

    else

    {

    return false;

    }

    }

    catch (Exception e)

    {

    // TODO Auto-generated catch block

    e.printStackTrace();

    return false;

    }

    }

    /**

    *

    * @param args

    */

    public void closePdf()

    {

    document.close();

    }

    /**

    * 开始新页

    */

    public void addNewPage()

    {

    try

    {

    document.newPage();

    }

    catch (DocumentException e)

    {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    //    public static void main(String[] args)

    //    {

    //        CreatePdfDocument pdfDocument=new CreatePdfDocument();

    //        String p1="    近几年来,业界逐渐推出了基于Web客户端的数据挖掘平台和产品,"

    //            + "如Kxen公司的数据挖掘产品,就是一个基于Web客户端的数据挖掘平台,该平台提供了简洁的网页"

    //            + "操作界面,利用向导一步一步的引导用户进行挖掘任务的设置、启动和系统维护。该方式已经逐步得"

    //            + "到了业界和客户的认可,已经成为一种公认的趋势。近几年来,业界逐渐推出了基于Web客户端的数据"

    //            + "挖掘平台和产品,例如Kxen公司的数据挖掘产品,就是一个基于Web客户端的数据挖掘平台,该平台"

    //            + "提供了简洁的网页操作界面,利用向导一步一步的引导用户进行挖掘任务的设置、启动和系统维护。"

    //            + "该方式已经逐步得到了业界和客户的认可,已经成为一种公认的趋势。";

    //        String fileSrc="c:\\test.pdf";

    //        pdfDocument.initPdfDocument(fileSrc);

    //

    //        pdfDocument.pdfOpen();

    //

    //        pdfDocument.addImage("D:\\图片\\2.jpg");

    //        pdfDocument.addText(p1);

    //        ArrayList arrayList=new ArrayList();

    //        for(int i=0;i<10;i++)

    //        {

    //            arrayList.add(i, "pdf"+i);

    //

    //        }

    //        pdfDocument.addItem(arrayList);

    //

    //        ArrayList<Field> fieldList=new ArrayList<Field>();

    //        for(int i=0;i<10;i++)

    //        {

    //            Field field=new Field(i,"username"+i,"password"+i,"telephone"+i);

    //            fieldList.add(field);

    //        }

    //        pdfDocument.addTable(fieldList, 10, 4);

    //

    //        pdfDocument.closePdf();

    //    }

    //

    //

    //

    }

    package com.isoftstone.inter.doc;

    import java.io.IOException;

    import com.lowagie.text.DocumentException;

    import com.lowagie.text.Font;

    /**

    *

    * <p>功能简述</p>

    * <p>功能详述</p>

    * @author Wang Jin

    * @version 1.0, 2012-10-15

    * @see

    * @since

    */

    public interface createDocument

    {

    /**

    * 初始化

    */

    public void initPdfDocument(Object filePath);

    /**

    *打开文档器

    */

    public void openPdf();

    /**

    * 添加图片

    */

    public boolean addImage(Object imagePath);

    /**

    * 添加条目

    */

    public boolean addItem(Object values,Font font);

    /**

    * 添加表格

    * @throws IOException

    * @throws DocumentException

    */

    public boolean addTable(Object values, Object row, Object cell,Font font);

    /**

    * 添加文本

    */

    public boolean addText(Object values,Font font);

    /**

    *关闭文档器

    */

    public void closePdf();

    }

    package com.isoftstone.parse;

    import java.io.File;

    import java.io.IOException;

    import java.util.ArrayList;

    import java.util.Iterator;

    import java.util.List;

    import org.dom4j.Document;

    import org.dom4j.DocumentException;

    import org.dom4j.Element;

    import org.dom4j.io.SAXReader;

    import com.isoftstone.impl.doc.CreatePdfDocument;

    import com.lowagie.text.Font;

    import com.lowagie.text.pdf.BaseFont;

    public class Dom4jToPdf

    {

    @SuppressWarnings( { "unchecked" })

    public static void main(String args[]) throws DocumentException

    {

    System.getProperties();

    SAXReader reader = new SAXReader();

    String filePath="d:/document.xml";

    Document document = reader.read(new File(filePath));

    Element rootElm = document.getRootElement();

    List documentNodes = rootElm.elements();

    for (Iterator documentNode = documentNodes.iterator(); documentNode.hasNext();)

    {

    Element documentElm = (Element)documentNode.next();

    CreatePdfDocument pdfDocument = new CreatePdfDocument();

    String fileSrc = "e:\\testwangjin.pdf";

    pdfDocument.initPdfDocument(fileSrc);

    pdfDocument.openPdf();

    if ("station".equals(documentElm.getName()))

    {

    try

    {

    String title = documentElm.attributeValue("title");

    BaseFont bfChinese = BaseFont.createFont(

    "C:\\WINDOWS\\Fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H,

    BaseFont.EMBEDDED);

    Font font = new Font(bfChinese, 12, Font.NORMAL);

    pdfDocument.addText(title, font);

    List contentNodes = documentElm.elements();

    for (Iterator contentNode = contentNodes.iterator(); contentNode.hasNext();)

          

首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇c++ 按行读取 (getline) 下一篇poj1523 解题报告

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: