0

I want to wrap text in a rect which is below (or left or right) of a image as below :

Please see link : http://upanh.in/SLk/

I use ColumnText for wrapping text in my code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/pdf");
    try {
        // step 1
        Document document = new Document(PageSize.A4.rotate());
        // step 2
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        // step 3
        document.open();
        // step 4
        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(new Phrase("text is very long ..."), 10, 10, 20, 20, 18, Element.ALIGN_CENTER);
        column.go();

        // step 5
        document.close();
        ServletOutputStream os = response.getOutputStream();
        baos.writeTo(os);
        os.flush();
        os.close();
    } catch (DocumentException e) {
        throw new IOException(e.getMessage());
    }
}

Result:

ExceptionConverter: java.io.IOException: The document has no pages.

Do you have any suggestions how to fix that?

Question 2

I try to display text (center and middle) in the rect with below code but it wasn't success. The text was only center in the rect.

ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(RectImg1[0], RectImg1[1], RectImg1[0] + squareHeight, RectImg1[1] + squareHeight
                        * 1 / 4);
Paragraph p = new Paragraph(imgr.getText(), fontH);
p.setAlignment(Element.ALIGN_CENTER | Element.ALIGN_MIDDLE);
p.setLeading(18);
column.addElement(p);
column.go();

where was my mistake ?

Han Kun
  • 73
  • 12

1 Answers1

1

I have edited the title of your question because it was misleading: the exception you encounter will occur in a standalone application too. The fact that you are using the code in a Servlet is irrelevant.

I see that you have the following line:

column.go();

You did not use something like this:

int status = column.go();

If you did, and if you examined status, you would have noticed that the column object still contained some text.

What text? All the text.

There is a serious error in this line:

column.setSimpleColumn(new Phrase("text is very long ..."), 10, 10, 20, 20, 18, Element.ALIGN_CENTER);

You are trying to add the text "text is very long ..." into a rectangle with the following coordinates:

float llx = 10;
float lly = 10;
float urx = 20;
float ury = 20;

You didn't define a font, so the font is Helvetica with font size 12pt and you defined a leading of 18pt.

This means that you are trying to fit text that is 12pt high with an extra 6pt for the leading into a square that measures 10 by 10 pt. Surely you understand that this can't work!

As a result, nothing is added to the PDF and rather than showing an empty page, iText throws an exception saying: there are no pages! You didn't add any content to the document!

You can fix this, for instance by changing the incorrect line into something like this:

column.setSimpleColumn(new Phrase("text is very long ..."), 36, 36, 559, 806, 18, Element.ALIGN_CENTER);

An alternative would be:

column.setSimpleColumn(rect);
column.addElement(paragraph);

In these two lines rect is a Rectangle object. The leading and the alignment are to be defined at the level of the Paragraph object (in this case, you don't use a Phrase).

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • It's very useful. Thanks Bruno Lowagie!! However, i want to wrap text in a rect then how to do this ? – Han Kun Sep 23 '14 at 10:56
  • Define the rect using the coordinates `llx`, `lly`, `urx` and `ury`. `ll` stands for the lower-left corner. `ur` stands for upper-right corner. These two corners define your rectangle. I've also update my question with an alternative approach. – Bruno Lowagie Sep 23 '14 at 11:16
  • I also searched on google.com and found this topic in link : "http://itextpdf.com/sandbox/objects/CenterColumnVertically". But text isn't center in rect as sample image : " http://upanh.in/MNk " . Could you suggest anything for me? – Han Kun Sep 24 '14 at 02:49
  • Yes, you had to combine the example that centers the text vertically with your code that centers the text horizontally. I guess that the difference between centering horizontally and vertically confused us ;-) – Bruno Lowagie Sep 24 '14 at 06:35
  • :( if i use VerticalText as below : VerticalText vt = new VerticalText(writer.getDirectContent()); vt.setVerticalLayout(marginLeft + squareHeight, 1191.0f - marginTop, squareHeight, 3, 20); vt.setAlignment(Element.ALIGN_CENTER); Paragraph p = new Paragraph(imgr.getText(), fontV); p.setLeading(10); vt.addText(p); vt.go(); The text is align_right. How to display text center in a verticalText ? – Han Kun Sep 24 '14 at 11:36
  • New questions posted in the comments of existing, answered questions are often overlooked and hard to answer due to the limitations of the comment functionality. Hence I will not answer the question here. Post a new question if you want an answer, but be quick: soon I'll be on a plane to Java One in San Francisco. – Bruno Lowagie Sep 24 '14 at 11:55
  • sorry @Burno Lowagie, I posted a new question. Could you give any idea to me ? Thanks so much !! – Han Kun Sep 24 '14 at 12:08