iTextSharp table.SpacingBefore not working when the table is the first item
Asked Answered
C

1

5

I am trying to put some space before a table in my PDF file using table.SpacingBefore, but it doesn't work.

I have found iTextSharp table.SpacingBefore not working which is exactly the same problem but the solution doesn't seem to be working.

Here's a bit of my code:

header.SpacingBefore = 150f;
Paragraph paragraph = new Paragraph();
paragraph.Leading = 0f;
doc1.Add(paragraph);
doc1.Add(header);

The result is:

result1

Adding a non-empty paragraph

Paragraph paragraph = new Paragraph(" TEXT ");

produces this:

enter image description here

Collection answered 19/8, 2013 at 14:13 Comment(0)
M
10

Please take a look at the following screen shot:

enter image description here

The file spacing1.pdf (shown underneath the other ones) is created using the way you describe. By design the "spacing before" is ignored, because the "spacing" functionality allows you to create some space between different elements on the page. If there is no other element, no spacing is added. This is the case in your example: the table is the first element, so there's no need to add "spacing before". For the same reason "spacing after" is ignored when the table is the final element on the page.

The file spacing2.pdf (shown in the middle) is created by adding a Paragraph object before adding the table. As you can see, extra spacing is added between the Paragraph and the PdfPTable. You say there isn't. In my case, the screen shot makes it very clear there is. Which version of iTextSharp are you using?

I think your analysis of your requirement is wrong. You don't want spacing before or after. You want spacing. That's what I did in spacing3.pdf (shown on the right, on top of the other windows).

I introduced this spacing by adding the following Paragraph before adding the table:

document.Add(new Paragraph(100, "\u00a0"));

The 100 in this Paragraph is the leading. It defines the amount of space that is added. The "\u00a0" is a string with a single "non-breaking space" character.

I think that is what you're asking for. That's something different than "spacing before".

Macaco answered 19/8, 2013 at 14:49 Comment(2)
This creates a huge gap for me, even if I use 1 instead of 100Brackett
Nevermind - I'd left in my previously redundant (SpacingBefore) on the table without realising (it had obviously then become not redundant once I added a paragraph before it.) Also, I just used document.Add(new Paragraph(" ")) for a simple gap, the space character was enough (your \u00a0 rendered as-is, and of course I found out that empty string is no good either as this just hides the paragraph.)Brackett

© 2022 - 2024 — McMap. All rights reserved.