写在pdf里,我试了不可以阿。谁帮我写写。

解决方案 »

  1.   

    不好意思,记混了,为表达歉意,给个例子。
    /*
     * 创建日期 2004-12-10
     *
     * 更改所生成文件模板为
     * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
     */
    package xiaopeipei;
    /**
     * @author Administrator
     *
     * 更改所生成类型注释的模板为
     * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
     */
    import java.awt.Color;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import com.lowagie.text.*;
    import com.lowagie.text.pdf.*;
    public class xiaopeipei {
    public static void main(String[] args) throws MalformedURLException, IOException {
    Pn_itext_pdf pdfCreate = new Pn_itext_pdf();
    try {
    pdfCreate.createPDF();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (DocumentException e) {
    e.printStackTrace();
    }
    }
    public void createPDF() throws DocumentException, BadElementException, MalformedURLException, IOException {
    Document document = new Document(PageSize.A4);
    //document.addTitle("Title");
    //document.addHeader("header","Header");
    PdfWriter.getInstance(document, new FileOutputStream("C:/Helloworld.PDF"));
    document.open();
    //add a word
    document.add(new Paragraph("Hello World!您好!hehe!"));
    //add a table
    Table table = new Table(3);
    table.setBorderWidth(1);
    table.setBorderColor(new Color(0, 0, 255));
    table.setPadding(5);
    table.setSpacing(5);
    Cell cell = new Cell("header");
    cell.setHeader(true);
    cell.setColspan(3);
    table.addCell(cell);
    table.endHeaders();
    cell = new Cell("example cell with colspan 1 and rowspan 2");
    cell.setRowspan(2);
    cell.setBorderColor(new Color(255, 0, 0));
    table.addCell(cell);
    table.addCell("1.1");
    table.addCell("2.1");
    table.addCell("1.2");
    table.addCell("2.2");
    table.addCell("cell test1");
    cell = new Cell("big cell");
    cell.setRowspan(2);
    cell.setColspan(2);
    table.addCell(cell);
    table.addCell("cell test2");
    document.add(table);
    Image png = Image.getInstance("D:\\a.png");
    document.add(png);
    //BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    //com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
    //Paragraph pragraph=new Paragraph("你好", FontChinese);
    //document.add(pragraph);
    document.close();
    }
    }
      

  2.   

    实例化时候有错误,你自己改改阿。
    Pn_itext_pdf pdfCreate = new Pn_itext_pdf();
    这句。改成xiaopeipei pdfCreate = new xiaopeipei();
      

  3.   

    "你好"这样的当然是可以写到pdf里阿。可是我的问题是<strong>你好</strong> 怎么在pdf里显示成加粗的“你好”呢?呵呵,谢谢!
      

  4.   

    首先谢谢哥们们。但是就没有谁做过吗?api我研究过,可是一直没有结果。
      

  5.   

    现在有个iReport可以生成一个xml文件,然后用jasperreport解析,就可以生成任意表格,图形
      

  6.   

    com.lowagie.text.Document对象都有了,com.lowagie.text.Table对象也有阿,还有com.lowagie.text.Font,我没有试,也许是com.lowagie.text.Font font =new com.lowagie.text.Font();
    font.set...就像html一样的。
      

  7.   

    你需要自己解析<html>语句,然后调用此类的接口。
    或者itext组件中提供了这么一个接口:
    HtmlParser以下是例子
    (1)<html>代码
    <?xml version="1.0" encoding="GBK"?>
    <html>
    <head>
    <title>
    this is test for Html to pdf 
    </title>
    </head>
    <body>
    <table border="1" cellpadding="1" cellspacing="1" width="100%">
    <tr>
    <td width="20%">1</td>
    <td width="20%">test2</td>
    <td width="20%">3</td>
    <td width="20%">test4</td>
    <td width="20%">5</td>
    <td width="20%">test6</td>
    </tr>
    <tr width="20%">
    <td>zhejiangyidongyingye</td>
    <td>49</td>
    <td>40</td>
    <td>37</td>
    <td>26</td>
    <td>152</td>
    </tr>
    <tr width="20%">
    <td>zhejiangyidongshoufei</td>
    <td>4</td>
    <td>7</td>
    <td>12</td>
    <td>4</td>
    <td>27</td>
    </tr>
    <tr width="20%">
    <td>zhejiangyidongzhangwu</td>
    <td>4</td>
    <td>2</td>
    <td>123</td>
    <td>123</td>
    <td>6</td>
    </tr>
    <tr width="10%">
    <td>tuanduifuzhugongju</td>
    <td>5</td>
    <td>123</td>
    <td>111</td>
    <td>123</td>
    <td>5</td>
    </tr>
    <tr width="10%">
    <td>heji</td>
    <td>435</td>
    <td>457</td>
    <td>460</td>
    <td>425</td>
    <td>1777</td>
    </tr>
    </table>
    </body>
    </html>
    <script language="javascript">
    var abc=0;
    </script>(2)java代码
    package red.web.data.doc;import com.lowagie.text.*;
    import com.lowagie.text.pdf.PdfWriter;
    import com.lowagie.text.pdf.BaseFont;
    import com.lowagie.text.html.HtmlParser;
    import java.io.*;public class TstringListToPdf
    {
      public TstringListToPdf()
      {
      }
      public static void main(String args[])
      {
        //BaseFont bfComic = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        //Font font = new Font(bfComic, 12, Font.NORMAL);
        Document document = new Document(PageSize.A4, 1, 1, 1, 1);
        try
        {      
          FileInputStream bbb= new FileInputStream("a.htm");
          PdfWriter.getInstance(document,new FileOutputStream("C:/Documents and Settings/Administrator/桌面/outputstream/HtmlToPdf.pdf"));
          
          HtmlParser.parse(document,bbb);
        }
        catch (Exception e)
        {
        }
      }
    }
    不过这样做对中文的支持不是很友好。我现在也在考虑这个问题。