我正在尝试使用java中的itext将jtable写入pdf

时间:2016-03-15 07:07:14

标签: java

我想打印或写jtable内容,我的jtable有波斯语内容到PDF或打印所以它不显示波斯语内容信息:我使用的是itext v5。 IDE NetBeans 8

我的代码如下。

    try {
            Document doc = new Document(PageSize.A4.rotate());
            PdfWriter.getInstance(doc, new FileOutputStream("Bill_Report.pdf"));
            doc.open();
            Paragraph co = new Paragraph("Brothers Shadab Co.LTD", FontFactory.getFont(FontFactory.TIMES_BOLD, 24, Font.BOLD, BaseColor.RED));
            co.setAlignment(Element.ALIGN_CENTER);
            doc.add(co);

            BaseFont bf = BaseFont.createFont("C:\\Windows\\Fonts\\XB Yagut BdIt.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font a = new Font(bf, 28);
            Paragraph PP = new Paragraph("شرکت تجارتی برادران شاداب لمیتد ", a);
            PP.setAlignment(Element.ALIGN_CENTER);
            PdfPTable table = new PdfPTable(1);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell cell = new PdfPCell(PP);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setBorder(0);
            table.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
            table.addCell(cell);
            table.setHeaderRows(1);
            doc.add(table);
            Paragraph number = new Paragraph("mobile : 0799550250 - 0799037929 - 0777550250 ", FontFactory.getFont(FontFactory.TIMES_BOLD, 24, Font.BOLD, BaseColor.RED));
            number.setAlignment(Element.ALIGN_CENTER);
            doc.add(number);

            BaseFont add = BaseFont.createFont("C:\\Windows\\Fonts\\XB Yagut BdIt.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font a_add = new Font(add, 18);
            Paragraph padd = new Paragraph("مندوی داخل نواب مارکیت دست راست منزل دوم ", a_add);
            padd.setAlignment(Element.ALIGN_CENTER);
            padd.setIndentationRight(Element.ALIGN_CENTER);
            PdfPTable table_add = new PdfPTable(1);

            PdfPCell cell_add = new PdfPCell(padd);
            cell_add.setBorder(0);
            cell_add.setHorizontalAlignment(Element.ALIGN_CENTER);
            table_add.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
            table_add.addCell(cell_add);
            doc.add(table_add);
            table_add.setHorizontalAlignment(Element.ALIGN_CENTER);

            Image image = Image.getInstance("shadab.png");
            image.setAlignment(Element.ALIGN_CENTER);
            doc.add(image);
            doc.add(new Paragraph(" bill report", FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.RED)));
            doc.add(new Paragraph(" Date : " + new Date().toString()));

      `enter code here`      doc.add(new Paragraph("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"));

            PdfPTable pdfTable = new PdfPTable(jTable1.getColumnCount());
            //adding table headers

            for (int i = 0; i < jTable1.getColumnCount(); i++) {
                BaseFont t = BaseFont.createFont("C:\\Windows\\Fonts\\XB Yagut BdIt.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                Font tt = new Font(t, 18);
                String coll = jTable1.getColumnName(i);

                pdfTable.addCell(coll);

            }
            //extracting data from the JTable and inserting it to PdfPTable
            for (int rows = 0; rows < jTable1.getRowCount(); rows++) {
                for (int cols = 0; cols < jTable1.getColumnCount(); cols++) {
                    String result = (jTable1.getModel().getValueAt(rows, cols).toString());
                    pdfTable.addCell(result);
**// point  my problem is exactly in this place
/* other code that are in up and down work correctly but jtable content which has persian contents is not displayed */**

                }

            }
            doc.add(pdfTable);

            BaseFont bffadd = BaseFont.createFont("C:\\Windows\\Fonts\\XB Yagut BdIt.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font ffadd = new Font(bffadd, 18);
            Paragraph fpadd = new Paragraph("آدرس : سرای سه راهی حسینیه عمومی چنداول اسم سرایدار:  حاجی امان و خداداد ", ffadd);
            fpadd.setAlignment(Element.ALIGN_CENTER);
            fpadd.setIndentationRight(Element.ALIGN_CENTER);
            PdfPTable table_footer = new PdfPTable(1);
            PdfPCell cell_f = new PdfPCell(fpadd);
            cell_f.setBorder(0);
            cell_f.setHorizontalAlignment(Element.ALIGN_CENTER);
            table_footer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
            table_footer.addCell(cell_f);
            doc.add(table_footer);
            table_footer.setHorizontalAlignment(Element.ALIGN_CENTER);

            BaseFont b1 = BaseFont.createFont("C:\\Windows\\Fonts\\XB Yagut BdIt.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font f1 = new Font(b1, 18);
            Paragraph p1 = new Paragraph("بعد از 24  ساعت مسولیت بدوش خریدار می باشد ", f1);
            p1.setAlignment(Element.ALIGN_CENTER);
            p1.setIndentationRight(Element.ALIGN_CENTER);
            PdfPTable t1 = new PdfPTable(1);
            PdfPCell c1 = new PdfPCell(p1);
            c1.setBorder(0);
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            t1.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
            t1.addCell(c1);
            doc.add(t1);
            t1.setHorizontalAlignment(Element.ALIGN_CENTER);

            doc.close();
            JOptionPane.showMessageDialog(null, "گزارش شما موفقانه ثبت شد ");

        } catch (DocumentException ex) {
            Logger.getLogger(Shadab_HomePage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Shadab_HomePage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(solditems.class.getName()).log(Level.SEVERE, null, ex);
        }

1 个答案:

答案 0 :(得分:0)

尝试将波斯语文本用作图像对象,

                 int w = 400;
                 int h = 150;

PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(w, h);
Graphics2D g2 = tp.createGraphicsShapes(w, h);        
g2.drawString("your persian text", 20, 100); 
// put your persian text in place of "your persian text" ,above.              
g2.dispose();
cb.addTemplate(tp, 50, 400);