这是我的代码
<%
Class.forName("com.mysql.jdbc.Driver");
class HeaderAndFooter extends PdfPageEventHelper {
private String name = "";
protected Phrase footer;
protected Phrase header;
PdfTemplate total;
Font headerFont = new Font(FontFamily.COURIER, 13, Font.UNDERLINE);
Font footerFont = new Font(FontFamily.HELVETICA, 11, Font.BOLD);
Font footerFont1 = new Font(FontFamily.TIMES_ROMAN, 9,Font.BOLDITALIC);
String date1;
public HeaderAndFooter(String name) {
super();
this.name = name;
header = new Phrase("***** Header *****");
footer = new Phrase("**** Footer ****");
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
String headerContent =name;
String footerContent = headerContent;
String log="C:/Users/Desktop/671.png";
float gap = (document.getPageSize().getWidth())/ 30;
float hei=(document.getPageSize().getHeight() - 60);
Image img1=null;
try{
String message="";
Connection con=null;
Statement st;
try{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/87a", "root", "root");
st=con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sqlText = "SELECT image FROM image";
ResultSet rset = st.executeQuery(sqlText);
while(rset.next()) {
Blob image = rset.getBlob("image");
img1= Image.getInstance(image.getBytes(1, (int) image.length()));
}
}catch (SQLException ex)
{
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
}
finally {
if (con != null) {
//closes the database connection
try {
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
Image img = Image.getInstance(img1);
//img.scalePercent((document.right()-document.left())/((float)img.getWidth())*100);
cb.addImage(img, 570, 0, 0, 60, gap, hei);
}catch(IOException e){}
catch(DocumentException e){}
ColumnText.showTextAligned(cb, Element.ALIGN_MIDDLE, new Phrase(String.format(" Page %d of", writer.getPageNumber()),footerFont), document.right() - 60 , document.bottom() - 15, 0);
}
}
OutputStream output=response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=details.pdf");
try{
Document document = new Document(PageSize.A4, 20, 20, 70, 20);
PdfWriter writer=PdfWriter.getInstance(document, output);
document.open();
BaseFont bf = BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
writer.setPageEvent(new HeaderAndFooter(date));
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
List arrlist = new ArrayList();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gla", "root", "root");
Statement st=con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=st.executeQuery("SELECT * FROM user_start2 where date='"+date+"' ");
while(rs.next()){
arrlist.add(rs.getString("data"));
}
document.add(new Paragraph(ds,new com.itextpdf.text.Font(bf, 12,Font.UNDERLINE)));
for(int i=0;i<4;i++){
String str =(String) arrlist.get(i);
document.add(new Paragraph(str,new com.itextpdf.text.Font(bf, 12)));
}
document.close();
}catch(IOException e){e.printStackTrace();}
writer.flush();
writer.close();
output.close();
%>
一切正常,除了我没有得到总页数。
我想念什么?
最佳答案
看起来您在onEndPage事件中正在尝试设置总页数。我不相信它会在那里工作(它可能会工作,但是要关闭一页)。我认为您必须等到pdf完整后再在页面总数上加盖印记。这是我在vb.net(w / itextsharp)中的操作方式;我敢打赌,您可以从中提取需要的东西:
''' <summary>
''' Adds the page numbers to the bottom right hand side of the page
''' </summary>
''' <param name="PdfFile">The pdf file that needs numbering</param>
''' <returns>A byte array containing the numbered pdf file</returns>
''' <remarks>This is a stamping operation</remarks>
Protected Shared Function AddPageNumbers(PdfFile As Byte()) As Byte()
'prepare font
Dim _bfTimes As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, False)
Dim mem As New LengthFixingStream()
Dim _reader As New PdfReader(PdfFile)
Dim _stamper As New PdfStamper(_reader, mem)
Dim PageCount As Integer = _reader.NumberOfPages
For i As Integer = 1 To PageCount
Dim _content As PdfContentByte = _stamper.GetOverContent(i)
_content.SaveState()
_content.BeginText()
_content.SetFontAndSize(_bfTimes, 8)
_content.SetTextMatrix(531, 18)
_content.ShowText(String.Format("Page {0} of {1}", i, PageCount))
_content.EndText()
_content.RestoreState()
Next
_stamper.Close()
AddPageNumbers = mem.GetBuffer()
End Function
如果您无法解决该问题,请发回。
该SO帖子解决了您的其他问题:Resize image