有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

pdf如何使用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();
}

共 (1) 个答案

  1. # 1 楼答案

    显示此代码行中的文本:

    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)
    

    即,角度参数应以弧度为单位!您应该使用(float) Math.PI / 2f(float) -Math.PI / 2f来代替值90(取决于您所追求的垂直书写的变体),例如:

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

    根据您所追求的垂直书写的变体,您可能还希望增加x一点,使书写不只是在页面区域之外