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);
Related
I use iTextSharp to create a PDF (it is a Receipt which will be printed on a Receipt-Printer with endless-paper-length):
public void GenrateReport() {
Document doc = new Document(new RectangleReadOnly(225f, 10000f), 5, 5, 5, 5);
var temp = Path.GetTempFileName();
PdfWriter.GetInstance(doc, new FileStream(this.reportConfiguration.DestinationPath, FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(1);
var cell = new PdfPCell();
cell.Border = iTextSharp.text.Rectangle.NO_BORDER;
cell.AddElement(new Paragraph("aaa"));
cell.VerticalAlignment = Element.ALIGN_LEFT;
table.AddCell(cell);
cell = new PdfPCell();
cell.AddElement(new Paragraph("bbb"));
cell.VerticalAlignment = Element.ALIGN_LEFT;
table.AddCell(cell);
doc.Add(table);
doc.Close();
}
The content of the table can varying. This is only a sample with two rows.
The font and the size of the font can also varying.
I am searching for a way, that I can change the height of the page at the and to the content of the page. Is this possible?
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
I have used the following code to fill my PdfPTable:
public PdfPTable GetTable(List<hsp_Narudzbe_IzdavanjeFakture4_Result> proizvodi)
{
PdfPTable table = new PdfPTable(5);
table.WidthPercentage = 100F;
table.DefaultCell.UseAscender = true;
table.DefaultCell.UseDescender = true;
Font f = new Font(Font.FontFamily.HELVETICA, 13);
f.Color = BaseColor.WHITE;
PdfPCell cell = new PdfPCell(new Phrase("Stavke narudžbe: ", f));
cell.BackgroundColor = BaseColor.BLACK;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Colspan = 5;
table.AddCell(cell);
for (int i = 0; i < 2; i++)
{
table.AddCell("Redni broj");
table.AddCell("Proizvod");
table.AddCell("Cijena");
table.AddCell("Šifra proizvoda");
table.AddCell("Kolicina");
}
table.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
table.HeaderRows = 3;
table.FooterRows = 1;
int broj = 0;
table.DeleteLastRow();
foreach (hsp_Narudzbe_IzdavanjeFakture4_Result p in proizvodi)
{
broj++;
table.AddCell(broj.ToString());
table.AddCell(p.Naziv);
table.AddCell(p.Cijena);
table.AddCell(p.Sifra);
table.AddCell(p.Kolicina.ToString());
}
float[] widths = { 15.00F, 15.00F,15.00F,15.00F};
table.GetRow(1).SetWidths(widths);
return table;
}
Little explanation of code:
broj++; represents the row number which gets incremented each time a new row is added. That way I get the number for each row.
Now my question is: how can I manipulate width of the first column (which represents the row number). I have tried the following code:
float[] widths = { 15.00F, 15.00F,15.00F,15.00F};
table.GetRow(1).SetWidths(widths);
Can someone help me out with this ?
Combining your original post and your comment, I see two questions:
You want to create a table where the first column isn't as wide as the other columns.
You want the content in those columns to be center-aligned.
I have written a small sample named ColumnWidthExample that should result in such a PDF:
This is the Java code that produces this PDF:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
float[] columnWidths = {1, 5, 5};
PdfPTable table = new PdfPTable(columnWidths);
table.setWidthPercentage(100);
table.getDefaultCell().setUseAscender(true);
table.getDefaultCell().setUseDescender(true);
Font f = new Font(FontFamily.HELVETICA, 13, Font.NORMAL, GrayColor.GRAYWHITE);
PdfPCell cell = new PdfPCell(new Phrase("This is a header", f));
cell.setBackgroundColor(GrayColor.GRAYBLACK);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setColspan(3);
table.addCell(cell);
table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f));
for (int i = 0; i < 2; i++) {
table.addCell("#");
table.addCell("Key");
table.addCell("Value");
}
table.setHeaderRows(3);
table.setFooterRows(1);
table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
for (int counter = 1; counter < 101; counter++) {
table.addCell(String.valueOf(counter));
table.addCell("key " + counter);
table.addCell("value " + counter);
}
document.add(table);
document.close();
}
It should be fairly simple to adapt this code to C#.
The first problem is solved by defining the column widths at the level of the table. For instance like this (there are other ways to get the same result):
float[] columnWidths = {1, 5, 5};
PdfPTable table = new PdfPTable(columnWidths);
The values 1, 5, and 5 aren't absolute widths. As we define the width of the table as 100% of the available width within the margins of the page, iText will divide the available width by 11 (1 + 5 + 5) and the first column will measure 1 time this value, whereas column two and three will measure 5 times this value.
Your second problem can be solved by changing the horizontal alignment. If you work with an explicit PdfPCell object, you need to change the value at the cell level, as is done in the following line:
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
If you leave it to iText to create the PdfPCell object, you need to change the alignment at the level of the default cell:
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
Note that the default cell properties are not applied when you add a PdfPCell object to the table. In that case, the properties of that PdfPCell are used.
Okay so I managed to center the cell's text in every column, now I just can't figure out how to make first row to have smallest width...
Here is the code
public PdfPTable GetTable(List<hsp_Narudzbe_IzdavanjeFakture4_Result> proizvodi)
{
PdfPTable table = new PdfPTable(5);
table.WidthPercentage = 100F;
table.DefaultCell.UseAscender = true;
table.DefaultCell.UseDescender = true;
Font f = new Font(Font.FontFamily.HELVETICA, 13);
f.Color = BaseColor.WHITE;
PdfPCell cell = new PdfPCell(new Phrase("Stavke narudžbe: ", f));
cell.BackgroundColor = BaseColor.BLACK;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Colspan = 5;
table.AddCell(cell);
for (int i = 0; i < 2; i++)
{
table.AddCell(new PdfPCell(new Phrase("Redni broj"))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER,
BackgroundColor = BaseColor.LIGHT_GRAY
});
table.AddCell(new PdfPCell(new Phrase("Proizvod"))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER,
BackgroundColor = BaseColor.LIGHT_GRAY
});
table.AddCell(new PdfPCell(new Phrase("Cijena"))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER,
BackgroundColor = BaseColor.LIGHT_GRAY
});
table.AddCell(new PdfPCell(new Phrase("Šifra"))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER,
BackgroundColor = BaseColor.LIGHT_GRAY
});
table.AddCell(new PdfPCell(new Phrase("Kolicina"))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER,
BackgroundColor = BaseColor.LIGHT_GRAY
});
}
table.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
table.HeaderRows = 3;
table.FooterRows = 1;
int broj = 0;
table.DeleteLastRow();
foreach (hsp_Narudzbe_IzdavanjeFakture4_Result p in proizvodi)
{
broj++;
table.AddCell(new PdfPCell(new Phrase(broj.ToString()))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER
});
table.AddCell(new PdfPCell(new Phrase(p.Naziv))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER
});
table.AddCell(new PdfPCell(new Phrase(p.Cijena))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER
});
table.AddCell(new PdfPCell(new Phrase(p.Sifra))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER
});
table.AddCell(new PdfPCell(new Phrase(p.Kolicina.ToString()))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER
});
}
return table;
}
Can someone help me figure out how to set the width of each row to a custom size?
Thanks !
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
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;