如何使用iText Java垂直显示段落中的内容

时间:2018-03-21 12:33:13

标签: java pdf itext itext7

如何垂直显示段落。我已经开发了这个代码,我可以垂直地看到该段落但不完全。它显示出交叉。你可以参考上面的图片。

private  void stampPdf(InputStream source, OutputStream dest) throws Exception {
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(source), new PdfWriter(dest));
    Document doc = new Document(pdfDoc);
    Paragraph header = new Paragraph("Received by ICA on " + getDate())
            .setFont(PdfFontFactory.createFont(FontConstants.HELVETICA))
            .setFontSize(8);

    for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {
        float x = pdfDoc.getPage(i).getPageSize().getLeft();
        float y = pdfDoc.getPage(i).getPageSize().getTop();
        doc.showTextAligned(header.setFontColor(Color.RED), x, y , i,
                TextAlignment.RIGHT, VerticalAlignment.TOP, 90);   
    }
    doc.close();
}

1 个答案:

答案 0 :(得分:0)

您在此代码行中显示文字:

doc.showTextAligned(header.setFontColor(Color.RED), x, y , i,
        TextAlignment.RIGHT, VerticalAlignment.TOP, 90);

您尝试使用90(度)作为角度参数垂直制作方向。

但是这个showTextAligned重载记录为:

/**
 * Convenience method to write a text aligned about the specified point
 *
 * @param p          paragraph of text to be placed to the page. By default it has no leading and is written in single line.
 *                   Set width to write multiline text.
 * @param x          the point about which the text will be aligned and rotated
 * @param y          the point about which the text will be aligned and rotated
 * @param pageNumber the page number to write the text
 * @param textAlign  horizontal alignment about the specified point
 * @param vertAlign  vertical alignment about the specified point
 * @param radAngle   the angle of rotation applied to the text, in radians
 * @return this object
 */
public T showTextAligned(Paragraph p, float x, float y, int pageNumber, TextAlignment textAlign, VerticalAlignment vertAlign, float radAngle)

即。角度参数预计以弧度给出!您应使用90(float) Math.PI / 2f(取决于您所追求的垂直书写的变体),而不是值(float) -Math.PI / 2f,例如:

doc.showTextAligned(header.setFontColor(Color.RED), x, y , i,
        TextAlignment.RIGHT, VerticalAlignment.TOP, (float) Math.PI / 2f);

根据您所追求的垂直书写方式,您可能还需要增加x以使书写不仅仅在页面区域外。