iTextSharp draw line under text (heading) at a random location on a page - itext

does anyone know how I can draw a line under a heading (a short line of bold text) that may be located in a random location on a page.
e.g.
My Heading 1
----------------------------------------------
Some random paragraph
My Heading 2
----------------------------------------------
I can do it as I have done above using a line of Underscores _ but in order to get the line anywhere near the heading I have to set the font size at 2 which results in a spotty ugly line.
I can also add two chunks over the top of each other (one with the heading and one with _) similar to the first example in this article http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs using the same font size but it seems that this only works at the top of the page, once I add other paragraphs and try to do it mid-way down the page the two chunks separate with a clear line-break at the end of the first chunk.
It seems you can draw lines in iTextSharp but I have no idea how to calculate the coordinates, as all the examples I have seen use this method to place a line at the top or bottom of the page in a fixed location.
Any help greatly appreciated.
Cheers
Rob

The LineSeperator object might be what you're looking for. Wrap it with a chunk and put it where you need it.
Here's a sample line seperator:
Chunk linebreak = new Chunk(new LineSeparator(4f, 100f, colorGrey, Element.ALIGN_CENTER, -1));
doc.Add(linebreak);

I know this is an older post but, maybe this will help somebody.
//Create Chunk for underline
Chunk chkHeader = new Chunk("My Title", fnt13Bold);
chkHeader.SetUnderline(1f, -2f);
//Add Chunk to paragraph
Paragraph pHeader = new Paragraph(chkHeader);

Related

Right-side text limit working erratically while changing the font

Please note that I have already posted this question # mathworks.com but did not get any answer.
Let me try to explain a slight problem that I am facing.
In this figure, you can see that I have font “Monospaced” with font size 14. The right-hand text limit is place at 75th column. As you can see from the code, the line terminates at 75th position and then goes to the next line.
Now, if I change the font to say, “Comic Sans”, keeping the same font, the ‘right-hand text’ limiting line moves to a new position. But strangely, the text terminates not at the position where the line is shown, but in the same place where initially the ‘right-hand text limit’ was there.
I checked it for other fonts also, but still the problem persists. Any idea of what is happening?
Using Matlab R2021b.
Thanks in advance.

Way of formatting Word so that a line occur at a given distance from page border?

I don't know how to properly specify this question, but basically I would like to format a document like specified here : http://etd.lib.hku.hk/thesis-form/Theses%20Binding%20specification(ed%20January%202018)c.pdf
It's on the 3rd page of the PDF document. So I need to input a line at the exact distance from the top border, while another line occurs at some exact distance from the bottom.
Does MS Word give the flexibility to do this?
Thanks!
Someone points out this question is off-topic for this forum but just in case someone is here anyway: use multiple text boxes can be the solution.
A user can adjust a text box's relative position on a page and thus achieve the formatting needed.

Text component displaying lines in reverse order

Alright, I do not know how to fix this and only ran into this problem after trying to put in some longer text on a UI Text component. I have tried both pasting a value into its Text attribute through the Unity editor, and setting its value programmatically like this:
t.GetComponent<Text> ().text = "This is where meat, fish, and [...] (long text)"
Because Horizontal wrap is on, the text wraps when it reaches the edge of the available space.
However, the text displays backwards. Meaning, the start of the paragraph is at the bottom, and bottom at the top. Taking off wrap fixes this, but then the font size has to be really small (or it won't all be visible), and it can't form a normal paragraph because it has to... you know... wrap.
Is this a bug in Unity?
This is what happens - as you can see, it is displayed backwards:
The negative Line Spacing value is what is causing the issue here:
When the value of this field is less than 0, the lines will actually be arranged backwards, leading to the behaviour you're currently encountering. Just change the value to a positive number to have the text display correctly.

How to easily control spacing height between two paragraphs?

Currently, I am using
document.add( Chunk.NEWLINE );
after each paragraph to generate space between two paragraphs. What is the way to generate a spacing of any height I specify?
The space between two lines of the same Paragraph is called the leading. See Changing text line spacing
If you want to introduce extra spacing before or after a Paragraph, you can use the setSpacingBefore() or setSpacingAfter() method. See itext spacingBefore property applied to Paragraph causes new page
For instance:
Paragraph paragraph1 = new Paragraph("First paragraph");
paragraph1.setSpacingAfter(72f);
document.add(paragraph1);
Paragraph paragraph2 = new Paragraph("Second paragraph");
document.add(paragraph2);
This puts 72 user units of extra white space between paragraph1 and paragraph2. One user unit corresponds with one point, so by choosing 72, we've added an inch of white space.

Can I tell iText how to clip text to fit in a cell

When I call setFixedHeight() on a PdfPCell, and add more text than fits in the given height, iText seems to print the prefix of the string which fits.
Can I control this clipping algorithm? For example:
Print a suffix of the string rather than a prefix.
Mark a substring of the string as not to be removed. This is with footnote references. If I add text saying "Hello World [1]", the [1] is a reference to a footnote and should not be removed. It's okay to remove the other characters of the string, like "World".
When there are multiple words in the string, iText seems to eliminate a word that doesn't fit, while I would like it partially printed. That is, if the string is "Hello World", and the cell has room only for "Hello Wo...", I would like that to be printed, rather than just "Hello", as iText prints.
Rather than printing characters in their entirety, print only part of them. Imagine printing the text to a PNG and chopping off the top and/or bottom part of the PNG to fit it in the space available. For example, notice that the top line and the bottom line are partially clipped here:
Are any of these possible? Does iText give me any control over how text is clipped? Thanks.
This is with reference to iText 2.1.6.
I have written a proof of concept, ClipCenterCellContent, where we try to fit the text "D2 is a cell with more content than we can fit into the cell." in a cell that is too small.
Just like in your other question ( iText -- How do I get the rendered dimensions of text? ), we add the content using a cell event, but we now add it twice: once in simulation mode (to find out how much space is needed vertically) and once for real (using an offset).
This adds the content in simulation mode (we use the width of the cell and an arbitrary height):
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(new Rectangle(0, 0, position.getWidth(), -1000));
ct.addElement(content);
ct.go(true);
float spaceneeded = 0 - ct.getYLine();
System.out.println(String.format("The content requires %s pt whereas the height is %s pt.", spaceneeded, position.getHeight()));
We now know the needed height and we can add the content for real using an offset:
float offset = (position.getHeight() - spaceneeded) / 2;
System.out.println(String.format("The difference is %s pt; we'll need an offset of %s pt.", -2f * offset, offset));
PdfTemplate tmp = canvas.createTemplate(position.getWidth(), position.getHeight());
ct = new ColumnText(tmp);
ct.setSimpleColumn(0, offset, position.getWidth(), offset + spaceneeded);
ct.addElement(content);
ct.go();
canvas.addTemplate(tmp, position.getLeft(), position.getBottom());
In this case, I used a PdfTemplate to clip the content.
I also have answers to your other questions, but I don't have the time to answer them right now.
For straight Text box clipping, I adapted the C# code given here
http://itextsharp.10939.n7.nabble.com/Limiting-Text-Width-using-PdfContentByte-td2481.html
to the Java code below. The clipping area ends up outside this rectangle, so you can still draw a rectangle on the same exact coordinates.
cb.saveState();
cb.rectangle(left,top,width,height);
cb.clip();
cb.newPath();
// perform clipped output here
cb.restoreState();
I used a try/finally to ensure restoreState() was called.