首页 > 解决方案 > 如何使用 Windows 窗体在 C# 中将 Listboxtext 打印为 pdf?

问题描述

我一直在开发一个应用程序,并且正在使用dapper来映射来自数据库的数据和数据访问。

我有一个球队和球员的表格。我进行了编码,以便当我单击团队名称时,它会显示该团队中的球员。我想将特定球队的球员打印到pdf 文件中。以下是我正在尝试做的代码;此外,我附上图片以便更好地理解。

我想将播放器中的播放器打印listbox到 pdf 文件中,其中包含每个播放器的图片。

管理团队的图像

private void printButton_Click(object sender, EventArgs e)
{
    if (teamsListBox.SelectedItem is TeamModel selectedTeam)
    {
        playersListBox.DataSource = selectedTeam.Players;
        nameValue.Text = playersListBox.Name;
        printDialog1.Document = printDocument1;
        nameValue.Text = playersListBox.Name;
        SetBindings();
        string items = "";
        for (int i = 0; i < selectedTeam.Players.Count; i++)
        {
            PlayerModel item = selectedTeam.Players[i];
            items += Name.ToString() + " ";

        }
        MessageBox.Show(items);
        myReader = new StringReader(items);
        if (printDialog1.ShowDialog() == DialogResult.OK)
        {
            this.printDocument1.Print();
        }

    }
}

标签: c#winformsdesktop

解决方案


您需要做的就是循环播放播放器名称和图像路径并将它们发送到您的 pdf 文件,这是使用PDFFlow 库创建带有播放器列表的 PDF 的简单代码

    {
        var imagePath = "imageFile.png";
        var numType = NumerationStyle.Arabic;
        DocumentBuilder.New()
        .AddSection()
            .AddParagraph("Cristiano Ronaldo").SetListNumbered(numType, 1).AddInlineImage(imagePath, 20, 20, ScalingMode.UserDefined).ToSection()
            .AddParagraph("Lionel Messi").SetListNumbered(numType, 1).AddInlineImage(imagePath, 20, 20, ScalingMode.UserDefined).ToSection()
            .AddParagraph("Ronaldinho").SetListNumbered(numType, 1).AddInlineImage(imagePath, 20, 20, ScalingMode.UserDefined).ToDocument()
        .Build("Result.pdf");}

结果将是这样的:

在此处输入图像描述


推荐阅读