pdf添加目录

来源:互联网 发布:软件开发个人简历 编辑:程序博客网 时间:2024/05/16 04:51
public class pdfML {
//  ----------------------------------------------------
public static final String CHARACTOR_FONT_CH_FILE = "SIMHEI.TTF";  //黑体常规  
public static int i=0;
public static void main(String[] args) {


//第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 10, 10, 50, 30);  
try {  


PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("D:/merged.pdf"));  
// step 3: we open the document  
writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);  
document.open();  
//*****  String str=new String(pdf.getBytes("ISO-8859-1"),"GB2312");
//   Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL, new BaseColor(255, 0, 0));  
//   Font sectionFont = FontFactory.getFont(FontFactory.HELVETICA, 20, Font.NORMAL, new BaseColor(0, 0, 255));  
//    Font subsectionFont = FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD, new BaseColor(0, 64, 64));  
BaseFont bfComic = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//BaseFont bfComic = BaseFont.createFont(CHARACTOR_FONT_CH_FILE,BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font1 = new Font(bfComic, 24,Font.NORMAL);
Font font2 = new Font(bfComic, 20,Font.NORMAL);
Font font3 = new Font(bfComic, 10,Font.NORMAL);
// we create some paragraphs  
JDBCUtil tUtil = new JDBCUtil();
//查询公告类型(参数公告类型)
String annTypeSql="select h.ann_type_code from tmu_tmedms.t_tmedms_tm_ann_search h where h.ann_num=? group by h.ann_type_code";
List<String> typeList= new ArrayList<String>();
ResultSet queryType=tUtil.doQuery(annTypeSql,new Object[]{"1526"});
while(queryType.next()){
typeList.add(queryType.getString(1));
}
tUtil.close(queryType);
System.out.println("公告类型数量:"+typeList.size());
//**************************************
//查询所有公告内容--------------------


String sql = "select * from (select f.content from tmu_tmedms.t_tmedms_tm_anno_files f where f.doc_id in" +
" (select t.id from tmu_tmedms.t_tmedms_tm_ann_info t where t.ann_num='1526' and t.ann_type_code= ?) order by f.doc_no) where rownum<101 ";
            //查询单个公告内容-------------------
String oneSql="select a.content from (select rownum as rn, t.* from (select * from tmu_tmedms.t_tmedms_tm_anno_files f "+
                " where f.doc_id in (select t.id from tmu_tmedms.t_tmedms_tm_ann_info t where t.ann_num = '1526'"+
                          " and t.ann_type_code =?) order by f.doc_no) t) a where rn =?";
//查询该类型公告总数量
String countSql="select max(h.page_num) from tmu_tmedms.t_tmedms_tm_ann_info h where h.ann_type_code=? and h.ann_num='1526'";
//查询杂文公告注册号-----------------------
String regNumSql="select h.reg_num from tmu_tmedms.t_tmedms_tm_ann_search h,"+
"(select t.ann_num, t.ann_type_code from tmu_tmedms.t_tmedms_tm_anno_files f,"+
" tmu_tmedms.t_tmedms_tm_ann_info t where f.doc_id = t.id and t.ann_num = '1526'"
+" and f.doc_no = ? and t.ann_type_code = ? order by f.doc_no) g"+
" where h.ann_num = g.ann_num  and h.ann_type_code = g.ann_type_code and h.page_no = ? ";


//**************************************
//----------------------------------------------
for (int i = 18; i <typeList.size(); i++) {  
String type="";
if(typeList.get(i).equals("TMZCSQ")){
type="商标初步审定公告";
}else if(typeList.get(i).equals("TMXZSQ")){
type="注册商标续展公告";
}else{
type="注册商标撤销公告";
}


Paragraph cTitle = new Paragraph("第1526期:"+type, font1);
Chapter chapter = new Chapter(cTitle,i);
//设置章节是否带序号
chapter.setNumberDepth(0);
//计算该类型公告总数量
int countAnno=0;
ResultSet count = tUtil.doQuery(countSql,new Object[]{typeList.get(i)});
if (count.next()) {
countAnno=count.getInt(1);
}
System.out.println(type+"公告总数量:"+countAnno);
tUtil.close(count);
//加入小节内容
for (int j = 1; j <=countAnno; j++) {
if(countAnno>200&&j==200){
break;
}
byte[] content1=null;
ResultSet queryAnno = tUtil.doQuery(oneSql,new Object[]{typeList.get(i),j});
if(queryAnno.next()) {
content1=queryAnno.getBytes(1);
}
tUtil.close(queryAnno);
//查询注册号
ResultSet query2=tUtil.doQuery(regNumSql,new Object[]{j,typeList.get(i),j});
List<String> regNumList=new ArrayList<String>();
while(query2.next()){
regNumList.add(query2.getString(1));
}
tUtil.close(query2);
System.out.println(typeList.get(i)+":第"+j+"个");
System.out.println("注册号数量:"+regNumList.size());
//转成图片数组
byte[] imageByte=pdfToImageTest(content1);
//生成图片
Image jpg = Image.getInstance(imageCut(imageByte));
jpg.setAlignment(Image.ALIGN_CENTER);  
jpg.scaleToFit(700,670);
//将图片加入章节
Paragraph sTitle =null;
if(regNumList.size()>0){
sTitle=new Paragraph("第"+regNumList.get(0)+"号", font2);
}
Section section = chapter.addSection(sTitle,1);  
section.setIndentationLeft(0); 
section.setNumberDepth(0);
//如果不是初审公告,将注册号连起来显示
if(!typeList.get(i).equals("TMZCSQ")){
String regStr="  ";
for (String str : regNumList) {
regStr+=str+";";
}
//缩放图片
if(regNumList.size()==0){
jpg.scaleToFit(700,700);
}else {
jpg.scaleToFit(700,665);
}
section.add(new Paragraph(regStr,font3));
}
// section.add(new Paragraph(list.get(i-1).getDocTypeCode())); 
//  sTitle.add(new Paragraph("lalalalalallalal"));
//  section.add(new Paragraph(list.get(i-1).getDocTypeCode()+";"+jpg));
//InputStream is=new ByteInputStream(content1,0,content1.length);
// section.add(is);
//section.add((Element) doc);

document.add(chapter);  
}  
}  
catch(Exception de) {  
de.printStackTrace();  
}  
// step 5: we close the document  
document.close();  




//   -----------------------------------------------------
}
/**
* pdf转图片

* @param b
* @return
*/
public static byte[] pdfToImg(byte[] pdfByte, float scale)
throws IOException
{
byte[] imageByte = null;
org.icepdf.core.pobjects.Document document = null;
float rotation = 0.0F;
ByteArrayOutputStream out = null;
try {
document = new org.icepdf.core.pobjects.Document();
document.setByteArray(pdfByte, 0, pdfByte.length, null);
out = new ByteArrayOutputStream();
BufferedImage img = (BufferedImage)document.getPageImage(0, 1, 2, rotation, scale);


RenderedImage rendImage = img;

ImageIO.write(rendImage, "png", out);
img.flush();
out.flush();


imageByte = out.toByteArray();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
out.close();
document.dispose();
}


return imageByte;
}
0 0
原创粉丝点击