C# 用于3k+的PDFSharp/Migradoc PDF生成;排得非常慢
我正在尝试生成一个包含3500条记录的PDF文件。大约需要5-10分钟。这简直荒谬。我是不是遗漏了什么?它卡在C# 用于3k+的PDFSharp/Migradoc PDF生成;排得非常慢,c#,.net,wpf,pdfsharp,migradoc,C#,.net,Wpf,Pdfsharp,Migradoc,我正在尝试生成一个包含3500条记录的PDF文件。大约需要5-10分钟。这简直荒谬。我是不是遗漏了什么?它卡在RenderDocument()方法上。我使用的是MigraDoc.Rendering.dll和PdfSharp.dll1.32版 这是我的一段代码。。也许我可以在构建PDF的过程中优化一些东西?我用这段代码生成的其他PDF会立即创建大约50条记录 using System; using System.Collections.Generic; using System.Linq; usi
RenderDocument()
方法上。我使用的是MigraDoc.Rendering.dll
和PdfSharp.dll
1.32版
这是我的一段代码。。也许我可以在构建PDF的过程中优化一些东西?我用这段代码生成的其他PDF会立即创建大约50条记录
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.Rendering;
using System.Diagnostics;
namespace Migradoc_PDF_Example
{
class PDFGenerator
{
Document document;
public void BuildAndOpenPDF()
{
//Argument = true if unicode
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
// Set the MigraDoc document
pdfRenderer.Document = document;
// Create the PDF document
pdfRenderer.RenderDocument();
pdfRenderer.Save("ExamplePDF.pdf");
//Let's create a process to open our generated PDF
Process p = new Process();
p.StartInfo = new ProcessStartInfo("ExamplePDF.pdf");
p.Start(); //Open PDF
}
public PDFGenerator() //In this example, the class constructor will set up the document and BuildAndOpenPDF will build the file/open it
{
//Actually generate invoice document.. everything before this is getting the data built together before actually writing to the pdf
document = new Document();
document.Info.Title = "DOCUMENT TITLE";
document.Info.Subject = "DOCUMENT SUBJECT";
document.Info.Author = "Me";
// Get the predefined style Normal.
Style style; //Creates style variable so we can change the different styles in the document as seen below..
style = document.Styles["Normal"]; //The normal style = default for everything
style.Font.Name = "Times New Roman"; //Default normal Font Type to Times New Roman
style = document.Styles[StyleNames.Header]; //Style for Header of document
style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right);
style = document.Styles[StyleNames.Footer]; //Style for footer of document
style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center);
// Create a new style called Table based on style Normal
style = document.Styles.AddStyle("Table", "Normal");
style.Font.Name = "Times New Roman";
style.Font.Size = 9;
// Create a new style called Reference based on style Normal
style = document.Styles.AddStyle("Reference", "Normal");
style.ParagraphFormat.SpaceBefore = "5mm";
style.ParagraphFormat.SpaceAfter = "5mm";
style.ParagraphFormat.TabStops.AddTabStop("16cm", TabAlignment.Right);
// Create a new style called TextBox based on style Normal
style = document.Styles.AddStyle("TextBox", "Normal");
style.ParagraphFormat.Borders.Width = 2.5;
style.ParagraphFormat.Borders.Distance = "3pt";
style.ParagraphFormat.Shading.Color = Colors.SkyBlue;
// Each MigraDoc document needs at least one section.
Section section = document.AddSection();
// Create TextFrame to store the text at the top (inside the borderFrame)
TextFrame addressFrame;
addressFrame = section.AddTextFrame(); //add this TextFrame to our section in the document
addressFrame.Height = "1.5cm"; //12 Pt Font = 0.5cm so 3 lines = 1.5cm
addressFrame.Width = "14cm"; //16 cm width - 1 inch left indention - 1 inch right indention = 14cm
addressFrame.Left = "1cm";
addressFrame.Top = "1.0cm";
addressFrame.RelativeVertical = RelativeVertical.Page;
addressFrame.LineFormat.Width = "1pt"; //Border pixel width = 1pt
addressFrame.LineFormat.Color = Colors.Black; //Border color = black
Paragraph addressinfo = addressFrame.AddParagraph(); //Add paragraph to addressFrame to store text
addressinfo.Format.Alignment = ParagraphAlignment.Center; //Align paragraph in center
addressinfo.AddFormattedText("LINE 1 EX.\n", TextFormat.Bold);
addressinfo.AddFormattedText("LINE 2 EX.\n", TextFormat.Bold);
addressinfo.AddFormattedText("LINE 3 EX.\n", TextFormat.Bold);
addressinfo.Format.Font.Name = "Times New Roman";
addressinfo.Format.Font.Size = 12;
addressinfo.Format.SpaceAfter = 0;
Paragraph Spacing = section.AddParagraph(); //Space between top and datagrid
Spacing.Format.SpaceAfter = "4cm";
/**************************************************************************************
* TABLE LOGIC BELOW
* */
//Next is all the crap for the table below
Table table = section.AddTable();
table.Style = "Table"; //Use the table style we created above
table.Borders.Color = new Color(81, 125, 192); //Red, Green, Blue
table.Borders.Width = 0.25;
table.Borders.Left.Width = 0.5;
table.Borders.Right.Width = 0.5;
table.Rows.LeftIndent = 0;
decimal tableWidth = 0.0M; //Used so we can center the table properly
//Before adding any rows, we must add our columns
Column column = table.AddColumn("1.0cm"); //ID Column
tableWidth += 1.0M; //Required for table center to be properly calculated
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn("1.0cm"); //Another Column
tableWidth += 1.0M; //Required for table center to be properly calculated
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn("1.0cm"); //Another Column
tableWidth += 1.0M; //Required for table center to be properly calculated
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn("1.0cm"); //Another Column
tableWidth += 1.0M; //Required for table center to be properly calculated
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn("1.0cm"); //Another Column
tableWidth += 1.0M; //Required for table center to be properly calculated
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn("1.0cm"); //Another Column
tableWidth += 1.0M; //Required for table center to be properly calculated
column.Format.Alignment = ParagraphAlignment.Left;
table.Rows.LeftIndent = ((16 - tableWidth) / 2).ToString() + "cm"; //Use this to center the table - Note: Max table width = 16CM
//Create Header Row for the table
Row row = table.AddRow();
row.HeadingFormat = true;
row.Format.Alignment = ParagraphAlignment.Center;
row.Format.Font.Bold = true;
row.Shading.Color = new Color(235, 240, 249); //Red Green Blue
row.Cells[0].AddParagraph("ID");
row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
row.Cells[1].AddParagraph("poopy");
row.Cells[1].Format.Alignment = ParagraphAlignment.Left;
row.Cells[2].AddParagraph("butt");
row.Cells[2].Format.Alignment = ParagraphAlignment.Left;
row.Cells[3].AddParagraph("randall");
row.Cells[3].Format.Alignment = ParagraphAlignment.Left;
row.Cells[4].AddParagraph("jim");
row.Cells[4].Format.Alignment = ParagraphAlignment.Left;
row.Cells[5].AddParagraph("duh");
row.Cells[5].Format.Alignment = ParagraphAlignment.Left;
table.SetEdge(0, 0, 6, 1, Edge.Box, BorderStyle.Single, 0.75, Color.Empty); //This is to draw box around header row, arguments are weird, only change 3rd argument to # of columns
//Now let's add our fake data
for (int i=0; i<3600; i++)
{
Row newRow = table.AddRow();
//ID Column [0]
newRow.Cells[0].Format.Alignment = ParagraphAlignment.Left;
newRow.Cells[0].AddParagraph("ID#"+i.ToString()); //Ex. ID#1 ID#2
//Another Column [1]
newRow.Cells[1].Format.Alignment = ParagraphAlignment.Left;
newRow.Cells[1].AddParagraph("test1");
//Amount Column [1]
newRow.Cells[2].Format.Alignment = ParagraphAlignment.Left;
newRow.Cells[2].AddParagraph("test2");
//Another Column [1]
newRow.Cells[3].Format.Alignment = ParagraphAlignment.Left;
newRow.Cells[3].AddParagraph("test3");
//Another Column [1]
newRow.Cells[4].Format.Alignment = ParagraphAlignment.Left;
newRow.Cells[4].AddParagraph("test4");
//Another Column [1]
newRow.Cells[5].Format.Alignment = ParagraphAlignment.Left;
newRow.Cells[5].AddParagraph("test5");
}
// Create footer
Paragraph footer = section.Footers.Primary.AddParagraph();
footer.AddText("Created by Me " + DateTime.Now.Year.ToString());
footer.Format.Font.Size = 8;
footer.Format.Alignment = ParagraphAlignment.Center;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用MigraDoc.DocumentObjectModel;
使用MigraDoc.DocumentObjectModel.Tables;
使用MigraDoc.DocumentObjectModel.Shapes;
使用文档渲染;
使用系统诊断;
名称空间:doc\u PDF\u示例
{
类PDF发生器
{
文件;
public void BuildAndOpenPDF()
{
//参数=如果为unicode,则为true
PdfDocumentRenderer pdfRenderer=新的PdfDocumentRenderer(true);
//设置文档的名称
pdfRenderer.Document=文档;
//创建PDF文档
pdfRenderer.RenderDocument();
pdfRenderer.Save(“ExamplePDF.pdf”);
//让我们创建一个过程来打开生成的PDF
过程p=新过程();
p、 StartInfo=新流程StartInfo(“ExamplePDF.pdf”);
p、 Start();//打开PDF
}
public PDFGenerator()//在本例中,类构造函数将设置文档,BuildAndOpenPDF将构建/打开该文件
{
//实际生成发票文档。在此之前的所有工作都是在实际写入pdf之前将数据构建在一起
文件=新文件();
document.Info.Title=“文档标题”;
document.Info.Subject=“文档主题”;
document.Info.Author=“我”;
//使预定义的样式正常。
Style Style;//创建样式变量,以便我们可以更改文档中的不同样式,如下所示。。
style=document.Styles[“Normal”];//Normal style=所有内容的默认样式
style.Font.Name=“Times New Roman”;//默认正常字体类型为Times New Roman
style=document.style[StyleNames.Header];//文档头的样式
style.ParagraphFormat.AddTabStop(“16cm”,TabAlignment.Right);
style=document.style[StyleNames.Footer];//文档页脚的样式
style.ParagraphFormat.AddTabStop(“8cm”,TabAlignment.Center);
//基于“常规样式”创建名为“表格”的新样式
style=document.Styles.AddStyle(“表”、“正常”);
style.Font.Name=“泰晤士报新罗马”;
style.Font.Size=9;
//基于样式“法线”创建名为“参照”的新样式
style=document.Styles.AddStyle(“参考”、“正常”);
style.ParagraphFormat.SpaceBefore=“5mm”;
style.ParagraphFormat.SpaceAfter=“5mm”;
style.ParagraphFormat.TabStops.AddTabStop(“16cm”,TabAlignment.Right);
//基于常规样式创建名为TextBox的新样式
style=document.Styles.AddStyle(“文本框”、“普通”);
style.ParagraphFormat.Borders.Width=2.5;
style.ParagraphFormat.Borders.Distance=“3pt”;
style.ParagraphFormat.Shading.Color=Colors.SkyBlue;
//每个文档至少需要一个部分。
Section=document.AddSection();
//创建文本框以将文本存储在顶部(边框框内)
文本框地址框;
addressFrame=section.AddTextFrame();//将此TextFrame添加到文档中的节中
addressFrame.Height=“1.5cm”//12 Pt Font=0.5cm所以3行=1.5cm
addressFrame.Width=“14cm”//16厘米宽-1英寸左缩进-1英寸右缩进=14cm
addressFrame.Left=“1cm”;
addressFrame.Top=“1.0cm”;
addressFrame.RelativeVertical=RelativeVertical.Page;
addressFrame.LineFormat.Width=“1pt”//边框像素宽度=1pt
addressFrame.LineFormat.Color=Colors.Black;//边框颜色=Black
段落地址信息=addressFrame.AddParague();//将段落添加到addressFrame以存储文本
addressinfo.Format.Alignment=ParagraphAlignment.Center;//在中间对齐段落
addressinfo.AddFormattedText(“第1行EX.\n”,TextFormat.Bold);
addressinfo.AddFormattedText(“第2行EX.\n”,TextFormat.Bold);
addressinfo.AddFormattedText(“第3行EX.\n”,TextFormat.Bold);
addressinfo.Format.Font.Name=“Times New Roman”;
addressinfo.Format.Font.Size=12;
addressinfo.Format.SpaceAfter=0;
段落间距=section.addparagration();//顶部和数据网格之间的间距
Spacing.Format.SpaceAfter=“4cm”;
/**************************************************************************************
*下表逻辑
* */
//下一步是下表的所有垃圾
Table Table=section.AddTable();
table.Style=“table”//使用上面创建的表格样式
table.Borders.Color=新颜色(81125192);//红、绿、蓝
表1.1.1宽度=0.25;
table.Borders.Left.Width=0.5;
table.Borders.Right.Width=0.5;
table.Rows.LeftIndent=0;
decimal tableWidth=0.0M;//用于使表格正确居中
//在添加任何行之前,我们必须添加列
纵队
public void RenderObject(DocumentRenderer docRenderer, XUnit xPosition, XUnit yPosition, XUnit width, DocumentObject documentObject, XGraphics currentGfx)
{
// we risk to cause an exception in order to call the time consuming function PrepareDocument as few times as possible
try
{
docRenderer.RenderObject(currentGfx, xPosition, yPosition, width, documentObject);
}
catch
{
docRenderer.PrepareDocument();
docRenderer.RenderObject(currentGfx, xPosition, yPosition, width, documentObject);
}
}