如何使用 WPF 创建包含 mysql 数据的多页 pdf?

How to create multiple pages pdf with mysql data using WPF?

我正在做一个小项目。在这个项目中,我创建了一个 WPF UI 用于编辑 mySQL 数据库。

现在,我发现使用 WPF 创建 PDF 很困难。有人说我应该使用带有批处理文件的外部程序来生成 PDF。

我想知道是否有其他选择或开源软件可以完成这项工作? 欢迎任何建议。谢谢。

PDFsharp and MigraDoc 是您可以使用的两个 .NET 库。

来自官网:

MigraDoc is a document generator. It supports almost anything you find in any good word processor. You just add paragraphs, tables, charts, arrange all this in sections, use bookmarks to create links, tables of contents, indexes, etc. MigraDoc will do the layout creating page breaks as needed. MigraDoc will create PDF or RTF documents.

PDFsharp is a .NET library for processing PDF file. You create PDF pages using drawing routines known from GDI+. Almost anything that can be done with GDI+ will also work with PDFsharp. Only basic text layout is supported by PDFsharp, and page breaks are not created automatically. The same drawing routines can be used for screen, PDF, or meta files.

他们也有 PDFsharp and MigraDoc 的例子。

PDFsharp 和 MigraDoc 在 MIT License.

下获得许可

尝试使用名为 PDFFlow 的 .Net PDF 生成库。它有许多独特的功能,如自动页码、multi-page 传播 table 和许多其他功能。 这是创建包含文本、图像和一行的简单 PDF 文件的代码示例:

var DocumentBuilder.New()    
  .AddSection()    
    .AddParagraphToSection("Hello world!")   
    .AddImage("image.png").SetWidth(250)   
  .ToSection()   
    .AddLine(300, Color.Red)   
.ToDocument()    
   .Build("Result.PDF");   

而且你上面提到了,另一个图书馆不支持中文。 用PDFFlow库添加中文文本很简单,只需要改变文本的编码:

DocumentBuilder.New()
.AddSection()
    .AddParagraph("Chinese text: 世界你好文本在这里")
    .SetFontEncodingName(EncodingNamesChineseSimplified.GBK_EUC_H)
    .SetFontName(FontNamesChineseSimplified.SimHei).ToDocument()
.Build("Result.pdf");

在使用数据库的情况下,首先你需要从数据库中获取数据到你的 collection:

创建class,例如员工:

public class employee
{
    public string name;
    public string address;
}

然后创建 collection 名员工:

List<employee> employee_list = new List<employee>();

创建与数据库的连接,并创建 select 查询:

SqlConnection conn = new SqlConnection("Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=");
conn.Open();
SqlCommand command = new SqlCommand("Select * from [employee_table]", conn);

从数据库读取数据到你的 collection:

using (SqlDataReader reader = command.ExecuteReader())
{
    if (reader.HasRows)
    {
        while (reader.Read())
        {
            var emp = new employee();
            emp.name = reader.GetString(1);
            emp.address = reader.GetString(2);
            employee_list.Add(emp);
        }
    }
}
conn.Close();

现在您需要将您的 collection 打印成 PDF 文件:

您可以使用段落或 Table 打印报告。

如果您想使用 table,这就是您的代码。只需将您的 collection 设置为数据源:

using Gehtsoft.PDFFlow;
DocumentBuilder.New()
    .AddSection()
        .AddTable()
            .AddColumnToTable("Name")
            .AddColumnToTable("Address")
        .SetDataSource(employee_list)
.ToDocument()
    .Build("Result.pdf");

或者您可以遍历 collection 并自行添加单元格:

var PDFtable = DocumentBuilder.New().AddSection()
    .AddTable()
        .AddColumnToTable("Name")
        .AddColumnToTable("Address");
foreach(var emp in employee_list)
{
    PDFtable
        .AddRow()
            .AddCellToRow(emp.Name)
            .AddCell(emp.Address);
}
PDFtable
    .ToDocument()
        .Build("Result.PDF");

此外,还有很多code examples可以帮助您创建真实的业务文档。