在上面用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文章几乎都用的这种.

PDFSharp生成PDF.的更多相关文章

  1. PDFSharp生成PDF (转)

    http://www.cnblogs.com/zhouxin/p/3228108.html 在上面用OpenXML生成word后,原来利用Word2010里的导出成PDF功能就不能用. 然后找开源组件 ...

  2. .net生成PDF文件的几种方式

    以下为在.net mvc中,生成pdf的几种方式,资料都是在做项目时网上找的 1.使用Microsoft.Office.Interop.Word.dll将word转换为PDF dll可以单独下载,一般 ...

  3. 如何从Windows Phone 生成PDF文档

    我需要从我的Windows Phone应用程序生成PDF. 遗憾的是没有标准的免费的PDF生成库在Windows Phone上运行. 我不得不自己生成PDF,通过直接写入到文件格式. 这竟然是真的很容 ...

  4. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  5. html 生成pdf

    HTML生成PDF(c#) 最近因为工作需要,小小的研究了一下HTML生成PDF的方法,这方面的内容很多,但要么是不尽如人意的方法,要么就是那种收费的类库!为了广大.neter的福利,把自己的一点小小 ...

  6. iTextSharp生成pdf的一个简单例子

    效果图: 参考:http://www.cnblogs.com/CareySon/archive/2011/11/09/2243496.html http://www.cnblogs.com/julyl ...

  7. 生成 PDF 全攻略【2】在已有PDF上添加内容

    项目在变,需求在变,不变的永远是敲击键盘的程序员..... PDF 生成后,有时候需要在PDF上面添加一些其他的内容,比如文字,图片.... 经历几次失败的尝试,终于获取到了正确的代码书写方式. 在此 ...

  8. PHP 生成PDF

    一个项目中需要用到网页生成PDF,就是将整个网页生成一个PDF文件, 以前也用过HTML2PDF,只能生成一些简单的HTML代码,复杂的HTML + css 生成的效果惨不忍睹, 百度了一下,发现有个 ...

  9. 用js生成PDF的方案

    在java里,我们常用Itext来生成pdf,在pdf文件里组合图片,文字,画表格,画线等操作,还会遇到中文支持的问题. 那好,现在想直接在web前端就生成pdf怎么办,目前有以下几个解决方案 1:J ...

随机推荐

  1. 【Unity】2.6 游戏视图(Game)

    分类:Unity.C#.VS2015 创建日期:2016-03-29 一.简介 游戏视图 (Game View) 从游戏的相机 ((Camera(s)) 中呈现,代表所发布游戏的最终版.你将需要一台或 ...

  2. vue.js 首屏优化

    我们以 vue-cli 工具为例,使用 vue-router 搭建SPA应用,UI框架选用 element-ui , ajax方案选用 axios, 并引入 vuex ,使用 vuex-router- ...

  3. sql中计算某天是全年的第几周及取得某天的所在周的周一的日期的函数

    --取得某天的所在周的周一的函数 CREATE FUNCTION getMondayBtDate(@date datetime) RETURNS date AS begin DECLARE @week ...

  4. oracle 负载均衡连接方式常用SQL语句备忘录

    1.---表中有重复记录用SQL语句查询出来 select * from Recharge where RechargeSerial in (select RechargeSerial from Re ...

  5. 每日英语:Political Gridlock, Beijing Style

    To admirers outside the country, China's political system stands far above the dysfunctional democra ...

  6. 使用CountDownTimer实现倒计时功能

    // 倒计时60s new CountDownTimer(60000, 1000) { @Override public void onTick(long millisUntilFinished) { ...

  7. Eclipse报This version of the rendering library is more recent than your version of ADT ...

    http://blog.csdn.net/zhao_3546/article/details/12968295 最近使用 Help --> Check for Updates 升级了Eclips ...

  8. js中取小数整数部分函数;取小数部分

    1.丢弃小数部分,保留整数部分 parseInt(23.56); 结果:23 2.向上取整,有小数就整数部分加1 Math.ceil(23.56) 结果:24 3,四舍五入. Math.round(2 ...

  9. idea 设置字体

    1.设置 ui字体 修改编辑器的字体(也就是代码的字体):设置-Editor-Color&Font,默认的scheme是不可以更改的,你需要save as,建立一个新的(名字可以随意写个,My ...

  10. 国内Docker下载镜像提速方法之一

    众所周知,Docker Hub并没有在国内部署服务器或者使用国内的CDN服务,因此在国内特殊的网络环境下,镜像下载十分耗时.为了克服跨洋网络延迟,能够快速高效地下载Docker镜像,我采用了DaoCl ...