I have a RDLC report in C# which displays a table.
I am providing a facility to the user to select the required columns to be displayed in the report.
So when the number of columns in the report are reduced half of the page on the right side appears blank due to which the presentation of the report looks bad.
I want to find out a way using which either I can set the column size of the visible columns dynamically.
OR
I can change the table location so that the table is displayed in the centre of the page.
So far I have found that I cannot write an Expression or pass parameter to set the size or location of a control in RDLC Report.
I would like to know if there is alternative way of achieving this.
What you could do is to change the rdlc at runtime, it is just and xml file, so you can parse it and go to set the width of the table programmatically. I use this approach to translate my reports for multilanguage and it works fine.
This link shows you how to translate the report, but this code is a good starting point. I think you can easily customize it for your purpose. Change RDLC XML
This might be an alternative:
Make columns small
Add group row
In the top group row, add image that will be used to "push" col width of both rows in the group
On the image properties, set it to use External image source and use a report parameter to set which image to use
On image, set "keep original size"
Edit: You may also generate such "padding image" dynamically to allow different widths, for example in MVC:
public class ImageUtilController : Controller
{
public FileContentResult GenerateTransparentRectangle(int width, int height)
{
var image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(image))
{
g.Clear(Color.Transparent);
g.FillRectangle(new SolidBrush(Color.Transparent), 0, 0, width, height);
}
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return File(ms.ToArray(), "image/png");
}
}
To make visible columns expand and fill the space of excluded (hidden) columns so that report width is maintained, work needs to be done is as under.
The rdlc file is an xml document. It defines column widths as following xml (This is specific to RDLC file created using Visual Studio 2005, it may differ for other version). Below xml suggests that there are 6 columns in the table.
<TableColumns>
<TableColumn>
<Width>0.5in</Width>
</TableColumn>
<TableColumn>
<Width>1.125in</Width>
</TableColumn>
<TableColumn>
<Width>1in</Width>
</TableColumn>
<TableColumn>
<Width>1in</Width>
</TableColumn>
<TableColumn>
<Width>0.5in</Width>
</TableColumn>
<TableColumn>
<Width>1.375in</Width>
</TableColumn>
</TableColumns>
Basically logic is simple, just increase the width of visible columns. But its implementation requires few lines of code.
Calculate the sum of width of hidden columns and then re-calculate width of visible columns
float[] resizedwidth;
// code for recalculation goes here
Read entire report xml into string variable 'rptxml'
String rptxml = System.IO.File.ReadAllText(#"D:\SO\WinFormQ\WinFormQ\Report1.rdlc");
Replace above xml segment with modified xml segment
int start = rptxml.IndexOf("<TableColumns>");
int end = rptxml.IndexOf("</TableColumns>") + "</TableColumns>".Length;
String resizedcolumns = String.format(
"<TableColumns>"
+ "<TableColumn><Width>{0}in</Width></TableColumn>"
+ "<TableColumn><Width>{1}in</Width></TableColumn>"
+ "<TableColumn><Width>{2}in</Width></TableColumn>"
+ "<TableColumn><Width>{3}in</Width></TableColumn>"
+ "<TableColumn><Width>{4}in</Width></TableColumn>"
+ "<TableColumn><Width>{5}in</Width></TableColumn>"
+ "</TableColumns>"
, resizedwidth[0], resizedwidth[1], resizedwidth[2], resizedwidth[3], resizedwidth[4], resizedwidth[5]
);
rptxml = rptxml.Substring(0, start) + resizedcolumns + rptxml.Substring(end);
Create TextReader from string variable 'rptxml'
TextReader tr = new StringReader(rptxml);
Use LoadReportDefinition() method to load the modified report definition
reportViewer1.LocalReport.LoadReportDefinition(tr);
Continue with specifying DataSources and ReportParameters etc. and finally display the report. NOTE: Don't forget to close TextReader tr.Close().
How To adjust each column size, Check Image
Click any column header and you will see the structure of that table will be visible just above the table in gray, from there you will be able to adjust your column size. Refer to the attached image, the top part pointed "red arrow" that's your table structure and that's where you modify the structure of your table. Hope it helps.
Related
I'm creating a PDFSharp document with an embedded table created using Migradoc.
This works just fine.
As the table has a variable number of rows, I need to know the actual height after rendering it into the main PDFsharp document. I need this to continue drawing other sections just after the table created using Migradoc.
This is the code that actually prepares and renders the table:
DocumentRenderer docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
docRenderer.RenderObject(gfx, XUnit.FromPoint(x), XUnit.FromPoint(y), XUnit.FromPoint(width), populatedTable);
In the docRenderer.PrepareDocument().FormatedDocument I can get the number of pages (in my case will always be one) and the size of the actual page (A4, for example) but not the height of the actual object.
How can I achieve this?
Well, I just found out how to do it.
After you call PrepareDocument(), you can access the RenderInfo array and check its content:
RenderInfo[] info = docRenderer.GetRenderInfoFromPage(1);
double yTableHeight = info[info.Length - 1].LayoutInfo.ContentArea.Height.Point;
In my case I'm rendering directly the table to a PDFsharp document, the whole ContentArea height is for the table. On a normal page, you will have also margins to add to this number.
I am working on a winform application for the first time and I have a gridview which contains a list of products users have bought.
I have a Print button on click which allows the user to generate a receipt like the one below:
So here I am confused whether I should use "winform default RDLC or Crystal Report" or whether I should generate PDF and then let it print out as receipt, but I am not sure if PDF is a good option for receipt generation or not.
For Crystal Report, I have read that I need to install it and client (who will use this desktop application) had to install Crystal Report and also there is some licensing involve with Crystal Report which I don't want.
Also if I use Crystal Report then I am not sure if it would be possible to generate exactly above receipt (with table formatting) and will it be complicated?
Receipt is bit complicated so is there a better tool or way, or how should I generate receipt I have shown in above image?
Update : Printing paper total size is : 7.50 centimeter and user wants to print all the content in center.
Discount = FinalAmount - MRP;
Customer Name, Mobile No, Bill No, Payment Mode values are entered on the form by user itself.
I am having a Excel file which contains list of products and with each products I have information like ProductId,ProductName,MRP,Tax information like CGST,SGST.
Code to fill gridview from excel file based on Product Id:
using (OleDbConnection cnnxls = new OleDbConnection(strConn))
using (OleDbDataAdapter oda = new OleDbDataAdapter(query, cnnxls))
{
oda.Fill(dtProductList);
DataColumnCollection columns = dtProductList.Columns;
if (!columns.Contains("FinalAmount"))
{
dtProductList.Columns.Add(new DataColumn() { ColumnName = "FinalAmount", DataType = typeof(decimal) });
}
if (!columns.Contains("Quantity"))
{
dtProductList.Columns.Add(new DataColumn() { ColumnName = "Quantity", DataType = typeof(int) });
}
DataRow lastRow = dtProductList.Rows[dtProductList.Rows.Count - 1];
lastRow["FinalAmount"] = Convert.ToDecimal(lastRow["MRP"]);
lastRow["Quantity"] = 1;
}
Generate and print the receipts
You can use any report designer tool like RDLC Reports or Crystal Reports to generate a report. RDLC reports are good enough. You can print the RDLC report with or without showing the print dialog. You can also easily export the RDLC report manually or using the code.
If for any reason you don't want to use a reporting tool, as another option you can consider generating HTML report easily using Run-time T4 templates.
Using an RDLC report, how to show multiple fields in a single cell
You can easily use an expression to show multiple values in a single cell. Also as another option, you can use rows in a single row group and show different fields in a single column.
Example 1 - RDLC - Show multiple fields in a single column using expression
The following steps show you how you can display multiple fields in a single column using expression. I assume you have set up the data source and have ProductName, UnitPrice and Quantity fields. Then, follow these steps:
Drop a Table from toolbox on the report design surface.
In first column, first data row (not the header row), right click and choose ProductName (image)
Select the header of the second column and type UnitPrice/Quantity (image)
In second column, first data row, right click and choose Expression. (image)
In the expression window, enter the desired expression, for example:
= "UnitPrice: " & Fields!UnitPrice.Value.ToString() & System.Environment.NewLine & "Quantitye: " & Fields!Quantity.Value.ToString()
Example 2 - RDLC - Show multiple fields in a single column using row group
The following steps show you how you can display multiple fields in a single column. I assume you have set up the data source and have ProductName, UnitPrice and Quantity fields. Then, follow these steps:
Drop a Table from toolbox on the report design surface.
In first column, first data row (not the header row), right click and choose ProductName (image)
Select the header of the second column and type UnitPrice/Quantity (image)
Right click on row header of the first data row and choose Insert Row → Inside Group - Below (image)
In second column, first data row, right click and choose UnitPrice. (image)
Click on the [UnitPrice], and then press Home and type UnitPrice: (image)
Do the same for Quantity, in the next row in the group.
If you need another row in the group, repeat step 3.
You can setup borders of the cells by selecting them and setting BorderStyle individually for top, left, bottom and right.
Download
You can clone or download an example using expression here:
repository
zip file
A quick and easy way I used before was to generate a html page, and then use the html2pdf library to convert it to a pdf file.
You may also consider this approach since the RDLC reports/Crystal reports may be a overkill for your case.
The RDLC is powerful as well as Crystal reports. You may choose the rdlc which comes close in eliminating licensing costs.
Using RDLC
Data
You need to add datasets Here or data sources to the report which you will manipulate to meet the design and data you want.
Design
On design you just drag and drop controls to your taste. There is a challenge that sometimes what you see on the design may not what be you see on final output so you need to test much.
Printing
You can put a print preview or send directly to a pdf viewer using rdlc. Here is an example.
Conclusion
I think If you have your data generated well on the report, the design and layout won't be much of a problem using both rdlc and crystal reports.
UPDATE
Based on further information provided I have tried to do something that may come close to what you want to achieve.
I have used crystal reports as well as database table to simulate because of time. Otherwise the same can be achieved using rdlc.
The sample table i created
Here is the sample query and results from the database. I have made groups that can be accomodated by the crystal reports. You can do calculated text values using the same to put distinction between the Tax information as well as Transaction Memo.
Here is the final look after tweaking the design. The page layout may also be tweaked with regards to your taste.
Update.
For RDLC I think you need to add datasets for memo data and tax information. Take a look at the below if it comes close. I failed to make a preview there were components I hadn't installed.
for adding 3 columns in one cell
you have two options:
1- Use new line expression
=Fields!MyField1.Value + System.Environment.NewLine + Fields!MyField2.Value
2- Use something like subreport or grouping in rdlc.
the first option seams easier
I have a problem regarding rdlc reports. I need 2 different reports whose contents are exactly same, they differ only in the number of columns. I want to make only one rdlc report for it. Then will hide some of the columns according to user input and will generate the report accordingly.
The problem is while exporting to PDF one report size is fine but the report with all columns breaks. Is this possible or I need to make 2 different rdlc reports?
Make your reportbody's ConsumeContainerWhitespace is set to True.
I hope your using visibility property to hide columns.
The page might be breaking because the table width with all columns might be exceeding the Entire Page Width.
Column width cannot be changed dynamically changed using Expression.
But if you are using tablix and the maximum number of columns is fixed
then you can create a workaround for it else if the columns are
dynamic i.e if your using Matrix then you cannot change column width
dynamically based on number of columns.
I want to create custom file dialog box with several validations. So I've chosen list box with multiple columns. I want to show all files of a specific location in this list box with horizontal scroll bar. I am facing a problem. I want to set the column width property to Auto so that if a file has longer name the column width automatically increased accordingly. I didn't find any "Auto Width" property, so I have to manually put the width of the column. I am stuck by this problem.
Please help me to resolve this problem.
Take this pseudo-code and use listbox graphics to measure string (http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx)
int colWidth, nextWidth;
foreach (string file in files)
{
nextWidth = MeasureFileName(file);
if (colWidth < nextWidth) colWidth= nextWidth;
// Add file to list
}
// set column width here to value in "colWidth"
I have a basic PDF file that I has 5 different blank content areas that I want to use iTextSharp to write text too. The problem is I'm not sure the best way to accomplish this. I have attempted to use ColumnText to accomplish this, but I cannot seem to add multiple ColumnText objects.
ColumnText tagColumn = new ColumnText(pdfContentByte);
tagColumn.SetSimpleColumn(460, 100, 620, 160);
string[] tagColors = bt.DealerTagColor.Split('|');
Font tagFont = FontFactory.GetFont(bt.DealerTagFont, bt.DealerTagSize, Font.NORMAL, new BaseColor(int.Parse(tagColors[0]), int.Parse(tagColors[1]), int.Parse(tagColors[2])));
Paragraph p = new Paragraph(dto.Tag, tagFont);
p.Leading = bt.DealerTagSize;
tagColumn.AddElement(p);
tagColumn.Go();
What I like about the ColumnText is it allows me to essential define a heigth/width, and position of the text area.
Any thoughts on how I can accomplish this with or without using ColumnText? I just need to have control of the font/fontsize/font color/leading/and width of the area which will allow the text to wrap.
The easiest way would be to create and use form fields on your template PDF. This way you can reference a form field by name and set its value with a simple function call.
This article helped me:
Fill in PDF Form Fields using the Open Source iTextSharp Dynamic Link Library
EDIT: PdfPTables?
If your ColumnTexts are tabularly arranged consider using a PdfPTable. I've used PdfPTables to fill rows of data in forms generated from a blank template form. You need to figure out the x-coordinate of each of your columns and use these values when adding PdfPCells to your table, but it does work. I've never had to set an upper limit to the height of a PdfPCell but I image you can do it.
Check this page for more on PdfPTables.