简体   繁体   中英

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

I have no big experience using iText and I have the following problem.

I have to put vertically a Phrase (a simple String, I think that also a Chunk is ok in my case) into a page.

So I have followed this tutorial finded on the official iText documentation: 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 !!!" String?

EDIT 1:

This is how I see the generated PDF after modify the code inserting the:

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

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:

  • startX - the top right X line position
  • startY - the top right Y line position
  • height - the height of the lines
  • maxLines - the maximum number of lines
  • 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."

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.

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 . As explained in the book Identity-H is for horizontal writing systems, whereas Identity-V is for vertical writing systems. 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 :

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 . Note that this parameter can't be used in combination with every font. For instance: it won't work with Helvetica. In my example, I used FreeSans:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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