简体   繁体   English

为什么我不能用iText垂直打印这个String?

[英]Why I can't vertically print this String with iText?

I have no big experience using iText and I have the following problem. 我没有使用iText的丰富经验,我有以下问题。

I have to put vertically a Phrase (a simple String, I think that also a Chunk is ok in my case) into a page. 我必须在页面中垂直放置一个短语 (一个简单的字符串,我认为在我的情况下也可以使用Chunk )。

So I have followed this tutorial finded on the official iText documentation: http://itextpdf.com/examples/iia.php?id=202 所以我在官方的iText文档中找到了这个教程: http//itextpdf.com/examples/iia.php?id = 202

This is my code: 这是我的代码:

private static void printPdf() {

    /** The resulting PDF file: */
    String result = "D:/MYCOMPANY/massive_pdf_print.pdf";

    // STEP 1 Creazione del documento in formato A4 e senza margini:
    com.itextpdf.text.Document document = new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4, 0, 0, 0, 0);

    try {
        /* STEP 2 Constructs a PdfWriter.
                  document: The PdfDocument that has to be written.
                  os: The OutputStream the writer has to write to
         */
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(result));

        // STEP 3:
        document.open();

        // STEP 4:
        //document.add(new Paragraph("Hello World!"));

        VerticalText vt = new VerticalText(writer.getDirectContent());

        vt.addText(new Phrase("Hello World !!!"));
        vt.go();

        // STEP 5:
        document.close();

    }catch (DocumentException ex){
        ex.printStackTrace();
    }
    catch (IOException ex){
        ex.printStackTrace();
    }

}

Ok the problem is that when try to perform this line: 好的问题是,当尝试执行此行时:

document.close();

The following exception is thrown: 抛出以下异常:

Exception in thread "main" ExceptionConverter: java.io.IOException: The document has no pages.
    at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:113)
    at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1217)
    at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:777)
    at com.itextpdf.text.Document.close(Document.java:398)
    at mainPkg.Main.printPdf(Main.java:123)
    at mainPkg.Main.main(Main.java:78)

Process finished with exit code 1

Why? 为什么? What am I missing? 我错过了什么? How can I solve this issue and print vertically my "Hello World !!!" 如何解决这个问题并垂直打印我的“Hello World !!!” String? 串?

EDIT 1: 编辑1:

This is how I see the generated PDF after modify the code inserting the: 这是我在修改插入的代码后看到生成的PDF的方法:

vt.setVerticalLayout(390, 570, 540, 12, 30);

在此输入图像描述

As you can see the text is not vertically aligned but seems to be horizontally with margin. 正如您所看到的那样,文本没有垂直对齐,但似乎是水平的边距。 Why? 为什么? What am I missing? 我错过了什么?

Tnx TNX

You aren't defining any dimensions. 您没有定义任何尺寸。

In the example from my book you refer to, there's this line: 在我所提到的书的例子中,有这一行:

vt.setVerticalLayout(390, 570, 540, 12, 30);

These coordinates define where the vertical columns go, see the setVerticalLayout() method: 这些坐标定义垂直列的位置,请参阅setVerticalLayout()方法:

  • startX - the top right X line position startX - 右上角的X行位置
  • startY - the top right Y line position startY - 右上方的Y行位置
  • height - the height of the lines height - 线条的高度
  • maxLines - the maximum number of lines maxLines - 最大行数
  • leading - the separation between the lines 领先 - 线条之间的分离

As you didn't define these values, iText doesn't know where to add the text, hence no text is added and "the document has no pages." 由于您没有定义这些值,因此iText不知道在何处添加文本,因此不会添加任何文本并且“文档没有页面”。

Update: 更新:

Although the original question was adequately answered, it was not accepted. 尽管最初的问题得到了充分的回答,但并未被接受。 Instead the question was changed. 而是改变了问题。 This is not correct behavior on StackOverflow: a new question should have been posted. 这在StackOverflow上不是正确的行为:应该发布一个新问题。

Moreover both questions, the original one and the adapted one, prove a lack of respect for the documentation. 此外,这两个问题,原始问题和改编版问题都证明了对文件的不尊重。 An example is taken from my book, then that example is mutilated, then I am asked: why doesn't this work. 我的书中有一个例子,然后那个例子被肢解,然后我被问到:为什么这不起作用。

The first mutilation consisted of removing an essential line. 第一次切割包括删除基本线。 The second mutilation reveals that the documentation that was written to support the example was ignored. 第二次切割表明,为支持该示例而编写的文档被忽略了。

When writing vertical text, you need to use a specific encoding: Identity-V . 在编写垂直文本时,您需要使用特定的编码: Identity-V As explained in the book Identity-H is for horizontal writing systems, whereas Identity-V is for vertical writing systems. 正如书中所解释的, Identity-H用于水平书写系统,而Identity-V用于垂直书写系统。 You are using an encoding for horizontal writing, expecting it to write text vertically... 您正在使用水平书写编码,期望它垂直写入文本...

How to fix this? 如何解决这个问题? By using Identity-V as shown in the VTExample : 通过使用VTE示例中显示的Identity-V

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    BaseFont bf = BaseFont.createFont(
        FONT, BaseFont.IDENTITY_V, BaseFont.NOT_EMBEDDED);
    Font font = new Font(bf, 20);
    VerticalText vt = new VerticalText(writer.getDirectContent());
    vt.setVerticalLayout(559, 806, 770, 29, 18);
    vt.addText(new Phrase("Hello World !!!", font));
    vt.go();
    document.close();
}

The important parameter is BaseFont.IDENTITY_V . 重要的参数是BaseFont.IDENTITY_V Note that this parameter can't be used in combination with every font. 请注意,此参数不能与每种字体结合使用。 For instance: it won't work with Helvetica. 例如:它不适用于Helvetica。 In my example, I used FreeSans: 在我的例子中,我使用了FreeSans:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM