This question already has an answer here:
How to merge rows with same value in PDF using iTextSharp, in ASP.NET and C#?
(1 answer)
Closed 6 years ago.
I am using iTextsharp to generate a table and print in pdf formant.
I am able to use colspan but when i try to use rowspan on PdfPCell it is unable to recognize it. I get "no definition for rowspan"
PdfPTable table = new PdfPTable(9);
table.WidthPercentage = 90f;
//set column widths
int[] firstTablecellwidth = { 20, 10, 5, 5, 10, 10,10,10,10 };
table.SetWidths(firstTablecellwidth);
doc1.Add(p1);
table.AddCell("Name :");
PdfPCell cell = new PdfPCell(new Phrase("Star Diamonds"));
cell.Colspan = 8;///this works fine
cell.Rowspan = 4; //does not contain definition for rowspan
table.AddCell(cell);
I remember that Rowspan wasn't defined for PdfPTable when I wrote the first edition of "iText in Action". I also remember that Rowspan was defined when I wrote the second edition of that book (that was actually a rewrite).
Given your claim that you get "no definition for Rowspan", the logical conclusion would be that you're using a mighty old version of iTextSharp. I suggest that you update to iTextSharp 5.5.9 if you want to use the code in your question, or that you upgrade to iText 7 for C# if you're just starting with your project.
Your claim is contradicted by many other answers if you'd say that you're using a recent version of iTextSharp:
Create PDF in asp.net using c# with row span & colspan
How to merge rows with same value in PDF using iTextSharp, in ASP.NET and C#?
pdfpCell.Rowspan in itextsharp not work properly
iText(Sharp): tables with headers and subheaders
...
See this forum question discussing the different iTextSharp versions. It should work in the latest iTextsharp version.
Related
1) i am using SpreadsheetLight library and i like to know how could i set row's color red or yellow ?
2) also tell me how could i set color range wise say Range["A1:Z1"] ?
3) how to apply format cell range wise ?
sheet.Range[DataRangeCoordinate].NumberFormat = "#,##0.000;[Red](-#,##0.000);#,##0.000";
the above code is devexpress spreadsheet related. so how to do the same when working with SpreadsheetLight ?
4) how to iterate in all cell value with in For loop ?
when i am using dev express spreadsheet grid then i use below code to set back & fore color
sheet.Range["A1:Z1"].Font.Color = Color.IndianRed;
sheet.Range["A1:Z1"].Fill.BackgroundColor = Color.LightGray;
sheet.Range["A1:Z1"].Style.Font.Bold = true;
5) How to set column width for all column ?
6) How to set autofit all columns ?
7) i am getting error when i am trying to create CreateStyle
my code as follows
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using SpreadsheetLight;
SLStyle style1 = sl.CreateStyle();
style.Fill.SetPattern(PatternValues.Solid, System.Drawing.Color.IndianRed, System.Drawing.Color.LightGray);
sl.SetCellStyle(1, 0, style1);
i have installed latest version of OpenXml from Nuget.
please help me with code sample. thanks
Did you check the developer documentation of spreadsheetlight?
They provide an example here: http://spreadsheetlight.com/downloads/samplecode/StyleRowColumnCell.cs
I am struggling with the Open XML SDK and I've already read a lot of posts on this topic but cannot figure it out. My goal is to have a locally created Excel file which contains a formula and edit the input online and retrieve the calculated value online.
I don't know if this is possible since Open XML may only change the data and I wonder if it is also able to perform Excels calculations.
For example, my local file contains three cells:
A1: 1
A2: 2
A3: =(A1+A2)
Using Open XML I adjust A2 to the value of 3, however the result of A3 remains 3 instead of 4.
I have already read about Excel having to recalculate, but my goal is to have an Excel file as some sort of calculation engine instead of transfering all calculations to C#.
All tips and advice are welcome.
Kind regards, Patrick
First of all thanks for all responses.
Second I guess the response answered my question and the open XML SDK is only able to adjust the file and won't do anything regarding recaculating existing formulas in the file. This will only occur when opened in Excel. I will take a look at EPPlus.
You can use something like this.
Cell cell; //supposing this is your cell referencing A3
CellFormula cellformula = new CellFormula();
cellformula.Text = "SUM(A1, A2)";
CellValue cellValue = new CellValue();
cellValue.Text = "0";
cell.Append(cellformula);
cell.Append(cellValue);
A similar example can be found at this link: Formula cells in excel using openXML
I feel there could be some ambiguity captured in the question. OpenXML is way of storing documents, therefore it is not possible to do calculations with OpenXML SDK. It is spread sheet engine (Excel application) which performs the calculations.
When inputs are updated, spreadsheet saved, calculated values should get updated.
I am using EPPlus version 3.1.3 to create a spreadsheet and I want to hide all of the columns from column L to column XFD and all the rows from the bottom most row to the end. I'm trying to hide the columns by using:
for (int i = 12; i <= 16384; i++)
{
worksheet.Column(i).Hidden = true;
}
This takes forever though for this loop to run. Does anyone know of an alternative way to hide a large amount of columns? I also have no idea how to hide the rows.
I'm wondering if there is another solution outside of EPPlus, but I really don't want to add another library to this.
I've found a solution for the columns.
I wanted to hide columns 10 to 16384 (last). The following Code did the trick and has a good performance.
//EPPlus 4.04 is used.
Dim col As ExcelColumn = osheet.Column(10)
col.ColumnMax = 16384
col.Hidden = True
Does either of these work?
worksheet.columns("L:XFD").Hidden=True
or
worksheet.columns("12:16384").Hidden=True
(please forgive me if these are miles away as I don't know EPPlus too well)
EDIT
I think Sean Cheshire's comments answer your question?
worksheet.cells("L:XFD").Hidden=True
The reference he provided seems to confirm this: EPPlus - Working with multiple columns by index rather than Alphabetical Representation
I have a small problem and the answer is probably really obvious and simple, but I guess I have failed in searching the internet for an answer, so once again I came to you guys.
I'm dynamically generating a PDF file in asp.net with c#, and right now I'm just making the base for it. One of the things it generates is a table in which a cart content should be revealed (yes I'm talking about an invoice) and I'm trying to give the table some mockup, but the mock up for the upper row will be different than the rest. (the header in which the columns are defined (Quantity, Title, Unit Price, Discount and Total)
Here's some code (it's the first time I did this so don't yell at me xD)
PdfPCell Quantity = new PdfPCell(new Phrase("Quantity"));
PdfPCell Title = new PdfPCell(new Phrase("Title"));
PdfPCell UniPr = new PdfPCell(new Phrase("Unit Price"));
PdfPCell Disc = new PdfPCell(new Phrase("Discount"));
PdfPCell Total = new PdfPCell(new Phrase("Total"));
PdfPCell[] cartheaderc = { Quantity, Title, UniPr, Disc, Total };
PdfPRow cartheader = new PdfPRow(cartheaderc);
So I've tried it this way and then say:
PdfPRow.BackgroundColor = new BaseColor(255,0,0);
Since that works for cells, I thought this might make sense, but apparently it didn't. I probably can do it when I take each cell apart, but there should be an easier way, right?
That's one problem, but sadly enough, I've got one more (although 10x more stupid and 10x easier). The color I want to use is #c5c5c5, but it doesn't want to recognize the color code.
Here is a list of the systems for ItextSharp I'm using (this is beside the standard systems from Visual Studio and SQLserver and no, I rather not want to add more systems if possible):
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
You have two questions:
You are using PdfPRow, but you're not supposed to do that. The PdfPRow class is for internal use only. You should work at the PdfPCell level. If you want to color complete rows, you can do so by using a PdfPTableEvent. See for instance the colored rows in alternating.pdf. They were colored in an AlternatingBackground table event.
You have difficulties creating the color #c5c5c5. The hexadecimal value C5 equals 197, hence you want to create the following color object: new BaseColor(197, 197, 197);
Your main mistake is that you create a PdfPRow by adding an array of PdfPCell objects. Where did you get inspiration to do so? If you find somebody who wrote such an example, please let me know and if he's near, I'll personally spank him ;-)
Tables are created like this:
PdfPTable table = new PdfPTable(5);
PdfPCell Quantity = new PdfPCell(new Phrase("Quantity"));
table.AddCell(Quantity);
PdfPCell Title = new PdfPCell(new Phrase("Title"));
table.AddCell(Title);
PdfPCell UniPr = new PdfPCell(new Phrase("Unit Price"));
table.AddCell(UniPr);
PdfPCell Disc = new PdfPCell(new Phrase("Discount"));
table.AddCell(Disc);
PdfPCell Total = new PdfPCell(new Phrase("Total"));
table.AddCell(Total);
There is an even easier way to do this. This easier way also allows you to define the background color for every cell:
PdfPTable table = new PdfPTable(5);
table.DefaultCell.BackgroundColor = new BaseColor(197, 197, 197);
table.AddCell("Quantity");
table.AddCell("Title");
table.AddCell("Unit Price");
table.AddCell("Discount");
table.AddCell("Total");
The AddCell() method will wrap the strings inside a Phrase. Create a PdfPCell with this Phrase and apply all the properties you've defined for the DefaultCell to that cell. This way, you can make sure that all the cell have the same back ground color (or border, or...). Obviously, the properties of the DefaultCell will be ignored if you create PdfPCell instances yourself.
I have a table in a docx file and i want to proccess it and change the height of a row.Here is my code so far
WordprocessingDocument wordDoc = WordprocessingDocument.Open("path_to_file", true) ;
Table table = wordDoc.MainDocumentPart.Document.Body.Elements<Table>().ElementAt(1);
TableRow row = table.Elements<TableRow>().ElementAt(1);
What i want is to change the height of a table row (to zero so that i can hide it in certain circumstances). The problem proves to be harder than it seems...
After you have the row object, try this piece of code
TableRowHeight trh = row.OfType<TableRowHeight>().FirstOrDefault();
trh.Val = 0;
I highly recommend you use the Open XML SDK 2.5 Productivity tool, that way you'll have a better idea of what you're working with.
hope this helps.