如何删除由ListView生成的ListViewSubItem表单PDF?

时间:2019-06-04 07:10:28

标签: c# itext

将我的ListView导出为PDF时,它会显示正确的信息,但会在每列信息的前面添加ListViewSubItems

这是我当前正在使用的代码:

private void tsbSave_Click(object sender, EventArgs e)
    {

        Document document = new Document();
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c:\\Users\\Dev\\Desktop\\TestDocument.pdf", FileMode.Create));
        document.Open();

        iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);

        PdfPTable table = new PdfPTable(lvwAttributes.Columns.Count);
        PdfPRow row = null;
        float[] columnWidths = new float[] { 10f, 20f, 30f, 10f, 10f, 10f };
        table.SetWidths(columnWidths);

        table.WidthPercentage = 100;
        int iCol = 0;
        string colname = "";
        PdfPCell cell = new PdfPCell(new Phrase("Products"));

        cell.Colspan = lvwAttributes.Columns.Count;

        foreach (ListViewItem item in lvwAttributes.Items)
        {
            if (lvwAttributes.Items.Count > 0)
            {
                table.AddCell(new Phrase(item.SubItems[0].ToString(), font5));
                table.AddCell(new Phrase(item.SubItems[1].ToString(), font5));
                table.AddCell(new Phrase(item.SubItems[2].ToString(), font5));
                table.AddCell(new Phrase(item.SubItems[3].ToString(), font5));
                table.AddCell(new Phrase(item.SubItems[4].ToString(), font5));
                table.AddCell(new Phrase(item.SubItems[5].ToString(), font5));
            }
        }
        document.Add(table);
        document.Close();
        MessageBox.Show("PDF CREATED!!");
    }

我希望它正确显示列名和信息。

例如,现在我得到

 |ListViewSubItem{Administrator} | ListViewSubItem{Full Access} | etc

我希望它显示为

 |Administrator | Full Access |

谢谢

1 个答案:

答案 0 :(得分:1)

像这样尝试,将文本放在Sub Item后面。

foreach (ListViewItem item in lvwAttributes.Items)
    {
        if (lvwAttributes.Items.Count > 0)
        {
            table.AddCell(new Phrase(item.SubItems[0].Text.ToString(), font5));
            table.AddCell(new Phrase(item.SubItems[1].Text.ToString(), font5));
            table.AddCell(new Phrase(item.SubItems[2].Text.ToString(), font5));
            table.AddCell(new Phrase(item.SubItems[3].Text.ToString(), font5));
            table.AddCell(new Phrase(item.SubItems[4].Text.ToString(), font5));
            table.AddCell(new Phrase(item.SubItems[5].Text.ToString(), font5));
        }
    }