在上面用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. create-react-app 使用详解

    快速开始 npm install -g create-react-app create-react-app my-app cd my-app/ npm start 通过http://localhost ...

  2. K8S 详细介绍

    k8s的中文文档,参考地址:http://docs.kubernetes.org.cn/227.html

  3. HTML5学习笔记(二十五):事件

    在浏览器或文档某个元素发生某个特定情况的瞬间,会作为一个事件进行广播,我们可以对其添加监听来处理特定的事件. 事件流 事件流描述了页面中接收事件的顺序. 整个事件流包含了三个阶段:事件捕获阶段.事件目 ...

  4. at org.apache.catalina.webresources.CachedResource.validateResources

    "catalina-exec-659" #5239 daemon prio=5 os_prio=0 tid=0x00007fcba8099800 nid=0x581 waiting ...

  5. (原创)c++11改进我们的模式之改进单例模式

    我会写关于c++11的一个系列的文章,会讲到如何使用c++11改进我们的程序,本次讲如何改进我们的模式,会讲到如何改进单例模式.观察者模式.访问者模式.工厂模式.命令模式等模式.通过c++11的改进, ...

  6. C++11 类型推导decltype

    我们之前使用的typeid运算符来查询一个变量的类型,这种类型查询在运行时进行.RTTI机制为每一个类型产生一个type_info类型的数据,而typeid查询返回的变量相应type_info数据,通 ...

  7. linux远程拷贝命令-scp

    因为某种原因需要远程一个CentOS主机,只能通过ssh访问并下载文件.搞了半天不知道怎么处理文件,比如上传和下载.那就学习下吧. 基本命令格式 由于使用ssh,登录之后的本机地址是不需要给出的.但是 ...

  8. 【Java】HashTable和HashMap区别

    ①继承不同 public class Hashtable extends Dictionary implements Map public class HashMap extends Abstract ...

  9. 3. 支持向量机(SVM)拉格朗日对偶性(KKT)

    1. 感知机原理(Perceptron) 2. 感知机(Perceptron)基本形式和对偶形式实现 3. 支持向量机(SVM)拉格朗日对偶性(KKT) 4. 支持向量机(SVM)原理 5. 支持向量 ...

  10. IBM ILOG JViews Charts 产品及功能介绍

    摘抄连接:http://www.ibm.com/developerworks/cn/websphere/library/techarticles/1004_lidb_ilogjchart/ IBM I ...