如何以表格的形式创建String缓冲区

时间:2013-07-18 14:26:42

标签: java string pdf itext

我正在处理报告应用程序,其中联系人信息需要显示在页眉上,这样如果信息更多,它会出现在下一列右对齐的表格中。 例如: - 假设联系信息包含5行(A,B,C,D,E),页眉上只允许3行,ti将如下所示: -

A D
B E

C

观察D和E出现在下一行。但是现在A,B,C是随机长度的,我的String缓冲区需要以表格或行和列的形式制作。制作cols

1。)我将联系信息更改为2D数组。 2.)取cols并从该列中找到最大宽度的内容 3.)为每个列行添加空格,以便下一列看起来是右对齐的。

对于第二步,我使用BaseFont获取了特定字符串的宽度         double w = bf.getWidthPoint(s,size);

找到最大宽度后,这个宽度被视为最大宽度,然后我减去

添加空间=最大宽度 - (A)到A

的宽度

同样,添加空间=最大宽度 - (B)到B

的宽度

这样D和E就会正确对齐。

但我没有找到方法给予

- >空格=高于宽度,用bf

计算

这样在pdfs上打印时它就像一张桌子。

以下是示例代码: -

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

public class PrintSpacePdf {
    private static String arrNames[] = { "US Rates Strategy Cash",
            "Pavan Wadhwa(1-212) 844-4597", "Srini Ramaswamy(1-212) 844-4983",
            "Meera Chandan(1-212) 855-4555", "Kimberly Harano(1-212) 823-4996",
            "Feng Deng(1-212) 855-2555", "US Rates Strategy Derivatives",
            "Srini Ramaswamy(1-212) 811-4999",
            "Alberto Iglesias(1-212) 898-5442",
            "Praveen Korapaty(1-212) 812-3444", "Feng Deng(1-212) 812-2456",
            "US Rates Strategy Derivatives", "Srini Ramaswamy(1-212) 822-4999",
            "Alberto Iglesias(1-212) 822-5098",
            "Praveen Korapaty(1-212) 812-3655", "Feng Deng(1-212) 899-2222" };
    private static int SIZE = arrNames.length;
    private static int maxRows = 0;
    private static int maxCols = 0;

    public static void main(String[] args) {
        writeStringToPDF(generateBuffer(), 500, 400, "C://barchart.pdf");
        // writeChartToPDF(generatePieChart(), 500, 400, "C://piechart.pdf");
    }

    public static String generateBuffer() {
        maxRows = 5;
        maxCols = (SIZE % maxRows == 0) ? SIZE / maxRows : (SIZE / maxRows) + 1;
        int size = 5;// fontSize
        BaseFont bf;
        String buffer = null;
        try {
            bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252,
                    BaseFont.NOT_EMBEDDED);

            System.out.println("rows:" + maxRows + "cols:" + maxCols);

            String arrData[][] = initializeTableArrays(arrNames);

            // showArrayData(arrData);

            arrData = addSpacesToArrayContents(arrData, size, bf);

            buffer = createBufferWithTableArray(arrData);
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return buffer;
    }

    public static String createBufferWithTableArray(String[][] arrData) {
        String buffer = "";
        for (int r = 0; r < maxRows; r++) {
            for (int c = 0; c < maxCols; c++) {
                if (arrData[r][c] != null) {
                    buffer += arrData[r][c];
                }
            }
            buffer += "\n";
        }
        return buffer;
    }

    public static String[][] initializeTableArrays(String arrNames[]) {
        String arrData[][] = new String[maxRows][maxCols];
        int col = 0;
        for (int i = 0; i < arrNames.length; i++) {
            if (arrNames[i] != null) {
                col = 0;
                for (int j = i; j < arrNames.length; j += maxRows) {
                    // System.out.println("i:" + i + "col" + col + "j:" + j);
                    if (arrNames[j] != null) {
                        arrData[i][col] = arrNames[j].trim();
                        arrNames[j] = null;
                    }
                    col++;
                }
            }
        }
        return arrData;
    }

    public static String[][] addSpacesToArrayContents(String arrData[][],
            int size, BaseFont bf) {
        for (int cols = 0; cols < maxCols; cols++) {
            double maxWidth = getMaximumColWidth(arrData, cols, size, bf);

            for (int rows = 0; rows < maxRows; rows++) {
                String s = arrData[rows][cols];
                if (s != null) {
                    double w = bf.getWidthPoint(s, size);
                    double noOfSpaces = maxWidth - w;
                    arrData[rows][cols] = addSpaces(s, noOfSpaces);
                }
            }
        }
        return arrData;
    }

    public static double getMaximumColWidth(String[][] arrData, int column,
            int size, BaseFont bf) {
        double maxSize = 0;
        for (int cols = 0; cols < maxCols; cols++) {
            for (int rows = 0; rows < maxRows; rows++) {
                String s = arrData[rows][cols];
                if (s != null) {
                    double w = bf.getWidthPoint(s, size);
                    maxSize = (maxSize < w) ? w : maxSize;
                }

            }
        }
        return (maxSize);
    }

    public static String addSpaces(String s, double noOfSpaces) {
        for (int i = 0; i < noOfSpaces; i++) {
            s = s.concat(" ");
        }
        return s;
    }

    public static void showArrayData(String arrData[][]) {
        for (int i = 0; i < arrData.length; i++) {
            String inner[] = arrData[i];
            for (int j = 0; j < inner.length; j++) {
                System.out.println("i:" + i + "j:" + j + "->" + inner[j]);
            }
            System.out.println();

        }
    }

    public static void writeStringToPDF(String buffer, int width, int height,
            String fileName) {
        PdfWriter writer = null;

        Document document = new Document();

        try {
            writer = PdfWriter.getInstance(document, new FileOutputStream(
                    fileName));
            document.open();
            PdfContentByte contentByte = writer.getDirectContent();
            System.out.println(buffer);
            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            contentByte.beginText();
            contentByte.setFontAndSize(bf, 5);
            contentByte.showText(buffer);
            contentByte.endText();

        } catch (Exception e) {
            e.printStackTrace();
        }
        document.close();
    }

}

import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfWriter; public class PrintSpacePdf { private static String arrNames[] = { "US Rates Strategy Cash", "Pavan Wadhwa(1-212) 844-4597", "Srini Ramaswamy(1-212) 844-4983", "Meera Chandan(1-212) 855-4555", "Kimberly Harano(1-212) 823-4996", "Feng Deng(1-212) 855-2555", "US Rates Strategy Derivatives", "Srini Ramaswamy(1-212) 811-4999", "Alberto Iglesias(1-212) 898-5442", "Praveen Korapaty(1-212) 812-3444", "Feng Deng(1-212) 812-2456", "US Rates Strategy Derivatives", "Srini Ramaswamy(1-212) 822-4999", "Alberto Iglesias(1-212) 822-5098", "Praveen Korapaty(1-212) 812-3655", "Feng Deng(1-212) 899-2222" }; private static int SIZE = arrNames.length; private static int maxRows = 0; private static int maxCols = 0; public static void main(String[] args) { writeStringToPDF(generateBuffer(), 500, 400, "C://barchart.pdf"); // writeChartToPDF(generatePieChart(), 500, 400, "C://piechart.pdf"); } public static String generateBuffer() { maxRows = 5; maxCols = (SIZE % maxRows == 0) ? SIZE / maxRows : (SIZE / maxRows) + 1; int size = 5;// fontSize BaseFont bf; String buffer = null; try { bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); System.out.println("rows:" + maxRows + "cols:" + maxCols); String arrData[][] = initializeTableArrays(arrNames); // showArrayData(arrData); arrData = addSpacesToArrayContents(arrData, size, bf); buffer = createBufferWithTableArray(arrData); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return buffer; } public static String createBufferWithTableArray(String[][] arrData) { String buffer = ""; for (int r = 0; r < maxRows; r++) { for (int c = 0; c < maxCols; c++) { if (arrData[r][c] != null) { buffer += arrData[r][c]; } } buffer += "\n"; } return buffer; } public static String[][] initializeTableArrays(String arrNames[]) { String arrData[][] = new String[maxRows][maxCols]; int col = 0; for (int i = 0; i < arrNames.length; i++) { if (arrNames[i] != null) { col = 0; for (int j = i; j < arrNames.length; j += maxRows) { // System.out.println("i:" + i + "col" + col + "j:" + j); if (arrNames[j] != null) { arrData[i][col] = arrNames[j].trim(); arrNames[j] = null; } col++; } } } return arrData; } public static String[][] addSpacesToArrayContents(String arrData[][], int size, BaseFont bf) { for (int cols = 0; cols < maxCols; cols++) { double maxWidth = getMaximumColWidth(arrData, cols, size, bf); for (int rows = 0; rows < maxRows; rows++) { String s = arrData[rows][cols]; if (s != null) { double w = bf.getWidthPoint(s, size); double noOfSpaces = maxWidth - w; arrData[rows][cols] = addSpaces(s, noOfSpaces); } } } return arrData; } public static double getMaximumColWidth(String[][] arrData, int column, int size, BaseFont bf) { double maxSize = 0; for (int cols = 0; cols < maxCols; cols++) { for (int rows = 0; rows < maxRows; rows++) { String s = arrData[rows][cols]; if (s != null) { double w = bf.getWidthPoint(s, size); maxSize = (maxSize < w) ? w : maxSize; } } } return (maxSize); } public static String addSpaces(String s, double noOfSpaces) { for (int i = 0; i < noOfSpaces; i++) { s = s.concat(" "); } return s; } public static void showArrayData(String arrData[][]) { for (int i = 0; i < arrData.length; i++) { String inner[] = arrData[i]; for (int j = 0; j < inner.length; j++) { System.out.println("i:" + i + "j:" + j + "->" + inner[j]); } System.out.println(); } } public static void writeStringToPDF(String buffer, int width, int height, String fileName) { PdfWriter writer = null; Document document = new Document(); try { writer = PdfWriter.getInstance(document, new FileOutputStream( fileName)); document.open(); PdfContentByte contentByte = writer.getDirectContent(); System.out.println(buffer); BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.CP1252, BaseFont.NOT_EMBEDDED); contentByte.beginText(); contentByte.setFontAndSize(bf, 5); contentByte.showText(buffer); contentByte.endText(); } catch (Exception e) { e.printStackTrace(); } document.close(); } }

请帮助我们在字符串缓冲区中显示联系人,该联系人相当于使用上述方法的表格。

由于

0 个答案:

没有答案