【问题标题】:how can I create a pdf using itext in java如何在java中使用itext创建pdf
【发布时间】:2017-05-23 10:28:46
【问题描述】:

我想使用 itext 创建一个 pdf,在我的 jsp 文件中,我创建了一个按钮,每当有人点击该按钮时,pdf 必须下载,大多数东西在这里都可以正常工作,但我无法添加我的表格内容,它在表格的位置显示为空。我的段落已添加,但在段落之后我想添加一个未添加的小表格。有人请帮我解决这些问题,这是我的代码:

/itext.jsp

function downloadfForm()
{
    document.location.href = './downloadPDF';
}


 <div class="col-md-3 col-sm-3">
          <div class="form-group">
              <label class="no-bold hidden hidden-md hidden-lg">&nbsp;</label>
              <a href="javascript:void(0)" class="btn btn-warning mutual-funds-submit-anchor" onclick="downloadfForm()">downloadfForm</a>             
          </div>          
          </div>

/itextcontroller.java

package com.itext.admin.controller;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.html.WebColors;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

@Controller

@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();

            document.add(new Paragraph(" asdfa hjsfvfaab asasjb ", blackBoldFont));
            document.add(new Paragraph("  fasasv  ", blackNormalFont));
            document.add(new Paragraph("  17th main, 23rd cross  ", blackNormalFont));
            document.add(new Paragraph("  savtfa Layout, Sector-III,  ", blackNormalFont));
            document.add(new Paragraph("  vchgasdasx - 50601022234  ", blackNormalFont));
            document.add(new Paragraph("  rtyrty lip  ", blackNormalFont));
            document.add(new Paragraph(" for february-2017  ", blackNormalFont));
            document.add(new Paragraph("  D vicky  ", blackNormalFont));    
            /* Upto here its working fine but below content not working */

                    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);

            table.addCell(cell);

                        /*This part is not adding*/

                        cell = new PdfPCell(new Paragraph("Broker/Agent Code ARN : ARN-96546",blackNormalFont));
                        cell.setPadding(5);
                        cell.setBorderColor(borderColor);
                        table.addCell(cell);

                        cell = new PdfPCell(new Paragraph("SUB-BROKER : ",blackNormalFont));
                        cell.setPadding(5);
                        cell.setBorderColor(borderColor);
                        table.addCell(cell);

                        cell = new PdfPCell(new Paragraph("EUIN : E123456",blackNormalFont));
                        cell.setPadding(5);
                        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();
                    }
         }

【问题讨论】:

    标签: java jsp spring-mvc pdf itext


    【解决方案1】:

    尝试使用 iText 库在 pdf 中创建表格。希望这对您有所帮助。

            PdfPTable table1 = new PdfPTable(4);
    
            float[] columnWidths1 = new float[]{30f, 10f, 30f, 10f};
            table1.setWidths(columnWidths1);
    
            table1.setWidthPercentage(100);
            table1.setHorizontalAlignment(table.ALIGN_JUSTIFIED);
            PdfPCell c1 = new PdfPCell(new Phrase("Details", blackNormalFont));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    
            c1.setBackgroundColor(BaseColor.GRAY);
            table1.addCell(c1);
    
            c1 = new PdfPCell(new Phrase("Amount (Rs)", blackNormalFont));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            c1.setBackgroundColor(BaseColor.GRAY);
            c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table1.addCell(c1);
    
            c1 = new PdfPCell(new Phrase("Deductions by HDBFS", blackNormalFont));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            c1.setBackgroundColor(BaseColor.GRAY);
            table1.addCell(c1);
    
            c1 = new PdfPCell(new Phrase("Amount (Rs)", blackNormalFont));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            c1.setBackgroundColor(BaseColor.GRAY);
    
            table1.addCell(c1);
    
            table1.setHeaderRows(1);
    
            table1.addCell(new Phrase("A. Product Cost", blackNormalFont));
    
            c1 = new PdfPCell(new Phrase(
                    CustomFormat.NUMBER_0.format(23233223), blackNormalFont));
            c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table1.addCell(c1);
    
            table1.addCell(new Phrase("1. Processing Fees", blackNormalFont));
            c1 = new PdfPCell(new Phrase(
                    CustomFormat.NUMBER_0.format(3232332), blackNormalFont));
            c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table1.addCell(c1);
    
    
            document.add(table1);
    
            document.close();
    

    【讨论】:

      猜你喜欢
      • 2016-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      相关资源
      最近更新 更多