PDFSharp生成PDF.
在上面用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.的更多相关文章
- PDFSharp生成PDF (转)
http://www.cnblogs.com/zhouxin/p/3228108.html 在上面用OpenXML生成word后,原来利用Word2010里的导出成PDF功能就不能用. 然后找开源组件 ...
- .net生成PDF文件的几种方式
以下为在.net mvc中,生成pdf的几种方式,资料都是在做项目时网上找的 1.使用Microsoft.Office.Interop.Word.dll将word转换为PDF dll可以单独下载,一般 ...
- 如何从Windows Phone 生成PDF文档
我需要从我的Windows Phone应用程序生成PDF. 遗憾的是没有标准的免费的PDF生成库在Windows Phone上运行. 我不得不自己生成PDF,通过直接写入到文件格式. 这竟然是真的很容 ...
- 利用Java动态生成 PDF 文档
利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...
- html 生成pdf
HTML生成PDF(c#) 最近因为工作需要,小小的研究了一下HTML生成PDF的方法,这方面的内容很多,但要么是不尽如人意的方法,要么就是那种收费的类库!为了广大.neter的福利,把自己的一点小小 ...
- iTextSharp生成pdf的一个简单例子
效果图: 参考:http://www.cnblogs.com/CareySon/archive/2011/11/09/2243496.html http://www.cnblogs.com/julyl ...
- 生成 PDF 全攻略【2】在已有PDF上添加内容
项目在变,需求在变,不变的永远是敲击键盘的程序员..... PDF 生成后,有时候需要在PDF上面添加一些其他的内容,比如文字,图片.... 经历几次失败的尝试,终于获取到了正确的代码书写方式. 在此 ...
- PHP 生成PDF
一个项目中需要用到网页生成PDF,就是将整个网页生成一个PDF文件, 以前也用过HTML2PDF,只能生成一些简单的HTML代码,复杂的HTML + css 生成的效果惨不忍睹, 百度了一下,发现有个 ...
- 用js生成PDF的方案
在java里,我们常用Itext来生成pdf,在pdf文件里组合图片,文字,画表格,画线等操作,还会遇到中文支持的问题. 那好,现在想直接在web前端就生成pdf怎么办,目前有以下几个解决方案 1:J ...
随机推荐
- 【Unity】9.2 如何添加粒子组件
分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 粒子系统是作为组件附加到游戏对象上的,有两种添加办法. 二.方式1--添加已制作好的预制体 第1种方式是直接添加已经制作 ...
- Django使用manage.py备份与恢复数据
Django dumpdata and loaddata django database model dumpdata dumpdata command It is a django manageme ...
- sql server获取时间格式
在本文中,GetDate()获得的日期由两部分组成,分别是今天的日期和当时的时间: Select GetDate() 用DateName()就可以获得相应的年.月.日,然后再把它们连接起来就可以了: ...
- HTML5学习笔记(十九):Lambda和Promise
Lambda 在ES6的标准中称为Arrow Function(箭头函数).下面是一个简单的箭头函数: x => x * x 上面的定义和下面的代码定义效果一样: function (x) { ...
- Booleans
两个取值false和true.但要注意Lua中所有的值都可以作为条件.在控制结构的条件中除了false和nil为假,其他值都为真.所以Lua认为0和空串都是真.
- 破解Linux系统root用户密码
linux系统的启动过程 在介绍破解Linux系统root密码之前先了解一下linux系统的启动过程 开机自检(POST),初始化部分硬件 搜素可用于引导的启动设备(如磁盘的MBR) 读取并将控制权 ...
- LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- linux环境下 卸载 Oracle11G
1.使用SQL*PLUS停止数据库 [oracle@OracleTest oracle]$ sqlplus log SQL> connect / as sysdba SQL> shutdo ...
- java框架篇---Struts入门
首先理解Struts与MVC的关系 在传统的MVC模式中所有的请求都要先交给Servlet处理,之后由Servlet调用JavaBean,并将结果交给JSP中进行显示.结构图如下 Struts是Apa ...
- WebSphere ILog JRules 域的介绍和定制
WebSphere ILog JRules 域的介绍和定制 引言 随着企业业务的不断发展,越来越多的企业正经历着以下的情形: 企业需要对于业务系统的频繁变化做出及时的关注和响应,例如,竞争对手或经济环 ...