Dynamic PDF Row Mockup - c#

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.

Related

OPENXML. How to apply multiple paragraph properties in a single run

Is there any way to create the Paragraph , run it and apply multiple properties in a single statemement instead of creating 'RunProperties' and append the the styles on by one.
In the example I am trying to add a second style - Font size like that but it doesn't work.
TableCell tc1_1 = new TableCell();
tc1_1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "5500" }));
tc1_1.Append(new Paragraph(new Run(
new Text("Test Company Name 123123123 123123"),
new FontSize() { Val = "8" }
)));
The issue is that you are creating invalid Open XML markup. For example, if you want to specify the font size for a specific run, this needs to be done by adding the FontSize instance to a RunProperties instance (i.e., w:rPr element).
My general recommendation is to look at Open XML markup created by Microsoft Word for what you want to have in your document, e.g., a table cell with some paragraphs and runs formatted in a certain way. You can then mimic in your code what Word has created.

Remove paragraph above a Word table in a document column with C#

I work in a three-column Word document. I would like to insert a new table within a document column flush at the top. Unfortunately it is not possible for me to insert it without a paragraph. i insert this table after a column break (see code). If i replace the column break with many paragraphs, i can place the table at the top. Unfortunately, this method is not practical and not so easy to implement programmatically.
Word.Paragraph oPara4;
var oRng = document.Bookmarks.get_Item(#"\EndOfDoc").Range;
oPara4 = document.Content.Paragraphs.Add(oRng);
Word.Range rngPara = oPara4.Range;
rngPara.InsertParagraphBefore();
rngPara.Text = "Some Text Before the break";
oPara4.Format.SpaceAfter = 24;
rngPara.InsertParagraphAfter();
rngPara.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
// Column Break
rngPara.InsertBreak(Word.WdBreakType.wdColumnBreak);
rngPara.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
// Create new table in new column (after column break)
oRng = document.Bookmarks.get_Item(#"\EndOfDoc").Range;
Word.Table tableCopy = document.Tables.Add(oRng, 1,1, ref oMissing, ref oMissing);
The reason for what you're seeing is that Word requires a paragraph between tables. The paragraph stores important information regarding the table's position on the page (this can be seen in the Word Open XML). Two immediately adjacent tables (no paragraph between them) are automatically merged. This is not a bug, as suggested elsewhere in this discussion.
You would, therefore, need a paragraph preceding all tables at the top of all columns. This paragraph could be formatted with a very small font size and no "space before" or "space after" - I'd create a special style for it.
It would be important to work in this order:
Insert the leading paragraph mark, formatted with the Normal style.
Insert the table below it (table formatting works most reliably when the table is created from the Normal style)
Apply the style for the preceding paragraph that makes it smaller
This is one of the reasons I, in response to a previous question, suggested a multi-column table across the page, rather than using newspaper columns...
Found a solution: I shift the new table until it has completely reached the new column.
Not very pretty, but it works (prototype):
// Add paragraph before table
var rangeBeforeTable = document.Bookmarks.get_Item(#"\EndOfDoc").Range;
rangeBeforeTable.InsertParagraphAfter();
// Add table
var newTableRange = document.Bookmarks.get_Item(#"\EndOfDoc").Range;
document.Content.Paragraphs.Add(newTableRange);
var newTable = document.Tables.Add(newTableRange, 1, 1, ref oMissing, ref oMissing);
newTable.Range.Paste();
// Save coordinates
int leftOriginal, topOriginal, widthOriginal, heightOriginal;
int left, top, width, height;
// Get coordinates from newly created table
word.ActiveWindow.GetPoint(out leftOriginal, out topOriginal, out widthOriginal, out heightOriginal, newTableRange);
while (true)
{
// Get coordinates from moving table
word.ActiveWindow.GetPoint(out left, out top, out width, out height, newTableRange);
if (top < topOriginal)
break;
// Add paragraph before new table and move table down
document.Content.Paragraphs.Add(newTableRange.Previous());
}
In above question there seems to be a bug in Word (tested with Version 2013)
Same document in LibreOffice (version 6.2.4.2)
The columns 1 and 2 is 1 table which spread across these 2 columns on the page. In the third column a new table is started. In word it seems to be impossible to align the top-position of all tables.

Itextsharp "RowSpan" missing? "no definition for rowspan" [duplicate]

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.

change height of table row with C# Open XML and Word docx documents

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.

Set default font for PowerPoint tables

I can create PowerPoint tables with many cells, but the default font (28 points) is too large. Without access to the user's PowerPoint template, how can I set the font for a table I have created? All I can do at the moment is:
Create the table.
Fill every cell with a single space because otherwise font changes are ignored
(though it works using the PowerPoint UI).
Create a range containing all cells and set font:
List<string> names = new List<string>();
foreach (PowerPoint.Column column in table.Columns)
foreach (PowerPoint.Cell cell in column.Cells) {
cell.Shape.TextFrame.TextRange.Text = "";
names.Add(cell.Shape.Name);
}
PowerPoint.ShapeRange range = ppt.ActivePresentation.Slides.Item(1).Shapes.Range(names.ToArray());
range.TextFrame.TextRange.Font.Size = 12;
I am using C# to control PowerPoint 2003..2010 via its COM API. My experiments have been with PowerPoint 2003.
In PPT 2010, this works w/o having to enter text into the cells (vba, but should be easy enough to translate). For demo purposes, I'm working with the currently selected table; substitute any other reference to the table as oTbl:
Dim oTbl As Table
Dim lCol As Long
Dim lRow As Long
Set oTbl = ActiveWindow.Selection.ShapeRange(1).Table
With oTbl
For lCol = 1 To .Columns.Count
For lRow = 1 To .Rows.Count
.Cell(lRow, lCol).Shape.TextFrame.TextRange.Font.Size = 28
Next
Next
End With
In PPT2003, as you've seen, you need to add text to the cell for the formatting to "take" (which seriously slows things down, unfortunately).
Because there are other instances in PPT automation where you may need to temporarily fill in text then delete it, you might want to write a more general routine that you can pass a shape to.
With Shape.TextFrame.TextRange
If Len(.Text) = 0 Then
.Text = "!##$%" ' or some other lunatic string not likely to be found in nature
End If
End With
"Pre-treat" the shape with this, do whatever's necessary, then "Un-treat" it with a companion routine that tests to see if the text = "!##$%" and sets it back to "" if so, leaves it alone otherwise.

Categories

Resources