java 生成pdf

来源:互联网 发布:淘宝店铺快递费 编辑:程序博客网 时间:2024/05/22 05:18

public class outpdf {
 
private Document doc=null;

private shoutpdf sh=null;

private String path="";

private  BaseFont basefont=null;

     outpdf(String ph) throws FileNotFoundException,Exception{
     
     basefont= BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //中文需要

  this.setPath(ph);  
  doc=new Document(PageSize.A4);
  doc.addTitle("xxxxxx");
  doc.addAuthor("xxxx");
  PdfWriter.getInstance(doc,new FileOutputStream(this.getPath()));
  doc.open();
  Font font=new Font(basefont,23,Font.BOLD);
 
  Paragraph chunk1 = new Paragraph("xxxxxx", font);   
   
  chunk1.setAlignment(chunk1.ALIGN_CENTER);
     doc.add(chunk1);
    
    
    }
   
   
     outpdf(String ph,shoutpdf sh) throws FileNotFoundException, ShOutPdfException, Exception{
    this(ph);
    this.setSh(sh);
    shoutpdf shou=  this.getSh();
    if(shou==null)throw new ShOutPdfException("pdf文本内容为空",1001);
     
    }
    public void Read() throws ShOutPdfException, Exception{
     
     
       if(sh==null)throw new ShOutPdfException("pdf文本内容为空",1001);
          Font font=new Font(basefont,14,Font.BOLD);
          Chunk chunk1 = new Chunk(this.sh.getTest_sname(), font);
          chunk1.append("                "+this.sh.getTest_snumber()+"             "+this.sh.getTest_speciaty() +"/n");
          chunk1.append(this.sh.getCourse_name() +"             "+this.sh.getCourse_number());
          Paragraph pg=new Paragraph(chunk1);
          pg.setAlignment(pg.ALIGN_LEFT);
             doc.add(pg);
         
             //设置总分
             Font fontscore=new Font(basefont,17,Font.UNDERLINE);
             fontscore.setStyle(basefont.FONT_TYPE_TT);
             Paragraph pgscore=new Paragraph(this.sh.getTest_socre(),fontscore);
             pgscore.setAlignment(pg.ALIGN_RIGHT);
          doc.add(pgscore);
         
         
         ArrayList<shSubjectType> sst=this.sh.getSst();
         String [] big={"一","二","三","四","五","六","七","八","九","十"};
         if(sst.size()<10){
          Font fontsubject=new Font(basefont,14,Font.BOLD);
           Paragraph parag=null;
           List lis=new List(true,100);
         for(int i=0;i<sst.size();i++){
          ArrayList<shSubjectContent> sujcontent=new ArrayList<shSubjectContent>();
          shSubjectType ssti=new shSubjectType();
            ssti=sst.get(i); 
               parag=new Paragraph(big[i]+"、"+ssti.getSubject_name()+":",fontsubject); 
              System.out.println(big[i]+"、"+ssti.getSubject_name());
             
              parag.setAlignment(parag.ALIGN_LEFT);
              doc.add(parag);//添加题目类别如基础题,专业题
             
              String[] str=ssti.getSubject_type();
              sujcontent=   ssti.getSuject_content();
              for(int x=0;x<str.length;x++){
                if(str[x]==null) break;
                parag=new Paragraph(str[x],fontsubject);  //添加题目类型如判断题选择题
                System.out.println(str[x]);
                doc.add(parag);
            
                for(int y=0;y<sujcontent.size();y++){
                 shSubjectContent tg=new shSubjectContent();
                 tg=sujcontent.get(y);
                 Font sjc=new Font(basefont,12,Font.NORMAL);
                 if(tg.getSubject_type().equals(x+"")){
                 StringBuffer result= new StringBuffer();
                 if(tg.getContent_color().equals(Color.red)){
                 result.append("×  ");
               }else{
                 result.append("√  ");
               }
                 result.append(tg.getContent_number());
                 result.append(tg.getContent_text());
                 result.append("  ");
                 result.append(tg.getContent_score());
                 result.append("    ");
                   result.append(tg.getContent_user_answer());
                   result.append("    ");
                   result.append(tg.getContent_key());
                 result.append("/n");
                 sjc.setColor(tg.getContent_color());
                 sjc.setStyle(Font.NORMAL);
                    parag=new Paragraph(result.toString(),sjc);  //添加题目
                    System.out.println(str[x]);
                    result.delete(0, result.length());
                    doc.add(parag);
                 }
                 
                }
             
              }
         }
         
     
         }
      
         
     
     
     
     
     
           this.destroy();
    }
    private void destroy(){
     this.doc.close();
    }

/**
 * @return doc
 */
    private Document getDoc() {
 return doc;
}

/**
 * @param doc 要设置的 doc
 */
    private void setDoc(Document doc) {
 this.doc = doc;
}

/**
 * @return path
 */
    private String getPath() {
 return path+".pdf";
}

/**
 * @param path 要设置的 path
 */
    private void setPath(String path) {
 this.path = path;
}

/**
 * @return sh
 */
    public shoutpdf getSh() {
 return sh;
}

/**
 * @param sh 要设置的 sh
 */
    public void setSh(shoutpdf sh) {
 this.sh = sh;
}

}

 

 

 

不能直接运行

原创粉丝点击