65.9K
CodeProject 正在变化。 阅读更多。
Home

创建和操作 PDF 文档 - 100% .NET

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.57/5 (6投票s)

2003年12月2日

CPOL

5分钟阅读

viewsIcon

575130

TallPDF.NET 和 PDFKit.NET 是 100% .NET 组件,完全用 C# 编写,用于创建、操作和读取 PDF 文档和 PDF 表单。重点在于简化将我们的组件集成到大型应用程序中的任务。

这是我们CodeProject赞助商的展示评论。这些评论旨在为您提供我们认为对开发人员有用且有价值的产品和服务信息。

引言

TallPDF.NETPDFKit.NET 是 100% .NET 组件,完全用 C# 编写,用于创建、操作和读取 PDF 文档和 PDF 表单。重点在于简化将我们的组件集成到大型应用程序中的任务。通过提供高度直观的对象模型并支持 XML/XSL 和 ADO.NET 等主流技术来实现这一点。

下载评估版: 此链接已过期并已删除

在线查看演示:此链接已过期并已删除

TallPDF.NET 2.0

TallPDF.NET 是一个 100% 受管理的 .NET 组件,用于从头开始动态创建文档。该组件通常作为 ASP.NET 应用程序的一部分部署,该应用程序从数据源或用户输入生成动态 PDF。但是,将 TallPDF.NET 集成到桌面应用程序中同样可行。TallPDF.NET 的核心是一个高度直观的对象模型,由 Document、Section、Paragraph、Table、Footer 等类组成。

您可以通过编程方式或从 XML 创建文档。从 XML 生成 PDF 时,类由元素表示,属性由属性或子元素表示。您通常会编写一个 XSL 文件,将您自己的特定 XML 文档转换为 TallPDF.NET XML 格式。利用 XmlReader 类的非缓存前向唯一特性,您可以将这些步骤进行管道化处理,从而实现极低的内存消耗,且完全独立于文档大小。这使得 TallPDF.NET 成为生成小型和非常大型文档的绝佳解决方案。

特点

PDF 功能 书签;文档属性;内部交叉引用;外部交叉引用
文本格式化 文本颜色;单行和双行删除线;单下划线和双下划线;下标和上标;左、右和居中文本对齐;两端对齐;超链接
页面布局 垂直对齐;相对定位(流式布局);绝对定位(网格布局);混合网格和流式布局;保留下一页约束;禁止断页约束
表格 跨栏;单元格垂直和水平对齐;嵌套表格;适应内容;边框;背景;重复一个或多个标题行
生成 声明式或事件驱动式;在生成过程中闪烁页面;从 XML 使用前向唯一 XmlReader 类;从 ADO.NET 使用前向唯一数据库游标
图像 BMP、GIF、JPEG、TIFF、PNG、DCT (JPG) 和 Flate 压缩;多页 TIFF;从文件、内存或 System.Drawing.Bitmap
文档布局 将文档划分为多个节;为每个节分配页眉和页脚;标准页面尺寸;区分第一、中间、最后、奇数和偶数页眉/页脚
字体 14 种标准 PDF 字体;TrueType 子集嵌入;Unicode;支持日语、西里尔语、希伯来语、阿拉伯语等。
加密 弱密码和强密码;限制权限,例如允许打印/复制
绘图 线条、矩形、椭圆、扇形、弧形、图像、文本、画笔和钢笔,在相对定位的框内绘制,在整个页面上作为画布绘制

以编程方式生成 PDF

以下代码示例显示了从 ASP.NET 页面以编程方式生成 PDF 的基本方法。

<%@Page Language="C#"%>
<%@Import Namespace="TallComponents.PDF.Layout"%>
<%
// create a new instance of the TallPDF.NET document
Document doc = new Document();

// add a section to the document
Section section = new Section();
doc.Sections.Add( section );

// add a text paragraph to the section
TextParagraph textParagraph = new TextParagraph();
section.Paragraphs.Add( textParagraph );

// add a piece of text to the text paragraph
Fragment fragment = new Fragment();
textParagraph.Fragments.Add( fragment );
fragment.Text = "Hello world!";

// stream out the PDF directly to the browser
doc.Write( Response );
%>      

从 XML 生成 PDF

TallPDF.NET 的核心思想是您可以构建 TallPDF DOM 的实例,然后将该实例保存为 PDF。除了以编程方式构建 DOM 实例外,您还可以从 XML 加载它。从 XML 加载的 DOM 实例与以编程方式构建的 DOM 实例没有区别。

以下代码示例显示了一个典型的 XML 文件以及它如何转换为 PDF。

<document author="Jim">
   <section pagesize="Letter">
      <!-- content goes here... -->
   </section>
</document>

// create an XmlDocument and load it
XmlDocument xml = new XmlDocument();
xml.Load( "topdf.xml" );

// create a PDF document and initialize it from XML
Document document =
 new  Document(); document
.Read( xml.DocumentElement );

// stream out the PDF to file
FileStream file = new FileStream( "out.pdf", FileMode.Create );
document.Write( file );

使用 XSL 将任何 XML 文档转换为 PDF

以下代码示例展示了如何使用 XSL 将现有 XML 转换为 PDF。

// load the XML
XmlDocument xml = new XmlDocument();
xml.Load( "data.xml" );

// load the XSL
XslTransform xsl = new XslTransform();
xsl.Load( "transform.xsl" );

// transform the XML using the XSL
XmlReader reader = xsl.Transform( xml, null );

// create a new PDF document and initialize if from the transformed XML
Document document = new Document();
document.Read( reader );

// stream out the PDF to file
FileStream file = new FileStream( "out.pdf", FileMode.Create );
document.Write( file );

PDFKit.NET 1.0

PDFKit.NET 允许您拆分、追加、盖印、加密 PDF 文档和填充 PDF 表单。PDFKit.NET 是一个 100% 受管理的 .NET 组件,用于动态操作 PDF 文档和表单。该组件通常作为 ASP.NET 应用程序的一部分部署,但同样可以将 PDFKit.NET 集成到桌面应用程序中。PDF.NET 的核心是一个高度直观的对象模型,由 Pages、Page、Overlay、Underlay、Shapes 和 Fields 等类组成。

特点

拆分和追加 创建新文档;从现有 PDF 文档中获取单个或多个页面;合并现有页面和新页面以创建新文档
表单填充 (多行)文本字段、复选框、单选按钮、列表框和组合框,读取字段的位置和大小,展平、删除或保留字段
安全/加密 40 位和 128 位加密;使用加密文档;设置用户和所有者密码;设置用户权限,例如允许打印/复制
盖印 多行文本(对齐、对齐、多字体等);超链接;基本形状,如线条、扇形、矩形、图像;定位、旋转和缩放形状
左键单击 gadget 并拖动以移动它。左键单击 gadget 的右下角并拖动以调整其大小。右键单击 gadget 以访问其属性。 写入磁盘、内存或直接写入 HTTP 响应;从磁盘或内存读取;恒定、低内存消耗,独立于文档大小

填充 PDF 表单

可以通过 PDFKit.NET 的对象模型访问 PDF 表单的所有表单字段。您可以按文档级别或页面级别枚举字段。您还可以按位置或按名称索引字段。

字段由 Field 类表示。您可以查询字段的类型(文本框、单选按钮等)、位置和大小以及各种属性(如“多行”和“只读”)。如果字段代表选择控件(如组合框或单选按钮组),您还可以枚举选项。最后,您可以通过设置字段的 Value 属性来填充表单字段,并通过设置 Flatten 属性来展平它。

// open an existing PDF form
FileStream file = new FileStream( "form.pdf", FileMode.Open, FileAccess.Read );
Document document = new Document( new BinaryReader( file ) );

// retrieve field
Page page = document.Pages[0];
Field field = page.Fields["LastName"]; 

/// fill out field and flatten it
field.Value = "John Smith";
field.Flatten = true;

// stream the filled PDF form directly to the browser
Response.ContentType = "application/pdf";
document.Write( new BinaryWriter( Response.OutputStream ) );

盖印 PDF 文档

PDFKit.NET 允许您打开现有的 PDF 文档并通过索引检索每个页面。页面由 Page 类表示。该类公开 2 个层:一个覆盖层和一个底层。您可以通过向每个层添加形状来在其上绘图。PDFKit.NET 提供了一组丰富的基本形状,如 Line、Pie、Rectangle、Text、Image(BMP、JPEG 等)。每个形状都可以位于页面上的任何位置,按任意角度旋转并缩放。

// open an existing PDF document
FileStream file = new FileStream( "in.pdf", FileMode.Open, FileAccess.Read );
Document document = new Document( new BinaryReader( file ) );

// stamp blue verdana text over existing content
TextShape text = new TextShape( 10, 300, "John Smith", 
                         Font.TrueType( "verdana.ttf" ), 10, Color.Blue ); 
page.Overlay.Add( text );

// stream the filled PDF form directly to the browser
Response.ContentType = "application/pdf";
document.Write( new BinaryWriter( Response.OutputStream ) );

拆分和合并 PDF 文档/页面

在您 打开 PDF 文档后,您可以从中选择页面并使用它们来构建新文档。您可以追加来自不同文档的页面。

// open existing documents
FileStream file1 = new FileStream( "x.pdf", FileMode.Open, FileAccess.Read );
Document source1 = new Document( new BinaryReader( file1 ) );

FileStream file2 = new FileStream( "y.pdf", FileMode.Open, FileAccess.Read );
Document source2 = new Document( new BinaryReader( file2 ) );

// create a new, empty document
Document target = new Document();

// append pages from the existing documents
target.Pages.Append( source1.Pages[4] );
target.Pages.Append( source2.Pages[30] );
target.Pages.Append( source1.Pages[45] );
target.Pages.Append( source2.Pages[230] );

// stream the new PDF document directly to the browser
Response.ContentType = "application/pdf";
target.Write( new BinaryWriter( Response.OutputStream ) );
© . All rights reserved.