itextsharp Rowspan Table - c#

I'm trying to create a rowscape table with itextsharp but it's impossible.
I want this table:
But the one it's generated is this one:
I'm using Rowscpae = 2 for the first 2 cells (and trying to put rowscape only in one):
table = new PdfPTable(new float[] { 40f, 10f, 50f }) { WidthPercentage = 100f };
table.TotalWidth = 555f;
cell = new PdfPCell(new Phrase("¿Fuma o ha fumado alguna vez?", fuente_cabecera));
cell.Rowspan = 2;
table.AddCell(cell);
Chunk c1 = new Chunk(" SÍ ",fuente_cabecera);
Chunk c2 = new Chunk(checkBox + "\n", f);
Chunk c3 = new Chunk("NO ", fuente_cabecera);
Chunk c4 = new Chunk(uncheckBox, f);
Paragraph p1 = new Paragraph();
p1.Add(c1);
p1.Add(c2);
p1.Add(c3);
p1.Add(c4);
cell = new PdfPCell(p1);
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.Rowspan = 2;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Indique cantidad diaria", fuente_cabecera));
table.AddCell(cell);
cell = new PdfPCell(new Phrase("sssss", fuente_cabecera));
table.AddCell(cell);

Related

Horizontal alignment on row PdfPRow itextsharp (c#)

Need some help here. I'm trying text within a row looks in the same horizontal line as it's corresponding dotted lines
This is the result
And this is the part of my code related to the image:
Font coverHeaderFont = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD, BaseColor.BLACK);
Font tableContentFont = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLDITALIC, BaseColor.BLACK);
Font textIndexFont = new Font(Font.FontFamily.HELVETICA, 11, Font.BOLD, BaseColor.BLACK);
Font invisibleFont = new Font(Font.FontFamily.HELVETICA, 11, Font.BOLD, BaseColor.WHITE);
Font underText = new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD | Font.ITALIC, BaseColor.BLACK);
//table creation
PdfPTable tblCon = new PdfPTable(3); //3 columns
tblCon.WidthPercentage = 90f; //wide %
tblCon.HorizontalAlignment = 1; //centered
//tblCon.LockedWidth = true;
//relative col widths in proportions - 1/3 and 2/3
float[] widths = new float[] { 4f, 6f, 2f };//{ 6f, 4f, 2f };
tblCon.SetWidths(widths);
//leave a gap before and after the table
tblCon.SpacingBefore = 20f;
tblCon.SpacingAfter = 30f;
//Header Cell
string appHeader = "Applications";
Chunk cAppHeader = new Chunk(appHeader, underText);
// CELLS
PdfPCell cellName = new PdfPCell();
cellName.PaddingTop = 10f;
cellName.VerticalAlignment = PdfPCell.ALIGN_TOP;
cellName.BorderWidth = 1;
cellName.MinimumHeight = 30f;
//cellName.HorizontalAlignment = 0; //0=Left, 1=Center, 2=Right
PdfPCell cellSeparator = new PdfPCell();
cellSeparator.PaddingTop = 10f;
cellSeparator.VerticalAlignment = PdfPCell.ALIGN_TOP;
cellSeparator.BorderWidth = 1;
cellSeparator.MinimumHeight = 20f;
PdfPCell cellPage = new PdfPCell();
cellPage.PaddingTop = 10f;
cellPage.VerticalAlignment = PdfPCell.ALIGN_TOP;
cellPage.BorderWidth = 1;
cellPage.MinimumHeight = 30f;
for (int i = 0; i < listOfDetailsPDF.Count; i++)
{
if (i == 0) //first column, just header
{
cellName.AddElement(new Paragraph(cAppHeader));
cellSeparator.AddElement(new Paragraph(".", invisibleFont));
cellPage.AddElement(new Paragraph("1", invisibleFont));
}
var obj = listOfDetailsPDF[i];
String title = (string)obj.GetType().GetProperty("applicantName").GetValue(obj, null);
Chunk cTitle = new Chunk(title, textIndexFont);
int pNPage = (int)obj.GetType().GetProperty("pdfPages").GetValue(obj, null);
String numberPage = pNPage.ToString();
Chunk cNumPage = new Chunk(numberPage, textIndexFont);
cellName.AddElement(new Paragraph(cTitle));
cellSeparator.AddElement(new Paragraph(dottedLine));
cellPage.AddElement(new Paragraph(cNumPage));
}
tblCon.Rows.Add(new PdfPRow(new PdfPCell[] { cellName, cellSeparator, cellPage }));
I just want: the name with the dotted line with the number page look in the same line in every row.
Thank you

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

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

Increase Height of last cell

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;

Code isn't drawing a horizontal line in my PDF

I'm trying to add a horizontal line on top to divide the header text from the actual values in my pdf file:
Here's my code:
public class StudentList
{
public void PrintStudentList(int gradeParaleloID)
{
StudentRepository repo = new StudentRepository();
var students = repo.FindAllStudents()
.Where(s => s.IDGradeParalelo == gradeParaleloID);
try
{
Document document = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\Alumnos.pdf", FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
cb.SetLineWidth(2.0f); // Make a bit thicker than 1.0 default
cb.SetGrayStroke(0.95f); // 1 = black, 0 = white
cb.MoveTo(20, 30);
cb.LineTo(400, 30);
cb.Stroke();
PdfPTable table = new PdfPTable(3);
float[] widths = new float[] { 0.6f, 0.75f, 2f };
table.SetWidths(widths);
PdfPCell numeroCell = new PdfPCell(new Phrase("Nro."));
numeroCell.Border = 0;
numeroCell.HorizontalAlignment = 0;
table.AddCell(numeroCell);
PdfPCell codigoCell = new PdfPCell(new Phrase("RUDE"));
codigoCell.Border = 0;
codigoCell.HorizontalAlignment = 0;
table.AddCell(codigoCell);
PdfPCell nombreCell = new PdfPCell(new Phrase("Apellidos y Nombres"));
nombreCell.Border = 0;
nombreCell.HorizontalAlignment = 0;
table.AddCell(nombreCell);
int c = 1;
foreach (var student in students)
{
PdfPCell cell = new PdfPCell(new Phrase(c.ToString()));
cell.Border = 0;
cell.HorizontalAlignment = 0;
table.AddCell(cell);
cell = new PdfPCell(new Phrase(student.Rude.ToString()));
cell.Border = 0;
cell.HorizontalAlignment = 0;
table.AddCell(cell);
cell = new PdfPCell(new Phrase(student.LastNameFather + " " + student.LastNameMother + " " + student.Name));
cell.Border = 0;
cell.HorizontalAlignment = 0;
table.AddCell(cell);
c++;
}
table.SpacingBefore = 20f;
table.SpacingAfter = 30f;
document.Add(table);
document.Close();
}
catch (DocumentException de)
{
Debug.WriteLine(de.Message);
}
catch (IOException ioe)
{
Debug.WriteLine(ioe.Message);
}
}
}
I don't understand why the cb.Stroke() isn't working. Any suggestions?
Drawing with iTextSharp's PdfContentByte class can be a little confusing. The height is actually relative to the bottom, not the top. So the top of the page is not 0f, but instead is actually document.Top, which on your page size of PageSize.LETTER is 726f. So if you want to draw your line 30 units from the top, try:
cb.MoveTo(20, document.Top - 30f);
cb.LineTo(400, document.Top - 30f);
Just curious -- why do it the hard way instead of using table borders? (I noticed you set your borders to 0) Trying to space out lines by hand is the biggest pain. If you ever move content in your PDF around, you'll have to make sure your measurements still work.
Try this:
numeroCell.Border = 0;
numeroCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black);
numeroCell.BorderWidthBottom = 1f;
codigoCell.Border = 0;
codigoCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black);
codigoCell.BorderWidthBottom = 1f;
nombreCell.Border = 0;
nombreCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black);
nombreCell.BorderWidthBottom = 1f;
That should give you a nice solid black line under your header row, no measurements needed:

Categories