I want to put an image inside a tables cell in MigraDoc, but I end up with image stretching the whole row. The part of the simpilfied code looks like so:
table = sec.AddTable();
table.Borders.Width = 0.1; //only for visualization
column = table.AddColumn("6cm");
column = table.AddColumn("7.5cm");
column = table.AddColumn("2.5cm");
row = table.AddRow();
row[0].AddParagraph("title");
row[1].AddImage("C:\\sample.jpg").Width = "3cm";
row = table.AddRow();
row[0].AddParagraph(" - some text");
row = table.AddRow();
row[0].AddParagraph(" - some text");
row = table.AddRow();
row[0].AddParagraph(" - some text");
row = table.AddRow();
row[2].AddParagraph("0.00$");
First image below shows the result and the second one - the desired effect.
When you set .WrapFormat.Style = WrapStyle.Through for the image then the image will be ignored by the layouter and the height of the row will not be adapted to the image.
If the table has borders then you may have to add the image after the table to have it drawn on top of the table.
A simple alternative could be using .MergeDown for the table cell with the image. This is like RowSpan in HTML. In this case the table will grow to fit the image, so you cannot get the effect of the second image (image extends below the table).
This avoids the complications that come with WrapStyle.Through if you have to deal with many different image sizes and should give you clean pagebreaks if the table should not fit a single page.
The Invoice sample does not use images in tables, but it used MergeDown:
http://pdfsharp.net/wiki/Invoice-sample.ashx
Related
I have a DataGridView in my WinForms application. Basically, I want to input some 2D data there or something like that. It means, that I want to have both columns named and rows named. And so I did. But then there was a problem. When it's fine with column names, the row names tend to be not visible. For example:
The code I use to somehow "beautify" the DataGridView:
private void BeautifyTable(TableView tableView)
{
foreach (DataGridViewRow row in tableView.Rows)
{
row.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
foreach (DataGridViewColumn col in tableView.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
}
TableView is a class created by me, and it inherits from DataGridView.
So now my question would be: Is there a way to somehow make those row names/titles appear in a normal way (in this particular case those are: s0, s1, s2.., but they're like cut from the left side).
P.S Is there a good way to "stretch" the columns? I mean if I have f.e 10 columns they would fill the whole DataGridView width, but if I would add (I do this dynamically) 5, so there would be 15 columns, still they would fit, just the width of every column would decrease?
Increase the width of the row header:
dataGridView1.RowHeadersWidth =
dataGridView1.RowHeadersWidth * 2; // or another value
Try to hide those arrows by setting the RowHeadersVisible to False in the properties of the datagridview at the Form.cs [Design] or just simply add the code below.
Me.DataGridView1.RowHeadersVisible = False
I actually found the best answer by myself.
You can set the WitdthSizeMode also for row headers, like this:
tableView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
And that's the result:
Using C# and Visual Studio, I have a DataGridView with 2 columns. For every row, the first column will display text. For every row EXCEPT one specific row, the second column will display text. In one specific cell in the second column, I need to show an image.
For example:
Row[0].Cell[0] = "test" Row [0].Cell[1] = "test"
Row[1].Cell[0] = "test" Row [1].Cell[1] = "test"
Row[2].Cell[0] = "test" Row [2].Cell[1] = need to display an image here
Row[3].Cell[0] = "test" Row [3].Cell[1] = "test"
There is more than one way to do it but here is a simple example that will set one single cell to show an image:
Bitmap bmp = (Bitmap) Bitmap.FromFile(someimagefile);
DataGridViewImageCell iCell = new DataGridViewImageCell();
iCell.Value = bmp;
dataGridView1[1, 2] = iCell;
Of course any other image source will work as well..
Try not to leak the bitmaps if you change them..
How do I remove a single cell from a word table using Novacode DocX?
I have tried:
table.Rows[0].Cells.RemoveAt(2);
and
Cell c = table.Rows[0].Cells[2];
table.Rows[0].Cells.Remove(c);
and
table.Rows[0].Cells.RemoveRange(2,4);
None of them remove any cells.
I have been working with Novacode DocX very recently. Something I have realized is that a lot of the methods classes have are inherited and are not overridden. An example is cell inherits Remove() from container and is never overridden to do what you would expect it to do.
sorcecode for Novacode.Table
What you can do to try and get around this is to hide the cell/cells, since I am assuming you are not removing an entire row or column.
Novacode.Table table = document.Tables[0]
Novacode.Row row = table.Rows[0]; //int of row that contains cell/cells to remove
int first_cell = 0; //beginning cell of range
int last_cell = 1; //last cell of range
for(int i = first_cell; i < last_cell + 1; i++)
{
foreach (var paragraph in row.Cells[i].Paragraphs)
{
paragraph.Remove(false);
}
}
row.MergeCells(first_cell, last_cell);
Novacode.Cell cell = row.Cells[first_cell];
Novacode.Border border = new Border();
border.Tcbs = Novacode.BorderStyle.Tcbs_none;
cell.SetBorder(Novacode.TableCellBorderType.Right, border);
cell.SetBorder(Novacode.TableCellBorderType.Left, border);
cell.SetBorder(Novacode.TableCellBorderType.Top, border);
cell.SetBorder(Novacode.TableCellBorderType.Bottom, border);
This code would remove text and merge the first two cells of your first table and make there borders invisible. So this would turn the area you want deleted into one invisible blank cell, assuming you are using Paragraphs of text in the Cell.
You may have to save the table back to the document.
Try
appending the table to the end of the document. if that's missing the cells
Remove and replace the table in the document.
You have to clear the child XML from the Cell Element yourself, as it is not correctly handled by the NovaCode DocX Api:
table.Rows.Last().Cells.Last().Xml.RemoveAll();
table.Rows.Last().Cells.Last().InsertParagraph();
Note that the InsertParagraph in the end is necessary as Cells cannot be empty.
In the Table class you can use the methods
MergeCells(int, int)
and
MergeCellsInColumn(int, int, int)
to 'remove' cells.
See: https://docx.codeplex.com/SourceControl/latest#DocX/Table.cs
I have to display an image in a GridView cell.
For example, let 'Stock' be a column in a GridView.
In that column, based on the condition, I have to display either an image or text. Is this possible?
Step 1 : Add your image in Resources.resx under properties folder. (ex. Picture1.jpeg)
Step 2 : Add a DataGridViewImageColumn in your DataGridView
Step 3 : Add image this way
for (int row = 0; row <= [YourDataGridViewName].Rows.Count - 1; row++)
{
((DataGridViewImageCell)gvFiles.Rows[row].Cells[1]).Value = Properties.Resources.Picture1
}
that's it
Manoj
I assume you mean DataGridView as GridView is a control in ASP.NET not Winforms.
And to answer your question: yes it is possible.
If you have DataGridViewImageColumn and want to show inside text you have to create textbox cell and replace it with default cell in that column:
var row = new DataGridViewRow();
row.Cells[Stock.Index] = new DataGridViewTextBoxCell();
row.Cells[Stock.Index].Value = "Test";
I started using PDFSharp the day before yesterday and I used the following code to add an Image to a cell:
Section section = this.document.AddSection();
Table table1 = section.Headers.Primary.AddTable();
Column column = table1.AddColumn("8.5cm");
column.Format.Alignment = ParagraphAlignment.Right;
column = table1.AddColumn("8.5cm");
column.Format.Alignment = ParagraphAlignment.Right;
Row row = table1.AddRow();
Image image = row.Cells[0].Elements.AddImage(#"C:\testdump\logo.jpg");
image.LockAspectRatio = true;
image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Left;
image.WrapFormat.Style = WrapStyle.None;
I added another table after this one:
Table table2 = section.AddTable();
The image in the cell overlaps onto table2. Anyone know why this happens?
Seems like adding a "blank" row to the second table solved the issue. Not sure how that solves the overlapping.
AFAIK the line
image.WrapFormat.Style = WrapStyle.None;
creates a free-floating image. The image gets its position from the cell, but has no effect on the layout of the table.
Use WrapStyle.None if you want to overlap the image with text or other elements.