2

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

做广告
_

C

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

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

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

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

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

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

这样 D 和 E 将右对齐。

但我没有找到给予的方法

--> 空格 = 上面用 bf 计算的宽度

这样在 pdf 上打印时,它看起来像一张桌子。

以下是示例代码:-

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

}

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

谢谢

4

0 回答 0