运用java操作PDF,生成PDF

来源:互联网 发布:手机微信监控软件 编辑:程序博客网 时间:2024/05/17 06:20
参考文档地址:
http://wenku.baidu.com/view/c3360006de80d4d8d15a4f6f.html


http://wenku.baidu.com/link?url=vwIcd482JeD1jp_lfNNX7Nc4BJ6rlOm41tX0gONl3x8lfOUW0s4t9GjPuXLuIS9b3G3Inb17MgiCqlt6wVAmZ28u6N0MIlPLtv-MSwbPNTe

1.一个简单的列子,不能包含汉子

     (1).第一步:建立一个document对象
                    Docuemnt document = new Document();
      (2).第二步.创建一个Writer来监听document,并指向一个输出文件流,这里是PdfWriter
                     PdfWriter.getInstance(document, new FileOutputStream("C:/Users/Administrator/Desktop/test1.pdf"));
      (3).打开一个文档
                     document.open();
       (4).我自己加了一个subject和title,结果发现没看见效果,郁闷!,原来他们的返回值都是boolean,所以只有在文件的文件属性中才能看到信息,还可以其他的信息
                      document.addTitle("title");
                      document.addSubject("subject");
        (5).给docuemnt文档添加一个段落Paragraph内容
                       document.add(new Paragraph("this is a pdf text"));
                              
         (6).关闭文档
                        document.close();
完整程序如下:

public class PdfTest_1 {
     public static void main(String[] args) {
          System.out.println("this is the first process!");
          Document docuemt = new Document();
          try {
               PdfWriter.getInstance(docuemt, new FileOutputStream("C:/Users/Administrator/Desktop/test1.pdf"));
               docuemt.open();
               docuemt.addTitle("title");
               docuemt.addSubject("subject");
               docuemt.add(new Paragraph("this is a pdf text"));
          } catch (FileNotFoundException e) {
               e.printStackTrace();
          } catch (DocumentException e) {
               e.printStackTrace();
          }
          System.out.println("end");
          docuemt.close();
         
     }

}


2.根据文档学习内容如下:

package pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Annotation;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Header;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfTest {
     public static void main(String[] args) throws Exception {
          System.out.println("this is the PDF process!");
          //设置文档大小格式
          Rectangle pageSize = new Rectangle(114,720);
          //设置背景颜色
          pageSize.getBackgroundColor();//无效果
          // pageSize.setBackgroundColor(BaseColor.RED);
          //Document document = new Document(pageSize);
          Document document = new Document(PageSize.A4);
          //当创建一个文件时,定义上、下、左、右页边距:
          //Document document = new Document(pageSize, 10, 10, 10, 10);
         
          try {
               PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/Administrator/Desktop/test1.pdf"));
               //PdfWriter w = PdfWriter.getInstance(document, System.out); //在控制台输出信息---
               //w.setCloseStream(false);  //不能关闭
               //增加文档的一些说明,返回值为boolean,在文档的文档属性中可以看到,这些摘要设置需要在文档打开之前设置
               //但是为什么我在打开之后设置也可以?

//               writer.CenterWindow = 1;
////               PdfWriter.NonFullScreenPageModeUseNone = 1;//既不显示大纲也不显示缩略图
//               writer.NonFullScreenPageModeUseOC  =1;
//               writer.NonFullScreenPageModeUseOutlines = 1;//显示大纲
//               writer.NonFullScreenPageModeUseThumbs = 1;//显示缩略图
//               writer.FitWindow = ;
               document.open();
               Chunk chunk = new Chunk("this is a chunk",FontFactory.getFont(FontFactory.TIMES_BOLD, 20,Font.ITALIC ,BaseColor.RED));
               Chunk chunk1 = new Chunk("this is a chunk1!",FontFactory.getFont(FontFactory.HELVETICA,20,Font.UNDEFINED,BaseColor.BLUE));
               // chunk.setTextRise(2.00);
               chunk.setBackground(BaseColor.BLUE);
               chunk1.setBackground(BaseColor.PINK);
               document.setPageCount(1);
               document.getPageNumber();
               document.addTitle("title");
               document.addSubject("subject");
               document.addAuthor("mushixing");
               document.addCreationDate();
               document.addCreator("mushixing");
               document.addHeader("header","i am a header");
               document.addKeywords("PDF,java,lixin");
               document.addLanguage("chinese,english");
               document.addProducer();
              
              
              
               //创建一个锚点
               Anchor anchor = new Anchor("baidu", FontFactory.getFont(FontFactory.TIMES_ROMAN, 30, Font.UNDERLINE, BaseColor.YELLOW));
               anchor.setName("baidu");
               anchor.setReference("www.baidu.com");

               document.add(chunk);//文档中添加一个块
               document.add(chunk1);//文档中添加另一个块
               anchor.add(chunk);
               document.add(anchor);
               document.add(new Phrase(chunk1));//文档中添加一个短句
               document.add(new Paragraph("this is a pdf text"));//文档中添加一个段落
               document.add(new Paragraph(chunk));//文档中添加一个段落。段落中添加一个块
               document.add(new Paragraph(new Phrase(chunk1)));//文档中添加一个段落,段落中添加一个短句,短句中添加一个块
              
              
              
               //加入序列----这里有问题哦。  
               //List list = new List(true,20);//需要排序,以数字排序
               List list = new List(false,20);//不需要排序,以"-"打头
               //list.setListSymbol("*");   //设置排序标志
               list.setListSymbol(new Chunk("**", FontFactory.getFont(FontFactory.SYMBOL ,15, Font.ITALIC, BaseColor.BLUE)));  //设置标志为字符
               list.setListSymbol(new Chunk(Image.getInstance("C:\\Users\\Administrator\\Desktop\\杂项\\star.jpg"), 0, 0));
               list.add(new ListItem("this is the first list!"));
               list.add(new ListItem("this is the second list!"));
               list.add(new ListItem(new Chunk("this is a chunk list!")));
               list.add(new ListItem(new Phrase("this is a Phrase list")));
               System.out.println(list.getFirst());//getFirst---1
               //System.out.println(list.getFirstItem());//getFirstItem----this is the first list!
               //System.out.println(list.getChunks()); //得到了全部的内容
               document.add(list);
              
              
              
               //增加页眉,为什么没有效果!
               Header header = new Header("head", "i am a header!");
               document.add(header);
              
              
              
               //增加章节和区域,为什么出现重复的section
//               Chapter chapter = new Chapter("this is a paragraph", 1);
//               Chapter chapter1 = new Chapter("this is title", 1);
//               Chapter chapter2 = new Chapter(1);
               Paragraph ptitle = new Paragraph("this is a paragraph",FontFactory.getFont(FontFactory.HELVETICA_BOLD, 20, Font.UNDEFINED,BaseColor.RED));
               Chapter chapter3 = new Chapter(ptitle, 5);
               Paragraph stitle = new Paragraph("this is a section",FontFactory.getFont(FontFactory.HELVETICA_BOLD, 20, Font.UNDEFINED,BaseColor.RED));
               Paragraph sstitle = new Paragraph("this is a stitle section",FontFactory.getFont(FontFactory.HELVETICA_BOLD, 20, Font.UNDEFINED,BaseColor.RED));
               //在章节中添加区域
               //Section section = chapter3.addSection(ptitle);
               Section section1 = chapter3.addSection(stitle);
               Section section2 = section1.addSection(sstitle);
               Section section3 = chapter3.addSection("this is a string section");
               Section section4 = section3.addSection("this is a stitle stitle section");
               document.add(section1);
               document.add(section2);
               document.add(section3);
               document.add(section4);
//               document.add(chapter);
//               document.add(chapter1);
//               document.add(chapter2);
              
              
              
               //添加图形
              
              
              
              
               //添加表格
               PdfPTable table = new PdfPTable(6);
              
               //如果不用PdfPCell的话,那么就只会有6个表格列,不会自动添加多余的值到下一行
//               PdfPCell cell = new PdfPCell(new Paragraph("..........."));
               PdfPCell cell = new PdfPCell();
               System.out.println(cell.getBorderColor());
               cell.setBackgroundColor(BaseColor.GRAY);
               cell.setBorder(10);
               cell.setBorderColor(BaseColor.RED);
               cell.setBorderColorBottom(BaseColor.BLUE);
               cell.setBorderColorLeft(BaseColor.GREEN);
               cell.setBorderColorRight(BaseColor.PINK);
               cell.setBorderColorTop(BaseColor.RED);
               //cell.setColspan(2);//合并两列
               cell.setPhrase(new Phrase("orgNo"));
               table.addCell(cell);
               table.addCell("string");
               table.addCell(new Paragraph("paragraph"));
               table.addCell(new Paragraph("1"));
               table.setTotalWidth(400);
               table.addCell("2");
               table.addCell("10");
               table.addCell("4");
               table.addCell("5");
               table.addCell("6");
               table.addCell("7");
               table.addCell("8");
               table.addCell("9");
               table.addCell("10");
               table.addCell("4");
               table.addCell("5");
               table.addCell("6");
               table.addCell("7");
               table.addCell("8");
               table.addCell("9");
               table.addCell("10");
               table.addCell("4");
               table.addCell("5");
               table.addCell("6");
               table.addCell("7");
               table.addCell("8");
               table.addCell("9");
               table.setHorizontalAlignment(10);
               document.add(table);
              
              
               //添加图片
               Image image = Image.getInstance("C:\\Users\\Administrator\\Desktop\\beaute.jpg");         
               image.setAlignment(1);
               document.add(image);
              
              
              
               //加入注释
               Annotation annotation = new Annotation("annotation", "this is a annotation!");
               Annotation annotation1 = new Annotation(100f,700f,200f,800f,new URL("http://www.baidu.com"));
               Annotation annotation2 = new Annotation(100f,700f,200f,800f,"http://www.baidu.com");
               document.add(annotation);
               document.add(annotation1);
               document.add(annotation2);
               //设置文档文件大小格式
               /*document.setPageSize(PageSize._11X17);
               document.newPage();
               document.add(new Paragraph("this is a pdf pagesize 11*17"));
               document.setPageSize(PageSize.A0);
               document.newPage();
               document.add(new Paragraph("this is a pdf pagesize A0"));
               document.setPageSize(PageSize.A1);
               document.newPage();
               document.add(new Paragraph("this is a pdf pagesize A1"));
               document.setPageSize(PageSize.FLSA);
               document.newPage();
               document.add(new Paragraph("this is a pdf pagesize flsa"));



               /*//新建一个页面并设置格式和内容
               //设置文档横向排列或者直接Document document = new Document(PageSize.A4.rotate());
               document.setPageSize(PageSize.A4.rotate());
               document.newPage();//新建一个页面
               document.add(new Paragraph("this is a pdf text"));*/
              
              
              
              
               /*
               * 为什么出不来,请给我一个理由。。。。。。。。。。啊!啊!啊!。。。。。。。。。。

               * */
               // 创建中文字体 
               BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UTF32-H", BaseFont.NOT_EMBEDDED);
               Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
               // 设置为中文 
               Paragraph pragraph=new Paragraph("你好,这是中文",FontChinese);
               document.add(pragraph);
              

          } catch (FileNotFoundException e) {
               e.printStackTrace();
          } catch (DocumentException e) {
               e.printStackTrace();
          }
          System.out.println("end");
          document.close();
         
     }

}




//这就是出来的结果

























0 0
原创粉丝点击