-
itextsharp.dll(4.0.8.0)完整示例PDF
private void button1_Click(object sender, EventArgs e)
{
//Document:(文档)生成pdf必备的一个对象,生成一个Document示例
Document document = new Document(PageSize.A4, 30, 30, 5, 5);
//为该Document创建一个Writer实例:
PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));
//打开当前Document
document.Open();//为当前Document添加内容:
document.Add(new Paragraph("Hello World"));
//另起一行。有几种办法建立一个段落,如:
Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.\n", FontFactory.GetFont(FontFactory.HELVETICA, 12)));
Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12)));
Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12));
//所有有些对象将被添加到段落中:
p1.Add("you can add string here\n\t");
p1.Add(new Chunk("you can add chunks \n")); p1.Add(new Phrase("or you can add phrases.\n"));
document.Add(p1); document.Add(p2); document.Add(p3);//创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块:
Chunk chunk = new Chunk("Hello world", FontFactory.GetFont(FontFactory.COURIER, 20, iTextSharp.text.Font.COURIER, new iTextSharp.text.Color(255, 0, 0)));
document.Add(chunk);
//如果你希望一些块有下划线或删除线,你可以通过改变字体风格简单做到:
Chunk chunk1 = new Chunk("This text is underlined", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED));
Chunk chunk2 = new Chunk("This font is of type ITALIC | STRIKETHRU", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.ITALIC | iTextSharp.text.Font.STRIKETHRU));
//改变块的背景
chunk2.SetBackground(new iTextSharp.text.Color(0xFF, 0xFF, 0x00));
//上标/下标
chunk1.SetTextRise(5);
document.Add(chunk1);
document.Add(chunk2);//外部链接示例:
Anchor anchor = new Anchor("website", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED, new iTextSharp.text.Color(0, 0, 255)));
anchor.Reference = "http://itextsharp.sourceforge.net/";
anchor.Name = "website";
//内部链接示例:
Anchor anchor1 = new Anchor("This is an internal link\n\n");
anchor1.Name = "link1";
Anchor anchor2 = new Anchor("Click here to jump to the internal link\n\f");
anchor2.Reference = "#link1";
document.Add(anchor);document.Add(anchor1);document.Add(anchor2);//排序列表示例:
List list = new List(true, 20);
list.Add(new ListItem("First line"));
list.Add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));
list.Add(new ListItem("Third line"));
document.Add(list);//文本注释:
Annotation a = new Annotation("authors","Maybe its because I wanted to be an author myself that I wrote iText.");
document.Add(a);//包含页码没有任何边框的页脚。
HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);
footer.Border = iTextSharp.text.Rectangle.NO_BORDER;
document.Footer = footer;
//Chapter对象和Section对象自动构建一个树:
iTextSharp.text.Font f1 = new iTextSharp.text.Font();
f1.SetStyle(iTextSharp.text.Font.BOLD);
Paragraph cTitle = new Paragraph("This is chapter 1", f1);
Chapter chapter = new Chapter(cTitle, 1);
Paragraph sTitle = new Paragraph("This is section 1 in chapter 1", f1);
Section section = chapter.AddSection(sTitle, 1);
document.Add(chapter);
//构建了一个简单的表:
Table aTable = new Table(4, 4);
aTable.AutoFillEmptyCells = true;
aTable.AddCell("2.2", new Point(2, 2));
aTable.AddCell("3.3", new Point(3, 3));
aTable.AddCell("2.1", new Point(2, 1));
aTable.AddCell("1.3", new Point(1, 3));
document.Add(aTable);
//构建了一个不简单的表:
Table table = new Table(3);
table.BorderWidth = 1;
table.BorderColor = new iTextSharp.text.Color(0, 0, 255);
table.Cellpadding = 5;
table.Cellspacing = 5;
Cell cell = new Cell("header");
cell.Header = true;
cell.Colspan = 3;
table.AddCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.Rowspan = 2;
cell.BorderColor = new iTextSharp.text.Color(255, 0, 0);
table.AddCell(cell);
table.AddCell("1.1");
table.AddCell("2.1");
table.AddCell("1.2");
table.AddCell("2.2");
table.AddCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);
table.AddCell(cell);
table.AddCell("cell test2");
// 改变了单元格“big cell”的对齐方式:
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
document.Add(table);//关闭Document
document.Close();
}转载自: http://hi.baidu.com/he_king/blog/item/18ec10eedb3b662c2df53486.html
itextsharp.dll 下载:http://sourceforge.net/projects/itextsharp/
最近看到别人代码中一个很好的功能类,该类是一个Win32 API实例类,该类功能包括:同一程序禁止启动多次;获取任意窗体;恢复窗体状态;设置窗体焦点等。
该类很实用,与大家分享一下:
1 /// Summary description for ProcessUtils.
2 public static class ProcessUtils
3 {
4 private static Mutex mutex = null;
5
6 /// Determine if the current process is already running
7 public static bool ThisProcessIsAlreadyRunning()
8 {
9 // Only want to call this method once, at startup.
10 Debug.Assert(mutex == null);
11
12 // createdNew needs to be false in .Net 2.0, otherwise, if another instance of
13 // this program is running, the Mutex constructor will block, and then throw
14 // an exception if the other instance is shut down.
15 bool createdNew = false;
16
17 mutex = new Mutex(false, Application.ProductName, out createdNew);
18
19 Debug.Assert(mutex != null);
20
21 return !createdNew;
22 }
23
24 [DllImport("user32.dll", SetLastError = true)]
25 static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
26
27 [DllImport("user32.dll")]
28 [return: MarshalAs(UnmanagedType.Bool)]
29 static extern bool SetForegroundWindow(IntPtr hWnd);
30
31 [DllImport("user32.dll")]
32 static extern bool IsIconic(IntPtr hWnd);
33
34 [DllImport("user32.dll")]
35 static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
36
37 const int SW_RESTORE = 9;
38
39 [DllImport("user32.dll")]
40 static extern IntPtr GetLastActivePopup(IntPtr hWnd);
41
42 [DllImport("user32.dll")]
43 static extern bool IsWindowEnabled(IntPtr hWnd);
44
45 /// Set focus to the previous instance of the specified program.
46 public static void SetFocusToPreviousInstance(string windowCaption)
47 {
48 // Look for previous instance of this program.
49 IntPtr hWnd = FindWindow(null, windowCaption);
50
51 // If a previous instance of this program was found...
52 if (hWnd != null)
53 {
54 // Is it displaying a popup window?
55 IntPtr hPopupWnd = GetLastActivePopup(hWnd);
56
57 // If so, set focus to the popup window. Otherwise set focus
58 // to the program's main window.
59 if (hPopupWnd != null && IsWindowEnabled(hPopupWnd))
60 {
61 hWnd = hPopupWnd;
62 }
63
64 SetForegroundWindow(hWnd);
65
66 // If program is minimized, restore it.
67 if (IsIconic(hWnd))
68 {
69 ShowWindow(hWnd, SW_RESTORE);
70 }
71 }
72 }
73 } [关键字: asp.net2]
PDF简介:PDF(Portable Document Format)文件格式是Adobe公司开发的电子文件格式。这种文件格式与操作系统平台无关,也就是说,PDF文件不管是在Windows,Unix还是在苹果公司的Mac OS操作系统中都是通用的。这一特点使它成为在Internet上进行电子文档发行和数字化信息传播的理想文档格式。越来越多的电子图书、产品说明、公司文告、网络资料、电子邮件开始使用PDF格式文件。PDF格式文件目前已成为数字化信息事实上的一个工业标准。
Adobe公司设计PDF文件格式的目的是为了支持跨平台上的,多媒体集成的信息出版和发布,尤其是提供对网络信息发布的支持。为了达到此目的, PDF具有许多其他电子文档格式无法相比的优点。PDF文件格式可以将文字、字型、格式、颜色及独立于设备和分辨率的图形图像等封装在一个文件中。该格式文件还可以包含超文本链接、声音和动态影像等电子信息,支持特长文件,集成度和安全可靠性都较高。
日常工作中经常遇到想把报表和网页导出到PDF的需求。本文提供完美的解决方案:
ASP.NET导出到PDF的最终效果图(其实winform和控制台程序都一样可以做)。
本文实现 文字,图片,数据表的导出
核心技术方案:使用itextsharp.dll
1.下载itextsharp.dll和ICSharpCode.SharpZipLib.dll
http://sourceforge.net/project/showfiles.php?group_id=72954iTextSharp.tutorial.01.zip 示例文件 提供了各种解决方案本文由于时间问题仅做抛砖引玉,希望大家自己研究其他需求
itextsharp.dll itextsharp-4.0.3-dll.zip
ICSharpCode.SharpZipLib.dll http://download.csdn.net/down/135897 ICSharpCode.SharpZipLib.dll
SharpZipLib.dll类库中的内容实现的压缩与解压功能,它是开源的
2.引用itextsharp.dll和ICSharpCode.SharpZipLib.dll
3.后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
static DataTable datatable = new DataTable("testpdf");
protected void Page_Load(object sender, EventArgs e)
{
//判断是否是回发页面http://blog.csdn.net/21aspnet
if (!Page.IsPostBack)
{
DataRow dr;
//建立Column例,可以指明例的类型,这里用的是默认的string
datatable.Columns.Add(new DataColumn("编号"));
datatable.Columns.Add(new DataColumn("用户名"));
for (int i = 1; i < 5; i++)
{
dr = datatable.NewRow();
dr[0] = System.Convert.ToString(i);
dr[1] = "清清月儿" + System.Convert.ToString(i);
datatable.Rows.Add(dr);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
Document document = new Document();
PdfWriter.getInstance(document, new FileStream(Server.MapPath("Chap0101.pdf"), FileMode.Create));
document.Open();
BaseFont bfChinese = BaseFont.createFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,new Color(0, 0, 0));
document.Add(new Paragraph(this.TextBox1.Text.ToString(), fontChinese));
iTextSharp.text.Image jpeg = iTextSharp.text.Image.getInstance(Server.MapPath("pic015.jpg"));
document.Add(jpeg);
PdfPTable table = new PdfPTable(datatable.Columns.Count);
for (int i = 0; i < datatable.Rows.Count; i++)
{
for (int j = 0; j < datatable.Columns.Count; j++)
{
table.addCell(new Phrase(datatable.Rows[i][j].ToString(), fontChinese));
}
}
document.Add(table);
document.Close();
}
catch (DocumentException de)
{;
Response.Write(de.ToString());
}
}
}
4.前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>清清月儿 制作导出PDF http://blog.csdn.net/21aspnet</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" /></div>
</form>
</body>
</html>5.前台操作:
6.属性说明:
itextsharp-4.0.3-dll.zip 示例文件包含几乎所有的PDF处理需求
颜色:
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,new Color(0, 0, 0)); //黑Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,new Color(0, 255, 0)); //绿注释:
iText支持不同风格的注释。
u 文本注释:
你可以添加一小段文本到你的文档中,但它并非文档内容的一部分,注释有标题和内容:
Annotation a = new Annotation(
"authors",
"Maybe it's because I wanted to be an author myself that I wrote iText.");对齐方式:
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
下划线/删除线:
Chunk chunk1 = new Chunk("This text is underlined", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE));
Chunk chunk2 = new Chunk("This font is of type ITALIC | STRIKETHRU", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC | Font.STRIKETHRU));
加密:
public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions);由于时间问题:更多如页眉页脚属性目录水印单元格间距边框等等请大家自己研究文档。
转载: http://www.sunxin.org/forum/thread/16253.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 版权信息