iTextSharp
选择字号: 超大 标准 发布时间:2010-8-1 15:0:45 | 作者:admin | 0个评论 | 人浏览
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.rtf;
namespace PdfTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Document document = new Document();
try
{
// creation of the different writers
PdfWriter.getInstance(document, new FileStream("Chap0512.pdf", FileMode.Create));
BaseFont bfSun = BaseFont.createFont(@"C:\WINDOWS\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(bfSun, 9, iTextSharp.text.Font.BOLD);
// we add some meta information to the document
document.addAuthor("哈尔滨11软件");
document.addSubject("22广告系统统计分析报表");
HeaderFooter footer = new HeaderFooter(new Phrase("当前页: ", font), true);
footer.Border = Rectangle.NO_BORDER;
document.Footer = footer;
HeaderFooter header = new HeaderFooter(new Phrase("22平台统计报表", font), false);
header.Border = Rectangle.NO_BORDER;
header.Alignment = HeaderFooter.RIGHT;
document.Header = header;
document.Open();
Table datatable = new Table(5);
// datatable.CellsFitPage = true;
datatable.AutoFillEmptyCells = true;
datatable.CellsFitPage = true;
datatable.Padding = 4;
datatable.Spacing = 0;
//datatable.setBorder(Rectangle.NO_BORDER);
float[] headerwidths = { 10,10,10,40,30 };
datatable.Widths = headerwidths;
datatable.WidthPercentage = 100;
// the first cell spans 10 columns
Cell cell = new Cell(new Phrase("22广告系统统计分析报表1", font));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Leading = 15;
cell.Colspan = 5;
cell.Border = Rectangle.NO_BORDER;
cell.BackgroundColor = new Color(0xEE, 0xEE, 0xEE);
datatable.addCell(cell);
//datatable.BorderColor = new Color(0xFF,0x00,0x00);
datatable.BorderWidth = 1;
// These cells span 2 rows
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultHorizontalAlignment = 1;
datatable.DefaultRowspan = 1;
datatable.addCell(new Phrase("序号", font));
datatable.addCell(new Phrase("用户名称", font));
datatable.addCell(new Phrase("广告地址", font));
datatable.addCell(new Phrase("广告详细", font));
datatable.addCell(new Phrase("其他", font));
// This cell spans the remaining 6 columns in 1 row
//datatable.DefaultRowspan = 1;
//datatable.DefaultColspan = 6;
//datatable.addCell("Permissions");
//// These cells span 1 row and 1 column
//datatable.DefaultColspan = 1;
//datatable.addCell("Admin");
//datatable.addCell("Data");
//datatable.addCell("Expl");
//datatable.addCell("Prod");
//datatable.addCell("Proj");
//datatable.addCell("Online");
// this is the end of the table header
datatable.endHeaders();
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultRowspan = 1;
for (int i = 1; i <300; i++)
{
datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
datatable.addCell(new Phrase(i.ToString(), font));
datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
datatable.addCell(new Phrase("感知北国", font));
if (i % 2 == 0)
{
datatable.addCell(new Phrase("游客", font));
}
else
{
datatable.addCell(new Phrase("User"+i.ToString(), font));
}
datatable.addCell(new Phrase("http://www.gzbg100.cn/adv/info.aspx?id=123", font));
datatable.addCell(new Phrase("无", font));
//datatable.addCell("No");
//datatable.addCell("Yes");
//datatable.addCell("No");
//datatable.addCell("Yes");
//datatable.addCell("No");
//datatable.addCell("Yes");
}
document.Add(datatable);
}
catch (Exception es)
{
Console.Error.WriteLine(es.StackTrace);
}
// we close the document
document.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
RtfWriter.getInstance(document, new FileStream("Chap0804.rtf", FileMode.Create));
BaseFont bfSun = BaseFont.createFont(@"C:\WINDOWS\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(bfSun, 9, iTextSharp.text.Font.BOLD);
// we add some meta information to the document
document.addAuthor("哈尔滨11软件");
document.addSubject("22广告系统统计分析报表");
HeaderFooter footer = new HeaderFooter(new Phrase("当前页: ", font), true);
footer.Border = Rectangle.RIGHT;
document.Footer = footer;
HeaderFooter header = new HeaderFooter(new Phrase("22平台统计报表", font), false);
header.Border = Rectangle.TOP;
header.Alignment = Element.ALIGN_CENTER;
document.Header = header;
document.Open();
Table datatable = new Table(5);
// datatable.CellsFitPage = true;
datatable.AutoFillEmptyCells = true;
datatable.CellsFitPage = true;
datatable.Padding = 4;
datatable.Spacing = 0;
//datatable.setBorder(Rectangle.NO_BORDER);
float[] headerwidths = { 10, 10, 10, 40, 30 };
datatable.Widths = headerwidths;
datatable.WidthPercentage = 100;
// the first cell spans 10 columns
Cell cell = new Cell(new Phrase("22广告系统统计分析报表1", font));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Leading = 15;
cell.Colspan = 5;
cell.Border = Rectangle.NO_BORDER;
cell.BackgroundColor = new Color(0xEE, 0xEE, 0xEE);
datatable.addCell(cell);
//datatable.BorderColor = new Color(0xFF,0x00,0x00);
datatable.BorderWidth = 1;
// These cells span 2 rows
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultHorizontalAlignment = 1;
datatable.DefaultRowspan = 1;
datatable.addCell(new Phrase("序号", font));
datatable.addCell(new Phrase("用户名称", font));
datatable.addCell(new Phrase("广告地址", font));
datatable.addCell(new Phrase("广告详细", font));
datatable.addCell(new Phrase("其他", font));
// This cell spans the remaining 6 columns in 1 row
//datatable.DefaultRowspan = 1;
//datatable.DefaultColspan = 6;
//datatable.addCell("Permissions");
//// These cells span 1 row and 1 column
//datatable.DefaultColspan = 1;
//datatable.addCell("Admin");
//datatable.addCell("Data");
//datatable.addCell("Expl");
//datatable.addCell("Prod");
//datatable.addCell("Proj");
//datatable.addCell("Online");
// this is the end of the table header
datatable.endHeaders();
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultRowspan = 1;
for (int i = 1; i < 1000; i++)
{
datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
datatable.addCell(new Phrase(i.ToString(), font));
datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
datatable.addCell(new Phrase("感知北国", font));
if (i % 2 == 0)
{
datatable.addCell(new Phrase("游客", font));
}
else
{
datatable.addCell(new Phrase("User" + i.ToString(), font));
}
datatable.addCell(new Phrase("http://www.gzbg100.cn/adv/info.aspx?id=123", font));
datatable.addCell(new Phrase("无", font));
//datatable.addCell("No");
//datatable.addCell("Yes");
//datatable.addCell("No");
//datatable.addCell("Yes");
//datatable.addCell("No");
//datatable.addCell("Yes");
}
document.Add(datatable);
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
// step 5: we close the document
document.Close();
}
}
}
标签: