C# - iTextSharp The document has no pages
I have had a quick look and would say that as you specified table = new PdfPTable(2);
tells the table it will have 2 cells per row however you have only provided one cell in the first row. As you only provided one cell the table definition is not complete therefore it doesnt have any rows to render.
Now if you change your code and provide 2 cells.
phrase = new Phrase();
phrase.Add(new Chunk("Company Name\n\n", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE)));
phrase.Add(new Chunk("Company Address", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
//yes this is just duplicating content but proves the point
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
docu.Add(table);
docu.Close();
This works just fine as we now have two cells and can complete the table definition. If you try adding 3 cells you will find the 3rd cell isnt rendered as it didnt complete the table definition.
As you add cells to a table you are adding them horizontally to the table and the table will render them into rows based on the column definitions. Ie in a 2 column table the cells become
cell 1 | Row 1 Cell 1
cell 2 | Row 1 Cell 2
cell 3 | Row 2 Cell 1
cell 4 | Row 2 Cell 2
Now this statement
table = new PdfPTable(2);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 0.3f, 0.7f });
Is equivalent to:
table = new PdfPTable(new float[] { 0.3f, 0.7f });
table.TotalWidth = 500f;
table.LockedWidth = true;
As you have specified in the constructor 2 column widths which will give the table a 2 column specification.
As a complete example see the code below:
private void printPDF(object sender, EventArgs e)
{
string path = "D:\\Test\\" + Guid.NewGuid().ToString() + ".pdf";
using (var fileStream = new FileStream(path, FileMode.Create))
{
Document docu = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(docu, fileStream);
docu.Open();
//Header Table
var table = new PdfPTable(new float[] { 0.3f, 0.7f });
table.TotalWidth = 500f;
//company name
var phrase = new Phrase("Company Name", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE));
var cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
phrase = new Phrase("My test company", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
//company address
phrase = new Phrase("Company Address", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
phrase = new Phrase("123 Main Street, Your City.", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
docu.Add(table);
docu.Close();
writer.Close();
}
}
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
Related videos on Youtube
![pemaustria](https://lh5.googleusercontent.com/-oCsLrb3aaRU/AAAAAAAAAAI/AAAAAAAAALo/sU8l0AdxoSI/photo.jpg?sz=256)
pemaustria
Updated on May 25, 2022Comments
-
pemaustria about 2 years
I do have here the codes and I keep on getting the error message: The document has no pages. I also set the table widths and phrase, but still the error message occurred. I'm totally out now, I've tried searching some other cases however, they tried to fix their upon setting table widths. Is there anything that I miss. Any help would be appreciated. Thank you!
private void printPDF(object sender, EventArgs e) { Document docu = new Document(PageSize.LETTER); PdfWriter writer = PdfWriter.GetInstance(docu, new FileStream("C:\\Report\\" + empno + ".pdf", FileMode.Create)); Phrase phrase = null; PdfPCell cell = null; PdfPTable table = null; BaseColor color = null; docu.Open(); //Header Table table = new PdfPTable(2); table.TotalWidth = 500f; table.LockedWidth = true; table.SetWidths(new float[] { 0.3f, 0.7f }); //Company Name and Address phrase = new Phrase(); phrase.Add(new Chunk("Company Name\n\n", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE))); phrase.Add(new Chunk("Company Address", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK))); cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); cell.VerticalAlignment = PdfPCell.ALIGN_TOP; table.AddCell(cell); docu.Add(table); docu.Close(); } private static PdfPCell PhraseCell(Phrase phrase, int align) { PdfPCell cell = new PdfPCell(phrase); cell.BorderColor = BaseColor.WHITE; cell.VerticalAlignment = PdfPCell.ALIGN_TOP; cell.HorizontalAlignment = align; cell.PaddingBottom = 2f; cell.PaddingTop = 0f; return cell; }
-
Nate Jenson almost 7 yearsIs there more code in your printPdf() method? It doesn't appear that you're doing anything with the document
docu
besides adding the table and closing it. Does it need to be returned to the caller?
-
-
pemaustria almost 7 yearsThank you for this! I just tried it out and it worked as it is expected. Didn't figure out about those cell per row.