Java itext无法在windows下创建pdf,在ubuntu上运行正常

时间:2011-05-12 13:28:49

标签: java windows itext

我有

public class Pdf {

    private static float FONTSIZE = 8;


    public void open(String filename) throws Exception
    {
        /*
        File f = new File(PATH + filename);
        if(f.exists() == true)
        {
            Runtime.getRuntime().exec("gnome-open " + PATH + filename);
            //Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler C:/pdf/test.pdf"); // WINDOWS
        }
        else
        {
            init(filename);
        }
        */
        init(filename +".pdf");
        Runtime.getRuntime().exec("gnome-open " + Config.PATH + filename +".pdf");
    }

    public void init(String filename) throws Exception
    {
        Document doc = new Document(PageSize.A4);
        doc.setMargins(10, 10, 10, 10);

        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(Config.PATH + filename));

        doc.open();
        doc.addTitle("incasation");

        PdfPTable header = new PdfPTable(5);
        float[] widths = {5.6f, 6, 8, 5, 5};
        header.setWidths(widths);
        header.setHorizontalAlignment(Element.ALIGN_CENTER);
        header.setWidthPercentage(100);
        header.setSpacingAfter(4);

        header.addCell(cell("Data: 20.04.2011", 0));
        PdfPCell pkt = cell("Punct de lucru: COM COM COM COM COM COM COM COM COM COM COM COM COM COM COM", 0);
            pkt.setColspan(2);
        header.addCell(pkt);
        PdfPCell cmp = cell("Company: SC ANC CNE", 0);
            cmp.setColspan(2);
        header.addCell(cmp);    
        PdfPCell pdate = cell("Ultima incasare: 06.03.2011", 0);

        header.addCell(pdate);
        header.addCell(cell("Zona: ADAAA", 0));
        header.addCell(cell("Operator: SOME ONE SPECIAL", 0));
        header.addCell(cell("Procent locatie: 45%", 0));
        header.addCell(cell("Metoda de plata: cash", 0));

        doc.add(header);

        Image img = Image.getInstance(Config.PATH +"tabelka.png");
        img.scalePercent(25, 25);


        // ON TOP
        PdfContentByte cb = writer.getDirectContent();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, "Cp1250", false);
        cb.setFontAndSize(bf, FONTSIZE);
        cb.beginText();     

        float x = doc.getPageSize().getHeight() - 4;

        float start = x;
        int j = 0;

        for(int i=0; i< 5; i++)
        {           
            j++;
            if(i > 0 && i % 5 == 0)
            {
                cb.endText();
                doc.newPage();

                cb = writer.getDirectContent();
                cb.setFontAndSize(bf, FONTSIZE);
                cb.beginText();

                start = x;
                j = 1;
            }
            doc.add(img);


            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "0305/HS", 75, start- 53, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "VUKA-HS-025", 75, start - 67.5f, 0);

            // ELEC
            // IN
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000001", 293, start - 67.5f, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000003", 365, start - 67.5f, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000002", 440, start - 67.5f, 0);

            // OUT
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000001", 293, start - 82, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000003", 365, start - 82, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000002", 440, start - 82, 0);

            // SUM
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "0.50", 465, start - 73.5f, 0);     
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "999.0", 565, start - 73.5f, 0);

            // MECH
            // IN
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000001", 293, start - 110, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000003", 365, start - 110, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000002", 440, start - 110, 0);

            // OUT
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000001", 293, start - 124, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000003", 365, start - 124, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "000002", 440, start - 124, 0);

            // SUM
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "0.50", 465, start - 115.5f, 0);        
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "999.0", 565, start - 115.5f, 0);

            /// COINS AND NOTES
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "0.5", 156, start - 82, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "1", 156, start - 95.5f, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "5", 156, start - 110, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "10", 156, start - 124, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "50", 156, start - 138, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "100", 156, start - 152, 0);

            // COMMENTS
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "comments...", 205, start - 138.5f, 0);

            start = (x - (img.getHeight() * 0.25f) * j);
        }
        //doc.add(content);
        cb.endText();
        // END

        PdfPTable footer = new PdfPTable(5);
        footer.setHorizontalAlignment(Element.ALIGN_CENTER);
        footer.setWidthPercentage(100);

        footer.addCell(cell("Total sum: 3397.50", 0));
        footer.addCell(cell("Total pierderi: 0.00", 0));
        footer.addCell(cell("Total taxa: 0.00", 0));
        footer.addCell(cell("Baza de calcul: 3397.50", 0));
        footer.addCell(cell("Locatie: 1528.88", 0));

        PdfPCell b = cell("Num. de ordine al primului bilet: 0", 0);
            b.setColspan(2);
        footer.addCell(b);
        footer.addCell(cell("Num. de tichete: 0", 0));
        footer.addCell(cell("Corectie tichete: 0", 0));
        footer.addCell(cell("Total bilete: 0.00", 0));

        PdfPCell b1 = cell("Num de ordine al ultimului bilet: 0", 0);
            b1.setColspan(3);
        footer.addCell(b1);

        PdfPCell t = cell("Bani ce trebuiesc viratii: 18.441", 0);
            t.setColspan(2);
        footer.addCell(t);

        PdfPCell s = cell("Suma de bani platita: de clienti", 0);
            s.setColspan(5);
        footer.addCell(s);

        PdfPCell o = cell("Obs: ", 0);
            o.setColspan(5);
        footer.addCell(o);
        doc.add(footer);

        PdfContentByte cb1 = writer.getDirectContent();
        BaseFont bf1 = BaseFont.createFont(BaseFont.HELVETICA, "Cp1250", false);
        cb1.setFontAndSize(bf1, FONTSIZE);
        cb1.beginText();    

        cb1.showTextAligned(PdfContentByte.ALIGN_RIGHT, "Semnatura Reprezentant Firma", 130, start - 210, 0);
        cb1.showTextAligned(PdfContentByte.ALIGN_CENTER, "Semnatura & Stampila Reprezentant", 500, start - 210, 0);
        cb1.showTextAligned(PdfContentByte.ALIGN_CENTER, "Pemct de lucru", 500, start - 220, 0);
        cb1.endText();
        // CLOSE
        doc.close();
    }

    public PdfPCell cell(String txt, int border)
    {
        Font font = new Font(Font.FontFamily.HELVETICA, FONTSIZE);
        Paragraph p = new Paragraph(txt, font);
            p.setSpacingAfter(0);
        PdfPCell cell = new PdfPCell(p);
        cell.setBorder(border);
        return cell;
    }

}

我已将我的项目导出到jar,然后将其编译为windows上的exe文件,pdf也无法正常工作。类创建文件pdf,0kb。 什么可能是错的?

0 个答案:

没有答案