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 ...
随机推荐
- jQuery页面引导插件 jquery-pagewalkthrough
http://jwarby.github.io/jquery-pagewalkthrough/ 源码 https://github.com/jwarby/jquery-pagewalkthrough
- java8的新特性以及用法简介
1. 介绍 2 接口的默认方法 2 lambda表达式 2.1 函数式接口 2.2 方法与构造函数引用 2.3 访问局部变量 2.4 访问对象字段与静态变量 3. 内建函数式接口 3.1 Predic ...
- MATLAB中-27开3次方得不到-3的原因
原代码: y=@(x) x.^(/); 运行结果: >> y(-27) ans = 1.5000 + 2.5981i 原因: 用(-8)^(1/3)时,matlab其实是调用C = pow ...
- Leetcode:Edit Distance 解题报告
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 第22章 RTX 低功耗之停机模式
以下内容转载自安富莱电子: http://forum.armfly.com/forum.php STM32F103 停机模式介绍 本章节我们主要讲解停机模式,停机模式是在 Cortex™-M3 的深睡 ...
- 解决java.io.IOException: Cannot run program "javac"问题,并设置jdk版本
原因:没有配置java路径 解决方法:设置java_home路径 设置jdk版本是10的方法:在sbt文件中添加一行: javacOptions := Seq(") 然后重新执行命令:sbt ...
- NLP自然语言处理 jieba中文分词,关键词提取,词性标注,并行分词,起止位置,文本挖掘,NLP WordEmbedding的概念和实现
1. NLP 走近自然语言处理 概念 Natural Language Processing/Understanding,自然语言处理/理解 日常对话.办公写作.上网浏览 希望机器能像人一样去理解,以 ...
- java程序设计
IP地址计数器 原理:获取用户的IP地址,然后存入数据库,当再次访问时查询数据库是否存在该条数据,即可完成此程序 设计过程 创建一个连接数据库类:DB.java package com.count.O ...
- spring data jpa的update操作
简介 使用jpa进行update操作主要有两种方式: 1.调用保存实体的方法 1)保存一个实体:repository.save(T entity) 2)保存多个实体:repository.save(I ...
- mysql多表查询及其 group by 组内排序
//多表查询:得到最新的数据后再执行多表查询 SELECT *FROM `students` `st` RIGHT JOIN( //先按时间排序查询,然后分组(GROUP BY ) SELECT * ...