How to tweak the vertical position of text in a cell? - c#

I have a problem with the vertical alignment in a table. The text is too close to the bottom border:
My code looks like this:
nested = new PdfPTable(3);
nested.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
nested.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
nested.WidthPercentage = 100;
nested.AddCell(new Phrase("blablabla"));
nested.AddCell(new Phrase("blablabla"));
nested.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
nested.AddCell(new Phrase("Stand: " +
pdfdoc.Add(nested);
Adding or removing the line DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE; doesn't have any effect.

You are creating PdfCell object with only one line of text. The height of the cell will be determined based on that line of text. The text will be aligned in the middle automatically. That explains why adding or removing DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE; has no effect: as far as iText is concerned, the text is already aligned in the middle.
You claim that this isn't true because it's your perception that the base line of the text is too close to the bottom. I understand that claim, but if your read my answer to the question How does a PdfPCell's height relate to the font size? you should understand which factors create that perception:
The leading: the default font size is 12 pt; the default leading is 18 pt. A leading of 18 pt is kind of high and results in extra space above the base line. If you reduce the leading, you'll see that there's less space at the top.
Different fonts have different ascender and descender values; the way you add the cells, iText won't take those values into account.
My suggestion: tell iText to use the ascender and descender of the font you're using:
nested.DefaultCell.UseAscender = true;
nested.DefaultCell.UseDescender = true;
You'll notice that the position of the text will already be much better. If it's not better, you may want to add some padding. All of this is, of course, explained in the official documentation where you'll find an example called Spacing.cs. Try this example and you'll see how the position of the content changes by tweaking values such as UseAscender, UseDescender, Padding, and so on.

Related

Migradoc real table cell padding

Is there a way to add real padding to migradoc table cells? The options that they currently have (LeftPadding and RightPadding) actually work like margins and not like padding. And if you use it, it pushes the background color as well instead of pushing the content only.
This is what happens when you add "padding" to the whole row or individual cells (added it to the first row):
By default it looks like this:
If you wish to remove the white space between the columns, it seems like you have to do it by setting these padding properties to 0 and then you get this:
..which is almost what I want, but I want to push the content of the cell a bit to towards the centre of the cell from all 4 sides so that it looks like this and has a little room to "breathe":
It was even asked on their forums a long time ago about whether this could be done, but the answer doesn't solve the issue at all. It simply mentions the padding properties which work the exact opposite way of what the OP in that thread asked for (which is the same thing I want to do).
I'm using PDFsharp-MigraDoc-gdi v1.50.4000-beta3b nuget package.
Any ways to hack around this odd behaviour? Thanks.
EDIT: updated with a piece of code I use and with added bit recommended by PDFsharp Novice
var table = new Table();
var columnCount = 4;
for (int i = 0; i < columnCount; i++)
{
table.AddColumn();
}
var hedingRow= table.AddRow();
hedingRow.Format.Font.Bold = true;
hedingRow.Format.Font.Color = Color.Parse("0xFFFFFFFF");
hedingRow.Format.Shading.Color = Color.Parse("0xFF005aa0");
hedingRow.HeadingFormat = true;
hedingRow.Cells[0].AddParagraph("Field");
hedingRow.Cells[1].AddParagraph("Type");
hedingRow.Cells[2].AddParagraph("Default");
hedingRow.Cells[3].AddParagraph(String.Empty);
// Doesn't work as I would assume it should based on PDFsharp Novice
hedingRow.Cells[0].Format.LeftIndent = 4;
hedingRow.Cells[0].Format.RightIndent = 4;
hedingRow.Cells[0].Format.Shading.Color = Color.Parse("0xFF005aa0");
You can set the background color for the cell and/or for the text in the cell.
If you set the color for the cell, the padded area will also have the color.
If you set the color the text only, the padded area will have no color.

Draw a borderless table in iTextSharp

It appears as though the PDfPCell class does have a border property on it but not the PdfPTable class.
Is there some property on the PdfPTable class to set the borders of all its contained cells in one statement?
Borders are defined at the level of the cell, not at the level of the table. Hence: if you want to remove the borders of the table, you need to remove the borders of each cell.
By default, each cell has a border. You can change this default behavior by changing the border of each cell. For instance: if you create PdfPCell objects, you use:
cell.setBorder(Rectangle.NO_BORDER);
In case the cells are created internally, you need to change that property at the level of the default cell. See What is the PdfPTable.DefaultCell property used for?
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
For special borders, for instance borders with rounded corners or a single border for the whole table, or double borders, you can use either cell events or table events, or a combination of both. In Chapter 5 of my book "iText in Action - Second Edition", you'll find a PDF with double borders. See the PressPreviews example to see how this was done. Note that all examples from the book were ported to C#. You can find these examples here.
The official site also has examples where the borders are dotted lines, have rounded corners, and so on.
iTextSharp has no setBorder() method.
I tried the following:
cell.HasBorder(Rectangle.NO_BORDER); // <= this did nothing
cell.BorderColor = BaseColor.WHITE; // <= this works for me, but if your document's background is not white, you might not like it
cell.BorderWidth = 0; // <= this works like gangbusters
So it seems the "safest" way to go is to set BorderWidth to zilch AKA nada.
The following worked for me.
cell.Border = Rectangle.NO_BORDER;

How to control winform mschart legend text alignment c#?

How does one set the alignment of text within a chart legend object? I've tried using:
myChartName.Legends["mySeriesName"].Alignment = stringAlignment.Near
With no effect. I've also tried to create custom legend items, again resulting in no effect. Text is ALWAYS centered (along with the series marker) in the legend "box". The only text I have been able to align is the title, but I don't need titles in my application.
My research to date says the legend object is basically a table with (by default) two cells. If that is the case there should be a way to access those cells and manipulate them as table cells. So, what gives? Why can't I access the text alignment properties of the legend object? Clearly, there is something I'm missing, but for the life of me I can't seem to figure this out. Quite frustrating.
Problem solved. The CustomItem approach wasn't working either, so I tried using the LegendCellColumn Class.
I changed the LegendStyle from Column to Row, then added two CellColumns, one for the series symbol and one for the legend text. Set the alignment, margins, and column widths (that turned out to be the trick), and voila; a legend that looks like I want. Here's the code for anyone with a similar issue.
chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.SeriesSymbol, ""));
chartSel.Legends[ySeries.Name].CellColumns[0].Alignment = ContentAlignment.TopLeft;
chartSel.Legends[ySeries.Name].CellColumns[0].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(0, 0, 1, 1);
chartSel.Legends[ySeries.Name].CellColumns[0].MinimumWidth = 250;
chartSel.Legends[ySeries.Name].CellColumns[0].MaximumWidth = 250;
chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.Text, ySeries.Name));
chartSel.Legends[ySeries.Name].CellColumns[1].Alignment = ContentAlignment.MiddleLeft;
chartSel.Legends[ySeries.Name].CellColumns[1].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(0, 0, 1, 1);
chartSel.Legends[ySeries.Name].CellColumns[1].MinimumWidth = 1500;
chartSel.Legends[ySeries.Name].CellColumns[1].MaximumWidth = 1500;
It's probably not the most efficient way to do it, but it works. Technically, the legend symbol and text are still centered in the object, but because I'm forcing the widths of the two columns it has the appearance of being left-justified.
Hopefully, this may help another newbie like me avoid days of consternation.
My understanding is that Legend alignment relates to the Docking property, not really how the text is aligned within the legend. So setting Alignment to Near means positioning the legend box near the Docking direction.
It is quite hard to explain this textually. The MS Chart Samples have a subsection named Chart features -> Legends -> Style and Auto Positioning which will help you play with those two properties and understand how they are meant to work.
For inner legend alignment, you may need to use Legend.CustomItems and define LegendCell individually.
chart.Legends["Default"].CustomItems.Clear();
chart.Legends["Default"].CustomItems.Add(new LegendItem("LegendItem", Color.Red, ""));
chart.Legends["Default"].CustomItems[0].Cells.Add(new LegendCell(LegendCellType.Text, "My text", ContentAlignment.MiddleLeft));
This is described inside the Chart features -> Legends -> Legend Cells section of the samples.
While continuing to try to figure this out I stumbled on this tidbit of info from the following MSDN library page:
http://msdn.microsoft.com/en-us/library/dd456711(v=vs.100).aspx
"NOTE: You cannot adjust the individual legend items and cells in the Chart.Legends collection. To adjust them, use custom legend items."
So, back to the CustomItem route. I've got this code gleaned from several sources (including you, Dominique, thanks):
chartSel.Legends.Add(ySeries.Name);
chartSel.Series[ySeries.Name].IsVisibleInLegend = false;
chartSel.Legends[ySeries.Name].CustomItems.Clear();
LegendItem li = new LegendItem();
li.ImageStyle = LegendImageStyle.Line;
li.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.TopLeft);
li.Cells[0].BackColor = Color.CornflowerBlue; //color is only to see the cell bounds
li.Cells.Add(LegendCellType.Text, ySeries.Name, ContentAlignment.TopLeft);
li.Cells[1].BackColor = Color.Aquamarine; //color is only to see the cell bounds
chartSel.Legends[ySeries.Name].CustomItems.Add(li);
From what I can find it should work, but it doesn't. It results in a legend object with two cells; the first is empty and the second has left-justified text. I tried to post an image of it, but the system says I'm not yet worthy.
Why there is no line for the series symbol is also a mystery, but that's another problem to solve. The text justification issue is my main concern at the moment. It looks like the text within the cells is left-justified as desired, but the cells themselves are centered in the legend object; not what I wanted. I want those cells to be left-justified in the object too, so the first cell is against the left edge of the legend object.
Ideas?

C# String.Format and SpriteBatch.DrawString spacing issues

I have strings formatted using the code below
String.Format("{0,-10} {1,10}", "Kills:", kills);
String.Format("{0,-10} {1,10}", "Points:", points);
String.Format("{0,-10} {1,10}", "$:", currency);
From what I understood, the first part of the strings should be left justified with a 10 space buffer and then the integer variables should be printed right justified with a 10 space buffer.
However when attempt to draw the strings using SpriteBatch.DrawString, nothing aligns properly.
The left aligned side prints properly, but the right aligned side centres on a certain point, for example if kills = 50 and points = 5002, the 50 will be centered over the 00 in the 5002...
What is going on?
Quite simply, I suspect you're not using a monospaced font. When different characters have different widths, you can't use spaces to line things up. (Your sample works when using Console.WriteLine for example, as the console has a fixed width font by default.)
You'll either have to use a monospaced font, or you'll have to draw the strings separately - draw each string to fit the relevant area. I don't know anything about XNA, but I'd expect you to either have to measure the width of the string before you draw it (so you can subtract it from the right-hand edge, for example) or specify some sort of layout value which indicates "right-align this string with a particular point".
Most likely you draw the text with a proportional font. Bear in mind that characters don't have the same width, so you cannot align texts with spaces.
As I do not have reply privileges, (or some such thing, as there is no reply button for answers), but I would like to contribute, I will post this answer.
Jon's answer mentioned measuring the string, this is possible by spriteFont.MeasureString(string s);. This returns a Vector2, the X portion of which is the width of the rendered text. (Y is height, which could be helpful for other things) This allows you to use a font other than a monospace font.
Here is an example of using MeasureString:
I'm not really sure what the question is asking, but if you wanted a single line of text similar to "Kills:50 Points:5002" but width two different spritebatch calls you could do the following (note I typed this directly into stackoverflow, so there may be minor syntax errors):
float killStringWidth = spriteFont.MeasureString(killString).X;
spriteBatch.DrawString(spriteFont, killString, new Vector2(0,0), Color.White );
spriteBatch.DrawString(spriteFont, pointString, new Vector2(killStringWidth + 10, 0), Color.White );

Get height of text in PDF AcroField

Is there any technique to calculate the height of text in AcroField?
In other words, I have a template PDF with a body section that I want to paste my long text into and I want to get the height of the text. If it overflows, insert a new page for rest of the text.
This will give height and width:
Vector curBaseline = renderInfo.GetBaseline().GetStartPoint();
Vector topRight = renderInfo.GetAscentLine().GetEndPoint();
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(
curBaseline[Vector.I1], curBaseline[Vector.I2],
topRight[Vector.I1], topRight[Vector.I2]
);
Single curFontSize = rect.Height;
Just set your field to a font size of zero. This will automatically size the font so that the given text will fit into your field... within certain limits. I don't think it'll shrink below 6 points.
Another alternative would be to use a ColumnText and call myColText.go(true). This will "simulate" layout, letting you know what goes where without actually drawing anything to the PDF. Just whip up a columnText with the same dimensions, font&font-size as your field, and your results should be the same.
In fact, I believe iText's text field rendering code uses ColumnText internally. Yep, have a look at the source for TextField.getAppearance().
Note that the bounding box of your field isn't going to match the box the text is laid out into... you have to account for borer style & thickness. That's why I suggest you look at the source.

Categories