每页添加3行到IText文档

时间:2018-03-16 16:35:59

标签: c# itext

我希望从数据表中将每行3行输出到一个itextsharp文档中,但是它只是将所有行都集中到一个表中,有人可能会向我显示我的错误吗?

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

       response.setContentType("text/html;charset=UTF-8");

       String userName = request.getParameter("username");
       String password = request.getParameter("password");

       System.out.println(userName + " " +password);
       UserAccount userAccount = DataDAO.findUser(userName, password);

       if(userAccount == null) {
           System.out.println("no such username or password");
           String error = "Invalid User Name or Password";
           request.setAttribute("errorMessage", error);
           RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Home.jsp");
           dispatcher.forward(request,response);
           //response.sendRedirect("/Home.jsp");

           return;

       } else{
           System.out.println("bing return the user");
           HttpSession session = request.getSession();
           session.setAttribute("user",userAccount);
           //response.sendRedirect("/WEB_INF/index.jsp");
           //request.getRequestDispatcher("/WEB_INF/index.jsp").forward(request,response);
           RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/WEB-INF/index.jsp");
           dispatch.forward(request,response);
       }

    }

1 个答案:

答案 0 :(得分:1)

您的代码不会向循环内的文档添加任何内容,因此忽略document.NewPage()次调用。

简单地将实例化和文档添加到循环中:

//Loop each row in the dataset and output
for (int i = 0; i < repDT.Rows.Count; i += 3)
{
    //Create new table and set columns and widths for Report Items
    PdfPTable itemTable = new PdfPTable(4);
    float[] ITWidths = new float[] { 10f, 20f, 10f, 60f };  //4 columns by size (f) for list of multi-items on report
    itemTable.SetWidths(ITWidths);

    PdfPCell datesvalues = new PdfPCell(new Phrase("" + repDT.Rows[i]["OLDDATE"] + "\n" + Convert.ToDateTime(repDT.Rows[i]["INSDATE"]).ToString("dd/MM/yyyy") + "\n" + Convert.ToDateTime(repDT.Rows[i]["NEXT DATE"]).ToString("dd/MM/yyy") + "\n" + repDT.Rows[i]["CONT FREQ"].ToString() + " months", smalltext));
    itemTable.AddCell(datesvalues);
    PdfPCell itemdetails = new PdfPCell(new Phrase("1. " + repDT.Rows[i]["DESC"] + "\n" + "2. " + repDT.Rows[i]["PLANTNUMBER"] + "\n" + "3. " + repDT.Rows[i]["SERIALNUMBER"] + "\n" + "4. " + repDT.Rows[i]["SUBLOC"].ToString(), smalltext));
    itemTable.AddCell(itemdetails);
    PdfPCell swl = new PdfPCell(new Phrase("" + repDT.Rows[i]["SWL"], smalltext));
    itemTable.AddCell(swl);
    PdfPCell defects = new PdfPCell(new Phrase("A. " + repDT.Rows[i]["ADEFECT"] + "\n" + "B. " + repDT.Rows[i]["BDEFECT"] + "\n" + "C. " + repDT.Rows[i]["OBS"] + "\n" + "" + repDT.Rows[i]["SPARE1"].ToString(), smalltext));
    itemTable.AddCell(defects);

    document.Add(itemTable);

    document.NewPage();
}