PdfSharp Image in Cell overlapping on other rows - c#

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.

Related

Image inside a cell overlapping other cells in MigraDoc

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

How do you add an image to a single specific cell in a DataGridView?

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..

Align Two Paragraphs in One Line

I'm using MigraDoc to create PDF file. I'm adding two paragraph in a textframe. One paragraph is a label while the other is a textbox. After adding the paragraphs to textframe I added the textframe to table row cell. Currently the textbox location goes below the label. I want it to be just beside the label or they are just in one line. Anyone knows the solution for this? Please help. Here's my code and image
Code:
static void AddTextBlockAndTextBoxToRow(Row row, int cellIndex, Paragraph label, Paragraph textbox)
{
var textFrame = new TextFrame();
label.Format.Alignment = ParagraphAlignment.Left;
textbox.Format.Alignment = ParagraphAlignment.Left;
textFrame.Add(label);
textFrame.Add(textbox);
row.Cells[cellIndex].Add(textFrame);
}
Image
MigraDoc cannot show two paragraphs side by side. Not in one table cell, not in one TextFrame.
You could create a table with two columns inside your TextFrame to work around this limitation.
Or do it without TextFrame and create two cells in the main table (you can use MergeRight for other rows to merge those two cells in other rows).
Sample table program in pdfsharp....
Table table = document.LastSection.AddTable();
table.Borders.Visible = true;
table.Format.Shading.Color = Colors.LavenderBlush;
table.Shading.Color = Colors.Salmon;
table.TopPadding = 5;
table.BottomPadding = 5;
Column column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Center;
column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Right;
table.Rows.Height = 35;
Row row = table.AddRow();
row.VerticalAlignment = VerticalAlignment.Top;
row.Cells[0].AddParagraph("Text");
row.Cells[1].AddParagraph("Text");
row.Cells[2].AddParagraph("Text");
row = table.AddRow();
row.VerticalAlignment = VerticalAlignment.Center;
row.Cells[0].AddParagraph("Text");
row.Cells[1].AddParagraph("Text");
row.Cells[2].AddParagraph("Text");
row = table.AddRow();
row.VerticalAlignment = VerticalAlignment.Bottom;
row.Cells[0].AddParagraph("Text");
row.Cells[1].AddParagraph("Text");
row.Cells[2].AddParagraph("Text");

how to display images in gridview using c# in windowsform from imagepath stored in sql database

How to display images in gridview using C# from ImageUrl stored in sql 2008 database. Images are stored in C:\ProjectFolder\Images
My grid has 1 text column and 4 image columns and database table structure is also same.
Have tried this and this is showing same images in two rows:
Image image = Image.FromFile("image path");
DataGridViewImageColumn ImageColumn = new DataGridViewImageColumn();
ImageColumn.Image = image;
ImageColumn.Name = "Name";
ImageColumn.HeaderText = "Nice Name";
dataGridView1.Columns.Insert(0, ImageColumn);
Image image = Image.FromFile("image path");
applies default image for that column.
For chaanging image on particular row and column you should write
int rowIndex = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIndex].Cells[0].Value = Image.FromFile("image path");//for first image column
dataGridView1.Rows[rowIndex].Cells[1].Value = Image.FromFile("image path");//for second image column
dataGridView1.Rows[rowIndex].Cells[2].Value = Image.FromFile("image path");//for third image column
dataGridView1.Rows[rowIndex].Cells[3].Value = Image.FromFile("image path");//for forth image column

Windows forms - display image in gridview

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";

Categories

Resources