vinod karampuri vinod karampuri - 1 month ago 19
C# Question

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.

This is my output table.

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.

Answer

You are trying to do something that is impossible in PDF.

There are three possible workarounds, two of which are not valid options:

  1. 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.
  2. 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.
  3. 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:

  1. 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.
  2. 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.

Comments