Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

itextsharp - How to Generate Table-of-Figures Dot Leaders in a PdfPCell for the Last Line of Wrapped Text

I have a Table of Figures generated using PDFPTable like so.

-------------------------------------- 
|Caption|Figure Title           |  PP|
-------------------------------------- 
| fig 1 |title text             |  2 |
--------------------------------|----|
| fig 2 | This is a longer title|    |
|       | text that wraps       | 55 |
--------------------------------------
| fig N | another title         | 89 |
--------------------------------------

I need to generate leader dots from the last line of text; it may have wrapped in the cell.

Here's a sample again showing the Figure 2 item of wrapped titled text and additionally having leader dots from the end of the text to the right side of the cell.

| fig 2 | This is a longer title|    |
|       | text that wraps.......| 55 |

This is what I want to achieve.

When I write the cell text I don't know where it will wrap and therefore I don't know what the last line of text will be for the purpose of measuring it, and if I do know about the last line of text I'm hard pressed to find the immediate x position following it.

So far, I've tried harnessing Cell Events and Table Events to write the leader dots after the fact. The two problems that have stopped me so far are:

  1. I can't find the x position directly after the last line of wrapped text.

  2. If I try the strategy of underlying dots beneath the entire last line of text (so I don't have to know where it stops in the x position. I do now the y position) the only canvas that allows the dots to appear is PdfPTable.TEXTCANVAS however it writes the leader dots on top of the existing text instead of beneath. I can give the text chunk a background color, so if some leader dots exist behind it they will not be visible (a poor man's crop).

I'm open to any strategy inside the context of the PDFPTable.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First things first: +1 for your question and the answer you provided yourself.

But...

Although your answer may work, there is a better way to achieve your goal. I've written a small sample called DottedLineLeader as an alternative that will be much easier to maintain. This example uses the DottedLineSeparator wrapped in a Chunk object.

Take a look at how such a chunk separator is used:

Chunk leader = new Chunk(new DottedLineSeparator());
Paragraph p;
p = new Paragraph("This is a longer title text that wraps");
p.add(leader);

When we add this Paragraph to a PdfPCell, we get the following effect:

enter image description here

The dots in the dotted line are not '.' characters. Instead it's an actual line (vector data) with a specific dash pattern. Using this approach instead of your own, will significantly reduce the number of lines in your code, use less CPU and result in cleaner PDFs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...