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..
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?
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);
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.
I have developed a pdf report but it contains some empty cells. how can I hide this empty cells? My o/p is,
Here I want to remove/hide the empty cells. When you read this you may think why he cannot declare table colomn count as his need. But here situation is different, my table cells fills as per my query result. ie, sometimes there will not be any empty cells...depends up on my query result. The way I create it,
PdfPTable table1 = new PdfPTable(30);
table1.TotalWidth = 800f;//table size
table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(new PdfPCell(new Phrase("80", font1)));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(new PdfPCell(new Phrase("85", font1)));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(new PdfPCell(new Phrase("85", font1)) );
table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(new PdfPCell(new Phrase("85", font1)));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(new PdfPCell(new Phrase("85", font1)) );
table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(new PdfPCell(new Phrase("" + lblSub4.Text "",font1)));
table.CompleteRow();
doc.Add(table);
This code I added only to know how I created pdf table.just a sample one it is
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;