-2
私のJSPファイルでitextを使用してpdfを作成したいのですが、誰かがそのボタンをクリックするたびにボタンを作成します。私は自分のテーブルの内容を追加することができません。そのテーブルの内容はテーブルの代わりに空になっています。私の段落が追加されますが、段落の後に追加していない小さなテーブルを追加したいと思います。誰かここで、これらの問題を解決するために私を助けてください、私のコードです:Javaでitextを使って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"> </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();
}
}