1. 程式人生 > >java匯出word、pdf之新增頁首----頁首(指定格式,包括圖片和文字)

java匯出word、pdf之新增頁首----頁首(指定格式,包括圖片和文字)

doc.open();

// 新增頁首
   Image headerImage = Image.getInstance("f:\\1.jpg");
   headerImage.scaleAbsolute(36, 36);
   Paragraph headerPara1 = new Paragraph();
headerPara1.setAlignment(HeaderFooter.ALIGN_CENTER);
   Phrase headerPara = new Phrase();
   /**
    * 插入表格 三列二行
    */
   Table table = new Table(3);
   int width[] = { 2, 10, 6 };
   table.setWidths(width);
   table.setBorderWidth(1);
   table.setBorder(0);
   table.setBorderColor(Color.BLACK);
   table.setPadding(0);
   table.setSpacing(0);

   table.setWidth(100);
   Cell cell = new Cell(headerImage);// 單元格
   cell.setBorderWidth(0f); // 設定表格沒有邊框
   cell.setBorderWidthBottom(1);
   cell.setHeader(true);
   cell.setRowspan(2);// 當前單元格佔兩行,縱向跨度
   cell.setVerticalAlignment(Element.ALIGN_LEFT);
   table.addCell(cell);

   Cell cellHeader = new Cell(new Phrase("某個公司的名字"));
   cellHeader.setBorderWidth(0f);
   cellHeader.setColspan(2);
   cellHeader.setHorizontalAlignment(Element.ALIGN_LEFT);
   table.addCell(cellHeader);

   cellHeader = new Cell(new Phrase("編號"));
   cellHeader.setColspan(2);
   cellHeader.setBorderWidth(0f);
   cellHeader.setUseAscender(true);
   cellHeader.setHorizontalAlignment(Element.ALIGN_RIGHT); // 水平居中
   cellHeader.setVerticalAlignment(Element.ALIGN_BOTTOM); // 垂直居中
   table.addCell(cellHeader);

   headerPara.add(table);
   HeaderFooter header = new HeaderFooter(headerPara, false);

   header.setAlignment(HeaderFooter.ALIGN_CENTER);

   // HeaderFooter header = new HeaderFooter(new Phrase("頁首的測試"),
   // false);
   // HeaderFooter header = new HeaderFooter(new Phrase("頁首的測試0"), new
   // Phrase("頁首的測試2"));
   // header.setAlignment(Rectangle.ALIGN_RIGHT);
   doc.setHeader(header);

doc.close();