Increase Height of last cell - c#

I created a table using the iTextSharp.text.pdf library.
Table I created:
Code I used:
var signTable = new PdfPTable(2);
signTable.WidthPercentage = 65f;
signTable.HorizontalAlignment = Element.ALIGN_RIGHT;
signTable.SetWidths(new[] { 25, 40 });
//SignName
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFPackagingSlip.SignName", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
signTable.AddCell(cell);
//creates empty cell
cell = new PdfPCell();
signTable.AddCell(cell);
//SignDate
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFPackagingSlip.SignDate", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
signTable.AddCell(cell);
//creates empty cell
cell = new PdfPCell();
signTable.AddCell(cell);
//Signature
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFPackagingSlip.Signature", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
signTable.AddCell(cell);
//creates empty cell
cell = new PdfPCell();
signTable.AddCell(cell);
//add table to pdf document.
doc.Add(signTable);
Question
How can I increase the height of the last added empty cell?(cell in right bottom corner)
I tried using following code, but it didn't change anything:
signTable.Rows[2].SetExtraHeight(5, 80f);

You can use
cell.MinimumHeight = 80f;
or
cell.FixedHeight = 80f;

Related

i have scheduled C# exe in task scheduler,when trigerring manually it send mail and pdf but from task scheduler it fails to create and attached pdf

i have scheduled C# exe in task scheduler,when trigerring manually it send mail and pdf attached but from task scheduler it fails to create and attached pdf it is only sending mail
below is my code snippets for pdf
public static void ExportToPdF_Verify_report(DataTable myDataTable, DataTable dt2, string filename)
{
DataTable dt = myDataTable;
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10, 10, 35, 10);
Font font13 = FontFactory.GetFont("ARIAL", 13);
Font font18 = FontFactory.GetFont("ARIAL", 10, Font.BOLD);
Font fonttext = FontFactory.GetFont("ARIAL", 10, Font.NORMAL);
try
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(filename, FileMode.Create));
pdfDoc.Open();
if (dt.Rows.Count > 0)
{
PdfPTable PdfTable = new PdfPTable(1);
PdfTable.TotalWidth = 200f;
PdfTable.LockedWidth = true;
PdfPCell PdfPCell = new PdfPCell(new Phrase(new Chunk("", font18)));
//PdfPCell PdfPCell = new PdfPCell(new Phrase(new Chunk("Manual Discrepancy Report", font18)));
PdfPCell.Border = Rectangle.NO_BORDER;
PdfTable.AddCell(PdfPCell);
//DrawLine(writer, 25f, pdfDoc.Top - 30f, pdfDoc.PageSize.Width - 25f, pdfDoc.Top - 30f, new BaseColor(222,0,0));
//DrawLine(writer, 25f, pdfDoc.Top - 30f, pdfDoc.PageSize.Width - 25f, pdfDoc.Top - 30f, new BaseColor(0,0,0));
pdfDoc.Add(PdfTable);
PdfTable = new PdfPTable(4); /* new PdfPTable(dt.Columns.Count); */
PdfTable.SpacingBefore = 20f;
PdfTable.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
PdfPCell cell_heading = new PdfPCell(new Phrase(new Chunk("Coupon Verification Report", font18)));
cell_heading.BackgroundColor = BaseColor.LIGHT_GRAY;
cell_heading.MinimumHeight = 16;
cell_heading.Colspan = 4;
cell_heading.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
PdfTable.AddCell(cell_heading);
PdfTable.DefaultCell.BackgroundColor = BaseColor.WHITE;
//for (int columns = 0; columns <= dt.Columns.Count - 1; columns++)
//{
// PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Columns[columns].ColumnName, font18)));
// PdfTable.AddCell(PdfPCell);
//}
PdfTable.HorizontalAlignment = 1;
int valid_amnt = 0; int excep_amnt = 0;
for (int rows = 0; rows <= dt2.Rows.Count - 1; rows++)
{
valid_amnt = valid_amnt + int.Parse(dt2.Rows[rows][6].ToString());
excep_amnt = excep_amnt + int.Parse(dt2.Rows[rows][8].ToString());
}
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Batch Id", font18))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][0].ToString(), fonttext))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Sap Code", font18))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][3].ToString(), fonttext))));
//pdfDoc.Add(PdfTable);
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Distributor Name", font18))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][2].ToString(), fonttext))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("State", font18))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][5].ToString(), fonttext))));
//pdfDoc.Add(PdfTable);
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Batch Date", font18))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][1].ToString(), fonttext))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Valid Amount", font18))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(valid_amnt.ToString(), fonttext))));
//pdfDoc.Add(PdfTable);
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Scan Date", font18))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][4].ToString(), fonttext))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Exception Amount", font18))));
PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(excep_amnt.ToString(), fonttext))));
pdfDoc.Add(PdfTable);
PdfTable = new PdfPTable(9);
PdfTable.SpacingBefore = 20f;
PdfTable.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
PdfPCell cell_heading2 = new PdfPCell(new Phrase(new Chunk("Detailed Summary", font18)));
cell_heading2.BackgroundColor = BaseColor.LIGHT_GRAY;
cell_heading2.MinimumHeight = 15;
cell_heading2.Colspan = 9;
cell_heading2.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
PdfTable.AddCell(cell_heading2);
//for (int columns = 0; columns <= dt2.Columns.Count - 1; columns++)
string[] arr_det = new string[] { "Coupon Type", "Claimed Coupon Count", "Claimed Coupon Amount", "Coupon Receipt Count", "Valid Coupon Count", "Valid Coupon Amount", "Exceptions Coupon Count", "Exceptions Coupon Amount", "Short/Excess Coupon Count" };
for (int columns = 0; columns < arr_det.Length; columns++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(arr_det[columns], font18)));
PdfTable.AddCell(PdfPCell);
}
for (int rows = 0; rows <= dt2.Rows.Count - 1; rows++)
{
//for (int column = 0; column <9; column++)
//{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][0].ToString(), fonttext)));
PdfTable.AddCell(PdfPCell);
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][2].ToString(), fonttext)));
PdfTable.AddCell(PdfPCell);
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][3].ToString(), fonttext)));
PdfTable.AddCell(PdfPCell);
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][4].ToString(), fonttext)));
PdfTable.AddCell(PdfPCell);
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][5].ToString(), fonttext)));
PdfTable.AddCell(PdfPCell);
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][6].ToString(), fonttext)));
PdfTable.AddCell(PdfPCell);
// int sums_exception = int.Parse(dt2.Rows[rows][8].ToString())+ int.Parse(dt2.Rows[rows][9].ToString())+ int.Parse(dt2.Rows[rows][10].ToString())+ int.Parse(dt2.Rows[rows][11].ToString()) + int.Parse(dt2.Rows[rows][12].ToString()) + int.Parse(dt2.Rows[rows][13].ToString());
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][7].ToString(), fonttext)));
PdfTable.AddCell(PdfPCell);
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][8].ToString(), fonttext)));
PdfTable.AddCell(PdfPCell);
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][9].ToString(), fonttext)));
PdfTable.AddCell(PdfPCell);
//}
}
PdfPCell = new PdfPCell(new Phrase(new Chunk("Total", font18)));
PdfTable.AddCell(PdfPCell);
string sum_colmns = sum_datatable_column(dt2, 2).ToString();
PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18)));
PdfTable.AddCell(PdfPCell);
sum_colmns = sum_datatable_column(dt2, 3).ToString();
PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18)));
PdfTable.AddCell(PdfPCell);
sum_colmns = sum_datatable_column(dt2, 4).ToString();
PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18)));
PdfTable.AddCell(PdfPCell);
sum_colmns = sum_datatable_column(dt2, 5).ToString();
PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18)));
PdfTable.AddCell(PdfPCell);
sum_colmns = sum_datatable_column(dt2, 6).ToString();
PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18)));
PdfTable.AddCell(PdfPCell);
sum_colmns = sum_datatable_column(dt2, 7).ToString();
PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18)));
PdfTable.AddCell(PdfPCell);
sum_colmns = sum_datatable_column(dt2, 8).ToString();
PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18)));
PdfTable.AddCell(PdfPCell);
sum_colmns = sum_datatable_column(dt2, 9).ToString();
PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18)));
PdfTable.AddCell(PdfPCell);
pdfDoc.Add(PdfTable);
}
pdfDoc.Close();
i tried to run change
run user when user not logged on
in action changes added path for arguments and optional
checked about itextsharper
changed configure for window server 2012 R2
please anyone help me on this , any more info required let me know

iTextSharp NET Remove spacing between cells

I'm trying to add three cells in one row, which have colored background with the same color. The problem is that I see space (or vertical border) between these cells, but I don't need this space.
As you can see on the image, there is some space. Maybe this is the wrong way to do what I need. I need this green text in the center of page and white text on the right side at the same row and with the same background color.
This is my code:
PdfPCell emptyCell = new PdfPCell();
emptyCell.BackgroundColor = BaseColor.DARK_GRAY;
emptyCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
emptyCell.VerticalAlignment = Element.ALIGN_CENTER;
emptyCell.MinimumHeight = 30f;
emptyCell.Colspan = 3;
emptyCell.Border = Rectangle.NO_BORDER;
_currentTable.AddCell(emptyCell);
iTextSharp.text.Font clubFont = new iTextSharp.text.Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL, new BaseColor(0.541f, 0.765f, 0f));
PdfPCell cell = new PdfPCell();
Paragraph clubName = new Paragraph(clubString, clubFont) {Alignment = Element.ALIGN_CENTER};
cell.AddElement(clubName);
cell.BackgroundColor = BaseColor.DARK_GRAY;
cell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
cell.VerticalAlignment = Element.ALIGN_CENTER;
cell.MinimumHeight = 30f;
cell.Colspan = _currentTable.NumberOfColumns - 6;
cell.Border = Rectangle.NO_BORDER;
_currentTable.AddCell(cell);
iTextSharp.text.Font dispFont = new iTextSharp.text.Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL, BaseColor.WHITE);
PdfPCell dispCell = new PdfPCell();
Paragraph dispersion = new Paragraph(dispersionString, dispFont);
dispersion.Alignment = Element.ALIGN_CENTER;
dispCell.AddElement(dispersion);
dispCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
dispCell.VerticalAlignment = Element.ALIGN_CENTER;
dispCell.MinimumHeight = 30f;
dispCell.Colspan = 3;
dispCell.BackgroundColor = BaseColor.DARK_GRAY;
dispCell.Border = Rectangle.NO_BORDER;
_currentTable.AddCell(dispCell);

How to sort a table in a pdf using itextsharp

I am creating a table in PDF by using iTextsharp. I want to sort that table by its columns.
.
When I click on first column it must be sort in alphabetical order,second column must sort in date wise order (Ascending order),and Third/fourth columns must sort in numbers order (Ascending order). Here is my code:
public void createPdf(string filestream)
{
FileStream outputStream = new FileStream(filestream, FileMode.Create,FileAccess.Write);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, outputStream);
document.Open();
PdfPTable table = createtable();
document.Add(table);
document.Close();
}
public PdfPTable createtable()
{
PdfPTable table = new PdfPTable(4);
float[] widths = new float[] { 2f, 1f,1f, 1f};
table.SetTotalWidth(widths);
PdfPCell cell;
//First Row
cell = new PdfPCell(new Phrase("Title"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Date"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Time"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Year"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
//Second Row
cell = new PdfPCell(new Phrase("The Counterfeiters"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("01/01/2007"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("09:30"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("2007"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
//Third Row
cell = new PdfPCell(new Phrase("Requiem for a Dream"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("02/01/2000"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("11:30"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("2000"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
//Fourth Row
cell = new PdfPCell(new Phrase("Cinema Paradiso"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("12/01/1988"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("20:00"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("1988"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
//Fifth Row
cell = new PdfPCell(new Phrase("About Last Night"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("15/10/1986"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("27:00"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("1986"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
//Sixth Row
cell = new PdfPCell(new Phrase("Sixteen Candles"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("11/09/1984"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("22:30"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("1984"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
return table;
}
I dont know any idea about how to implement this.
You are trying to do something that is impossible in PDF.
There are three possible workarounds, two of which are not valid options:
Use a RichMedia annotation with an embedded .swf component. In this workaround, you don't create a table in PDF, but you visualize the table in Flex/Flash. This workaround isn't a valid option because (1) this functionality is deprecated (it won't be in ISO-32000-2), (2) there is very little support for Flash components, (3) even Adobe has abandoned Flash.
Create a PDF that is actually an XFA form. Change the table by using JavaScript. This can't be done using iText(Sharp) and this isn't a valid option because (1) XFA will be deprecated in ISO-32000-2, (2) many PDF viewers don't support XFA and (3) Adobe has informed the world that they would never support XFA on mobile.
Add the table as many times as there are columns at the same coordinates. Use Optional Content to hide/show specific tables. Make the cells in the header clickable and use JavaScript to trigger an action that makes one specific table visible and all the other tables invisible. Caveat: PDF viewers that don't support PDF 1.5 will show all the tables at the same time. Fortunately, there aren't many of those stone-age PDF viewers around anymore.
Basically, it boils down to this:
Tell your employer that he's asking something that is impossible in PDF. If he doesn't believe you, say that you won't do a thing until he shows you an example of a PDF that meets his requirements.
Use the workaround that involves OCG. Start by looking at different examples that involve optional content groups. If you don't understand my answer, please don't post a comment "give me an example please", but adapt your code and post a new question explaining what it is that you don't understand about OCG. Looking at your output table, I see that you have been reading my book "iText in Action - Second Edition" (you're using one of my examples). OCG is explained in chapter 15 of that book.
Update: a sample was sent to my personal mail address. I don't know if this is allowed by StackOverflow, but please note that I don't allow this. I answer questions on StackOverflow on a voluntary basis, but I do not answer questions for free that are sent to my personal e-mail address.
The mail that was sent to me contained a PDF document that was different than what was described in the question. That PDF document consists of 5 pages. Each page has a header that looks like a sequence of five tabs. On top of these visual tabs, an annotation is added. Clicking on such an annotation triggers a "local Goto" action. Examples for such annotations can be found in chapter 7 of the official documentation.

Adding an image next to a table in iTextSharp

I want to add an image next to a table without a newline. I have following code:
iTextSharp.text.Font font = new iTextSharp.text.Font(FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL));
iTextSharp.text.Font fontbold = new iTextSharp.text.Font(FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD));
PdfPTable tkunde = new PdfPTable(1);
tkunde.WidthPercentage = 30;
tkunde.HorizontalAlignment = Element.ALIGN_LEFT;
PdfPCell labkunNavn = new PdfPCell(new Phrase(navnn, fontbold));
labkunNavn.PaddingTop = 2f;
labkunNavn.HorizontalAlignment = Element.ALIGN_LEFT;
labkunNavn.Border = 0;
tkunde.AddCell(labkunNavn);
PdfPCell labkunAdresse = new PdfPCell(new Phrase(adresse, font));
labkunAdresse.PaddingTop = 2f;
labkunAdresse.HorizontalAlignment = Element.ALIGN_LEFT;
labkunAdresse.Border = 0;
tkunde.AddCell(labkunAdresse);
PdfPCell labkunPost = new PdfPCell(new Phrase(postnr + " " + område, font));
labkunPost.PaddingTop = 2f;
labkunPost.HorizontalAlignment = Element.ALIGN_LEFT;
labkunPost.Border = 0;
tkunde.AddCell(labkunPost);
doc.Add(tkunde);
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(#"C:\Users\Osman\Desktop\download.jpg");
png.WidthPercentage = 10;
png.ScalePercent(50F);
png.Alignment = Element.ALIGN_RIGHT;
doc.Add(png);
But it adds the image below the table as shown in the figure on following link:
http://postimg.org/image/z9rr0dk5p/4289a76a/
I want the layout to be like this instead:
http://postimg.org/image/3yae8w6q3/42ddcd80/
How do I achieve this ?.
Thanks in advance

ItextSharp text operations

table.AddCell(new Paragraph("Sample text", font));
How can I set this text to center of the cell and add background of cell?
Here is the sample code for it:
PdfPCell c;
c = new PdfPCell(new Paragraph(#"Sample text", font));
c.HorizontalAlignment = Element.ALIGN_CENTER;
c.VerticalAlignment = Element.ALIGN_MIDDLE;
c.BackgroundColor = iTextSharp.text.Color.BLACK;
table.AddCell(c);
Hope this will work..

Categories