程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 文檔抽象模型xml,xml解析生成pdf,word文檔

文檔抽象模型xml,xml解析生成pdf,word文檔

編輯:C++入門知識

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <station title="stationParse" flag="true">
        <pargh font="chineseOne">
            <content><![CDATA[站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點
            能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析
            站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析
            站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析
            站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析
            站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析。]]>
            </content>   
        </pargh>
        <table row="2" cell="3" align="left">
            <tr>
                <td><![CDATA[站點能耗分析站1.]]></td>
                <td><![CDATA[站點能耗分析站2.]]></td>
                <td><![CDATA[站點能耗分析站3.]]></td>       
            </tr>
            <tr>
                <td><![CDATA[站點能耗分析站4.]]></td>
                <td><![CDATA[站點能耗分析站5.]]></td>
                <td><![CDATA[站點能耗分析站6.]]></td>       
            </tr>   
        </table>
        <image algin="left">
            <imagepath><![CDATA[D:\\圖片\\2.jpg]]></imagepath>   
        </image>
        <items font="chineseOne">
            <item><![CDATA[站點能耗分析站1.]]></item>       
            <item><![CDATA[站點能耗分析站2.]]></item>
            <item><![CDATA[站點能耗分析站2.]]></item>   
        </items>
    </station>
    <fangan title="stationParse" falg="false">
        <pargh font="chineseOne">
            <content><![CDATA[站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點
            能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析
            站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析
            站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析
            站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析
            站點能耗分析站點能耗分析站點能耗分析站點能耗分析站點能耗分析。]]>
            </content>   
        </pargh>
        <table row="2" cell="3" align="left">
            <tr>
                <td><![CDATA[站點能耗分析站1.]]></td>
                <td><![CDATA[站點能耗分析站2.]]></td>
                <td><![CDATA[站點能耗分析站3.]]></td>       
            </tr>
            <tr>
                <td><![CDATA[站點能耗分析站4.]]></td>
                <td><![CDATA[站點能耗分析站5.]]></td>
                <td><![CDATA[站點能耗分析站6.]]></td>       
            </tr>   
        </table>
        <image algin="left">
            <imagepath><![CDATA[D:\\圖片\\2.jpg]]></imagepath>   
        </image>
        <items font="chineseOne">
            <item><![CDATA[站點能耗分析站1.]]></item>       
            <item><![CDATA[站點能耗分析站2.]]></item>
            <item><![CDATA[站點能耗分析站2.]]></item>   
        </items>
    </fangan>
</document>
package com.isoftstone.impl.doc;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import com.isoftstone.domain.doc.Field;
import com.isoftstone.inter.doc.createDocument;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.List;
import com.lowagie.text.Paragraph;

import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class CreatePdfDocument implements createDocument
{
    private Document document;   
    public CreatePdfDocument()
    {       
       
    }   
    /**
     * 初始化
     */
    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();)
                    {
                        Element contentElm = (Element)contentNode.next();
                        if ("pargh".equals(contentElm.getName()))
                        {
                            String p1 = contentElm.elementText("content");
//                            elmContent.setAttributeValue(title, )
                            pdfDocument.addText(p1, font);
                        }
                        if ("image".equals(contentElm.getName()))
                        {
                            String imagePath = contentElm
                                .elementText("imagepath");

                            pdfDocument.addImage(imagePath);
                        }
                        if ("items".equals(contentElm.getName()))
                        {
                            List itemNodes = contentElm.elements();
                            List listValues = new ArrayList();
                            for (Iterator itemNode = itemNodes.iterator(); itemNode.hasNext();)
                            {
                                Element itemContent = (Element)itemNode.next();
                                String itemData = (String)itemContent.getData();
                                listValues.add(itemData);
                            }
                            pdfDocument.addItem(listValues, font);
                        }
                        if ("table".equals(contentElm.getName()))
                        {
                            List tableValues = new ArrayList();
                            int tableCells = Integer.parseInt(contentElm
                                .attributeValue("cell"));
                            int tableRows = Integer.parseInt(contentElm
                                .attributeValue("row"));
                            List trNodes = contentElm.elements();
                            for (Iterator trNode = trNodes.iterator(); trNode.hasNext();)
                            {
                                Element trContent=(Element)trNode.next();
                                List tdNodes = trContent.elements();
                                for (Iterator tdNode = tdNodes.iterator(); tdNode
                                .hasNext();)
                                {
                                    Element tdContent=(Element)tdNode.next();
                                    String tdData=(String)tdContent.getData();
                                    tableValues.add(tdData);
                                }
                            }
                            pdfDocument.addTable(tableValues, tableRows, tableCells,font);
                        }
                    }
                   
                }
                catch (com.lowagie.text.DocumentException e)
                {                  
                    e.printStackTrace();
                }
                catch (IOException e)
                {                   
                    e.printStackTrace();
                }
                finally
                {
                    pdfDocument.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();)
                    {
                        Element contentElm = (Element)contentNode.next();
                        if ("pargh".equals(contentElm.getName()))
                        {
                            String p1 = contentElm.elementText("content");
//                            elmContent.setAttributeValue(title, )
                            pdfDocument.addText(p1, font);
                        }
                        if ("image".equals(contentElm.getName()))
                        {
                            String imagePath = contentElm
                                .elementText("imagepath");

                            pdfDocument.addImage(imagePath);
                        }
                        if ("items".equals(contentElm.getName()))
                        {
                            List itemNodes = contentElm.elements();
                            List listValues = new ArrayList();
                            for (Iterator itemNode = itemNodes.iterator(); itemNode.hasNext();)
                            {
                                Element itemContent = (Element)itemNode.next();
                                String itemData = (String)itemContent.getData();
                                listValues.add(itemData);
                            }
                            pdfDocument.addItem(listValues, font);
                        }
                        if ("table".equals(contentElm.getName()))
                        {
                            List tableValues = new ArrayList();
                            int tableCells = Integer.parseInt(contentElm
                                .attributeValue("cell"));
                            int tableRows = Integer.parseInt(contentElm
                                .attributeValue("row"));
                            List trNodes = contentElm.elements();
                            for (Iterator trNode = trNodes.iterator(); trNode.hasNext();)
                            {
                                Element trContent=(Element)trNode.next();
                                List tdNodes = trContent.elements();
                                for (Iterator tdNode = tdNodes.iterator(); tdNode
                                .hasNext();)
                                {
                                    Element tdContent=(Element)tdNode.next();
                                    String tdData=(String)tdContent.getData();
                                    tableValues.add(tdData);
                                }
                            }
                            pdfDocument.addTable(tableValues, tableRows, tableCells,font);
                        }
                    }
                   
                }
                catch (com.lowagie.text.DocumentException e)
                {                  
                    e.printStackTrace();
                }
                catch (IOException e)
                {                   
                    e.printStackTrace();
                }
                finally
                {
                    pdfDocument.closePdf();
                }
            }

        }
    }

}

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved