我怎样才能创建一个itext pdf

时间:2017-05-24 06:37:25

标签: itext

我无法创建pdf表,我想创建我的pdf:

 Wxbcvjh wsjedfs mxzxbc 
     fsdfhssds
  nzxvzz, 23213
  rgsdv, vuydsjbjh

_____________________________________
Emp Num: 122321   Tel Num: 1234
Fun:              Addr: hfsd,asddas
Loctn: jbfsd      DOJ: 12/12/12
_____________________________________

 ______________________________________
|_______asds_______________dbfjd_______|
|                  |                   |                   
|                  |                   |
|                  |                   |
|                  |                   |  
|__________________|___________________|______________________________________
|__sdf___|____erb__|________sfhsd______|_____nbnb___|____vbnvb___|__vbnvb_____|  
|        |         |                   |            |            |            |
|        |         |                   |            |            |            |
|        |         |                   |            |            |            |
|        |         |                   |            |            |            |
|________|_________|___________________|____________|____________|____________|

asdahs sehbwsdb:                                     sdb sjbfsbdfu sfnnjs 3tffds  

dfjhvsd jdfjb ksjdfsd                                             sdfbjbsdfb

我收到段落但无法完成表格,有人请求我解决这些问题的任何想法或参考,这是我的代码:

/pdfcontroller.java

@RequestMapping(value="/downloadPDF")
    public void downloadPDF(HttpServletRequest request, HttpServletResponse response) throws Exception
    {

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        try 
        {   
            Font blackNormalFont = new Font(Font.FontFamily.TIMES_ROMAN, 10f,Font.NORMAL, BaseColor.BLACK);
            Font blackBoldFont = new Font(Font.FontFamily.TIMES_ROMAN, 10f,Font.BOLD, BaseColor.BLACK);

            BaseColor backgroundColor = WebColors.getRGBColor("#FFF0F5");
            BaseColor borderColor = WebColors.getRGBColor("#000000");

            Rectangle pageSize = new Rectangle(PageSize.A4);
            pageSize.setBorder(1);
            pageSize.setBorderColor(borderColor);
            pageSize.setBackgroundColor(backgroundColor);

            Document document = new Document(pageSize,10,10,10,10);     
            String real_path = request.getServletContext().getRealPath("");
            String FILE = real_path + "download" + File.separator  + "test.pdf";    
            PdfWriter.getInstance(document, new FileOutputStream(FILE));
            document.open();

            PdfPTable table = new PdfPTable(3); // 3 columns.
            table.setWidthPercentage(100);
            table.setSpacingBefore(10f);
            table.setSpacingAfter(10f);            
            float[] columnWidths = {33f, 33f, 33f};
            table.setWidths(columnWidths);

            PdfPCell cell = new PdfPCell();
            cell.setPadding(5);
            cell.setColspan(3);
            cell.setBackgroundColor(backgroundColor);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

            Paragraph para1 =  new Paragraph("  Wxbcvjh wsjedfs mxzxbc  ", blackBoldFont);           
            para1.setAlignment(Element.ALIGN_CENTER);
            cell.addElement(para1);  

            Paragraph para2 =  new Paragraph( " fsdfhssds ", blackNormalFont);           
            para2.setAlignment(Element.ALIGN_CENTER);
            cell.addElement(para2);  

            Paragraph para3 =  new Paragraph("  nzxvzz, 23213 ", blackNormalFont);           
            para3.setAlignment(Element.ALIGN_CENTER);
            cell.addElement(para3);  

            Paragraph para4 =  new Paragraph(" rgsdv, vuydsjbjh ", blackNormalFont);           
            para4.setAlignment(Element.ALIGN_CENTER);
            cell.addElement(para4);  

            table.addCell(cell);
  /* This part is not getting */
            cell = new PdfPCell(new Paragraph("Emp Num : 122321",blackNormalFont));
            cell.setPadding(5);
            cell.setColspan(3);
            cell.setBorderColor(borderColor);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph(" Tel Num: 1234",blackNormalFont));
            cell.setPadding(5);
            cell.setColspan(3);
            cell.setBorderColor(borderColor);
            table.addCell(cell);


            document.add(table);

            document.close();

            File f=new File(FILE);
            System.out.println(FILE);           
            FileInputStream fin = new FileInputStream(f);
            ServletOutputStream outStream = response.getOutputStream();
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment;filename=test.pdf"); 
            byte[] buffer = new byte[1024];
            int n = 0;
            while ((n = fin.read(buffer)) != -1) 
            {
                outStream.write(buffer, 0, n);
            }
            outStream.flush();
            fin.close();
            outStream.close();

        }
        catch(Exception ex)
            {                   
                ex.printStackTrace();
            }
    }

0 个答案:

没有答案