C# - iTextSharp文档没有页面

时间:2017-08-29 01:36:31

标签: c# itext pdf-generation

我这里有代码,我不断收到错误消息:文档没有页面。我还设置了表格宽度和短语,但仍然出现了错误消息。我现在完全出去了,但我尝试过搜索其他一些情况,但是他们试图设置表格宽度。有什么我想念的吗?任何帮助,将不胜感激。谢谢!

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

1 个答案:

答案 0 :(得分:2)

我快速浏览一下,并且会说你指定table = new PdfPTable(2);告诉表格它会有每行2个单元格但是你只提供了一个单元格第一行。由于您只提供了一个单元格,因此表定义不完整,因此它没有任何要呈现的行。

现在,如果您更改代码并提供2个单元格。

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

这很好用,因为我们现在有两个单元格,可以完成表格定义。如果你尝试添加3个单元格,你会发现第3个单元格没有被渲染,因为它没有完成表格定义。

在向表中添加单元格时,您将它们水平添加到表中,表格将根据列定义将它们呈现为行。即在2列表中,单元格变为

cell 1    | Row 1 Cell 1 
cell 2    | Row 1 Cell 2
cell 3    | Row 2 Cell 1
cell 4    | Row 2 Cell 2

现在这句话

table = new PdfPTable(2);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 0.3f, 0.7f });

相当于:

table = new PdfPTable(new float[] { 0.3f, 0.7f });
table.TotalWidth = 500f;
table.LockedWidth = true;

正如您在构造函数中指定的2列宽度,这将为表提供2列规范。

作为完整示例,请参阅以下代码:

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