private void button1_Click(object sender, EventArgs e) //为当前Document添加内容: //创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块: //外部链接示例: //排序列表示例: //文本注释: //包含页码没有任何边框的页脚。
//关闭Document |
转载自: http://hi.baidu.com/he_king/blog/item/18ec10eedb3b662c2df53486.html
itextsharp.dll 下载:http://sourceforge.net/projects/itextsharp/
最近看到别人代码中一个很好的功能类,该类是一个Win32 API实例类,该类功能包括:同一程序禁止启动多次;获取任意窗体;恢复窗体状态;设置窗体焦点等。
该类很实用,与大家分享一下:
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 }
PDF简介:PDF(Portable Document Format)文件格式是Adobe公司开发的电子文件格式。这种文件格式与操作系统平台无关,也就是说,PDF文件不管是在Windows,Unix还是在苹果公司的Mac OS操作系统中都是通用的。这一特点使它成为在Internet上进行电子文档发行和数字化信息传播的理想文档格式。越来越多的电子图书、产品说明、公司文告、网络资料、电子邮件开始使用PDF格式文件。PDF格式文件目前已成为数字化信息事实上的一个工业标准。
Adobe公司设计PDF文件格式的目的是为了支持跨平台上的,多媒体集成的信息出版和发布,尤其是提供对网络信息发布的支持。为了达到此目的, PDF具有许多其他电子文档格式无法相比的优点。PDF文件格式可以将文字、字型、格式、颜色及独立于设备和分辨率的图形图像等封装在一个文件中。该格式文件还可以包含超文本链接、声音和动态影像等电子信息,支持特长文件,集成度和安全可靠性都较高。
日常工作中经常遇到想把报表和网页导出到PDF的需求。本文提供完美的解决方案:
ASP.NET导出到PDF的最终效果图(其实winform和控制台程序都一样可以做)。
本文实现 文字,图片,数据表的导出
核心技术方案:使用itextsharp.dll1.下载itextsharp.dll和ICSharpCode.SharpZipLib.dll
http://sourceforge.net/project/showfiles.php?group_id=72954
iTextSharp.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