在上面用OpenXML生成word后,原来利用Word2010里的导出成PDF功能就不能用.

然后找开源组件生成PDF,最开始用的是iTextSharp,做完导出报表了才发现,这个开源协议用的是AGPL,只能放弃,重新查找后,找到PDFSharp(MTI协议).结合了MigraDoc来生成PDF,过程大同小异,对比iTextSharp,在画相关元素时,会慢不少,20页A4内容,OpenXML和iTextSharp都能保持在2S内输出完成,而PDFSharp需要5-10S,不知是不是因为GDI+的原因.

代码不讲解了,只贴出来,只说明一点,对中文的支持,需要设置下.

System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection();
string strFontPath = @"C:/Windows/Fonts/msyh.ttf";//字体设置为微软雅黑
pfcFonts.AddFontFile(strFontPath);
Style style = document.Styles["Normal"];
style.Font = new MigraDoc.DocumentObjectModel.Font(pfcFonts.Families[0].Name, 12);

 using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.DocumentObjectModel.Shapes;
using System.Drawing;
using MigraDoc.Rendering;
namespace EDM.ReportTemplate
{
public class ExportPDF : ExportReport
{
private Section section;
private SizeF PdfSize; public override void Execute()
{
var size = InitPage();
Document doc = new Document();
section = doc.AddSection();
section.PageSetup = InitPage();
DefineStyles(doc);
PForm.ReportProgress(PShow.StartExecute, null);
//CreateHead();
foreach (ReportCommon common in Commons)
{
if (common is ReportTable)
{
ReportTable table = common as ReportTable;
// ChangeTableColumn(table);
AddTable(table, doc);
}
if (common is ReportText)
{
ReportText table = common as ReportText;
AddText(table, doc);
}
if (common is ReportImage)
{
ReportImage table = common as ReportImage;
AddImage(table, doc);
}
if (common is ReportValueList)
{
ReportValueList table = common as ReportValueList;
AddValueList(table, doc);
}
if (common is ReportValue)
{
ReportValue table = common as ReportValue;
AddValue(table, doc);
}
SetExectueProgress(common);
}
PForm.ReportProgress(PShow.EndExecute, null);
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
pdfRenderer.Document = doc;
pdfRenderer.RenderDocument();
pdfRenderer.Save(FileName);
} public static void DefineStyles(Document document)
{
System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection();
string strFontPath = @"C:/Windows/Fonts/msyh.ttf";//字体设置为微软雅黑
pfcFonts.AddFontFile(strFontPath);
Style style = document.Styles["Normal"];
style.Font = new MigraDoc.DocumentObjectModel.Font(pfcFonts.Families[].Name, );
style.Font.Color = Colors.Black; style = document.Styles["Heading1"];
//style.Font.Name = "Tahoma";
style.Font.Size = ;
style.Font.Bold = true;
style.ParagraphFormat.Alignment = ParagraphAlignment.Center;
// style.Font.Color = Colors.DarkBlue;
style.ParagraphFormat.PageBreakBefore = true;
style.ParagraphFormat.SpaceAfter = ; style = document.Styles["Heading2"];
style.Font.Size = ;
style.Font.Bold = true;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = ;
style.ParagraphFormat.SpaceAfter = ;
style.ParagraphFormat.Alignment = ParagraphAlignment.Left; style = document.Styles["Heading3"];
style.Font.Size = ;
style.Font.Bold = true;
style.Font.Italic = true;
style.ParagraphFormat.SpaceBefore = ;
style.ParagraphFormat.SpaceAfter = ;
style.ParagraphFormat.Alignment = ParagraphAlignment.Left; style = document.Styles["Heading4"];
style.Font.Size = ;
style.Font.Bold = true;
style.Font.Italic = true;
style.ParagraphFormat.SpaceBefore = ;
style.ParagraphFormat.SpaceAfter = ;
style.ParagraphFormat.Alignment = ParagraphAlignment.Left; style = document.Styles[StyleNames.Header];
style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right); style = document.Styles[StyleNames.Footer];
style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center); // Create a new style called TextBox based on style Normal
style = document.Styles.AddStyle("TextBox", "Normal");
style.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
style.ParagraphFormat.Borders.Width = 2.5;
style.ParagraphFormat.Borders.Distance = "3pt";
//TODO: Colors
style.ParagraphFormat.Shading.Color = Colors.SkyBlue; // Create a new style called TOC based on style Normal
style = document.Styles.AddStyle("TOC", "Normal");
style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right, TabLeader.Dots);
style.ParagraphFormat.Font.Color = Colors.Blue; // Create a new style called Table based on style Normal
style = document.Styles.AddStyle("Table", "Normal");
style.Font.Name = pfcFonts.Families[].Name;
style.Font.Size = ; // 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);
} public PageSetup InitPage()
{
PageSetup pageSetup = new PageSetup();
pageSetup.PageFormat = PageFormat.Letter;
PdfSharp.PageSize size = PdfSharp.PageSize.Letter;
if (PageSize == "A4")
{
pageSetup.PageFormat = PageFormat.A4;
size = PdfSharp.PageSize.A4;
}
if (PageSize == "B4")
{
pageSetup.PageFormat = PageFormat.B5;
size = PdfSharp.PageSize.A5;
}
var psize = PageSizeConverter.ToSize(size);
if (IsOrientation)
{
pageSetup.Orientation = Orientation.Landscape;
double width = psize.Width;
psize.Width = psize.Height;
psize.Width = width;
}
PdfSize = psize.ToSizeF();
return pageSetup;
} public void CreateHead()
{
var image = section.Headers.Primary.AddImage("../../PowerBooks.png");
image.Height = "2.5cm";
image.LockAspectRatio = true;
image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Right;
image.WrapFormat.Style = WrapStyle.Through;
} public void AddTable(ReportTable table, Document document)
{
var ptable = section.AddTable();
ptable.Style = "Table";
ptable.Borders.Width = 0.25;
ptable.Borders.Left.Width = 0.5;
ptable.Borders.Right.Width = 0.5;
ptable.Rows.LeftIndent = ;
int colIndex = ;
int cols = table.Column;
for (int i = ; i < cols; i++)
{
double width = GetWidth(table.GetWidth(i));
if (width != 0.0)
ptable.AddColumn(width);
else
ptable.AddColumn();
} foreach (List<string> strs in table.Table)
{
Row row = ptable.AddRow();
for (int i = ; i < cols; i++)
{
string text = string.Empty;
if (strs.Count > i)
{
if (!string.IsNullOrEmpty(strs[i]))
text = strs[i];
}
//如果有4栏,但是只有三列数据,那到第三列时,开始合并
if (strs.Count != cols && i >= strs.Count - )
{
var cell = row.Cells[strs.Count - ];
if (i == strs.Count - )
{
cell.AddParagraph(text);
cell.MergeRight = ;
}
if (i > strs.Count - )
{
cell.MergeRight = cell.MergeRight + ;
}
}
else
{
row.Cells[i].AddParagraph(text);
}
if (colIndex == && table.IsHaveColumn)
{
row.Format.Font.Bold = true;
}
if (table.IsHaveLevel && colIndex != )
{
if (!strs[].StartsWith(" "))
{
row.Format.Font.Bold = true;
}
}
}
colIndex++;
}
} public void AddText(ReportText text, Document document)
{
if (text.Size == )
{
var paragraph = section.AddParagraph(text.Text, "Heading1");
}
else if (text.IsHead)
{
var paragraph = section.AddParagraph(text.Text, "Heading" + (text.HeadSize - ));
}
else
{
var paragraph = section.AddParagraph(text.Text);
paragraph.Format.Font.Size = text.Size;
paragraph.Format.Alignment = GetParagraphAlignment(text.Alignment);
paragraph.Format.Font.Bold = text.IsBold;
}
} public void AddImage(ReportImage image, Document document)
{
try
{
var pImage = section.AddImage(image.Value);
pImage.Width = PdfSize.Width - ;
pImage.Left = ShapePosition.Center;
section.AddParagraph("");
}
catch (Exception e)
{
}
} public void AddValueList(ReportValueList valueList, Document document)
{
var ptable = section.AddTable();
ptable.Borders.Visible = false;
ptable.Rows.LeftIndent = ;
for (int c = ; c < valueList.Column; c++)
{
ptable.AddColumn(PdfSize.Width / valueList.Column);
} for (int i = ; i < valueList.Values.Count; i++)
{
var value = valueList.Values[i];
//当前行数
int rowindex = i / valueList.Column;
//当前列数
int colindex = i % valueList.Column;
if (colindex == )
{
ptable.AddRow();
}
var cell = ptable[rowindex, colindex];
cell.Borders.Visible = false;
this.AnalysisText(value);
cell.AddParagraph(value.Text + value.Value);
} } public void AddValue(ReportValue value, Document document)
{
this.AnalysisText(value);
var paragraph = section.AddParagraph(value.Text + value.Value);
//paragraph.Format.Alignment = GetParagraphAlignment(text.Alignment);
} public ParagraphAlignment GetParagraphAlignment(int alignment)
{
ParagraphAlignment result = ParagraphAlignment.Left;
if (alignment == )
result = ParagraphAlignment.Center;
if (alignment == )
result = ParagraphAlignment.Right;
return result;
} }

总的来说,和OpenXML一样,在用OpenXML导出数据时,已经把数据整理为,输出数据,输出图形,输出表格,输出一组数据.这几种形式,我用PDFSharp时,只是针对这几个类型进行处理一下就行了.

毕竟OpenXML只能导出word2007及以上识别的文件,word2003还是需要Ms office com组件,这个类我就不贴了,搜索出word文章几乎都用的这种.

04-30 03:38