pdf添加目录

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要给 PDF 添加目录,可以使用 Python 中的 PyPDF2 库。以下是一个简单的示例代码: ```python import PyPDF2 # 打开 PDF 文件 pdf_file = open('example.pdf', 'rb') pdf_reader = PyPDF2.PdfFileReader(pdf_file) # 创建 PDF writer 对象 pdf_writer = PyPDF2.PdfFileWriter() # 将 PDF 中的所有页面复制到新的 PDF 中 for page_num in range(pdf_reader.numPages): pdf_writer.addPage(pdf_reader.getPage(page_num)) # 创建目录 toc = PyPDF2.pdf.Outlines() toc_title = PyPDF2.pdf.OutlineTitle("Contents") toc.append(toc_title) # 添加目录项 for page_num in range(pdf_reader.numPages): page = pdf_reader.getPage(page_num) title = "Page {}".format(page_num + 1) dest = pdf_writer.addBookmark(title, page, parent=toc_title) # 将目录添加PDFpdf_writer.addBookmark("Table of Contents", 0, parent=None) pdf_writer.addBookmark(toc_title.title, 0, parent=None) pdf_writer.addPageLabel(0, PyPDF2.pdf.PageLabel(start=1, prefix='', numberingStyle=PyPDF2.pdf.PageLabelStyle.upperRoman)) pdf_writer.addOutline(toc) # 写入新的 PDF 文件 output_file = open('example_with_toc.pdf', 'wb') pdf_writer.write(output_file) # 关闭文件 output_file.close() pdf_file.close() ``` 这个代码会打开名为 `example.pdf` 的 PDF 文件,然后将其所有页面复制到一个新的 PDF 文件中。然后,它会创建一个目录,并为每一页添加一个目录项。最后,它将目录添加PDF 中,并将新的 PDF 文件保存为 `example_with_toc.pdf`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值