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

How to display the content in a paragraph vertically using iText Java

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

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

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

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 一点以使书写不只是在页面区域之外。