在.NET中央银行使iTextSharp创造/读取PDF报告: Part I [翻译]

原文地址:Create/Read Advance PDF Report using iTextSharp in C# .NET:
Part
I   
By Debopam
Pal,
27 Nov 2013

一、C# Word操作
引入Word COM组件
菜单=》项目=》添加引用=》COM=》Microsoft Word 11.0 Object Library
using Word =  Microsoft.Office.Interop.Word;
1、功能:将数据以自制表格形式插入WORD中
2、主要程序代码如下:
创建新Word
object oMissing = System.Reflection.Missing.Value;
Word.Application oWord;
Word.Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

 

打开文档:
object oMissing = System.Reflection.Missing.Value;
Word.Application oWord;
Word.Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @”E:CCCXCXXTestDoc.doc”;
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

到PDF原文介绍了iTextSharp这个类库,并演示了一些基本的操作,基本属于入门级别的,可惜作者并没有在编写后续的文章。

导入模板
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @”E:XXXCCXTest.doc”;
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);

恰好自己也在学习这个类库,想实现一个导出ASPX页面到PDF的功能,如作者所说,网上找到的示例好多都是针对旧版本iTextSharp编写的,还有些驴唇不对马嘴,而且,很多照抄的连验证都省了,一点价值都没有。这篇文章算是详实的入门文章,实例也都基本操作了一遍实现没有问题。

.添加新表
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

ps.:第一次翻译老外的文章,英语水平一般,有些地方词不达意,还望海涵,如果出入希望帮忙指出。

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

文章内容

  • 简介
  • 要求
  • 安装步骤
  • 6步创建PDF文档 
  • 关联PDF文档的页面大小 
  • 设置PDF文档背景色
  • 设置PDF文档边距
  • 设置PDF文档文字对齐方式
  • 设置PDF文档的元信息或属性
  • 创建多页文档
  • 从已有文档创建新的PDF文档
  • 使用Layer为PDF文档添加水印
  • 使用Removing
    Layer移除刚刚创建Layer水印的PDF文档
  • 在创建过程为每一页添加水印
  • E在不存盘的情况下,导出/打印/输出PDF文件到客户端
    下面内容是在更新版本后添加的:
  • 设置PDF浏览参数
  • 加密PDF文档
  • 声明
  • 参考文献
  • 历史

.表插入行
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

简介

最近我一直在寻找一个高级的工具来创建复杂的PDF报告用在C#.NET中,我找到了iTextSharp.主要的问题是iTextSharp.缺乏文档。好吧,有少量的C#代码例子,但是那些对初学者来说是不够的并且这些示例代码都是建立在iTextSharp的旧版本上,在最新版本上有很多变化。所以,对初学者来说转换旧版本到新版本比较困难。此外,我认为假如我写一篇关于iTextSharp的文章,它能帮助我同时也能作为日后的参考,我将为每一个功能点书写示例。老实说,在这篇文章,我编写的所有示例,你都能在《iText
in Action, Second
Edition》这本书的第一节找到,这本书是针对Java开发者编写的。我将在我的文章中解释[从java到c#]这本书余下章节的所有的示例.所以,如果有人对这个库(iTextSharp)感兴趣,这里将是一个好的开始。

想知道关于(iTextSharp)的更多细节,可以通过他们的官方网站了解

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

要求

  • 编译这个类库,你需要一个C#2008(vs2008)编译器或者更高版本,Visual
    Studio 2008 or Visual C# 2008 Express Edition
  • 这个库代码运行在:
    • .NET 2.0
    • .NET 3.0
    • .NET 3.5
    • .NET 4.0
    • .NET 4.0 Client Profile
    • .NET 4.5
    • .NET 4.5 Client Profile

Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);

安装

  • 你需要安装 NuGet
    package

或者
你可以从下面这个SourceForge的链接下载DLL,然后参照下面步骤:

  • 添加引用BlahBlah(步骤如下,翻译掠过). Just see the image below:
    威尼斯官方网站登录 1
  • 你需要引入到C#文件中的命名空间:
    • iTextSharp.text
    • iTextSharp.text.pdf

.单元格合并
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

快速入门

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

6步创建一个PDF文档:

  • Step 1: 创建一个 System.IO.FileStream 对象:

    FileStream fs = new FileStream(“Chapter1_Example1.pdf”, FileMode.Create, FileAccess.Write, FileShare.None);

  • Step 2: 创建一个 iTextSharp.text.Document 对象:

    Document doc = new Document();

  • Step 3: 创建一个 iTextSharp.text.pdf.PdfWriter 对象:
    它有助于把Document书写到特定的FileStream:

    PdfWriter writer = PdfWriter.GetInstance(doc, fs);

  • Step 4: 打开 Document:

    doc.Open();

  • Step 5: 创建一个 iTextSharp.text.Paragraph 对象并添加到Document里:

    doc.Add(new Paragraph(“Hello World”));

  • Step 6: 关闭 Document:

    doc.Close();

Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);

关联PDF文档的页面大小:

创建一个特定大小的页面,我们需要创建一个iTextSharp.text.Rectangle
对象同时传递一个页面大小的参数到它的构造函数里面,下面是定义页面大小的方法:

  • 第一种定义一个版面大小的方式:
    通过定义像素或者英寸定义一个页面尺寸。注意:在iTextSharp里面页面大小的单位是‘point。72point=1英寸。假设我们需要一个宽度=2英寸&高度=10英寸的PDF文件,那么我们需要144pt&72pt,让我们看下该怎么做:

Rectangle rec = new Rectangle(144, 720);
  • 第二种定义版面大小的方式:
    使用内建的 iTextSharp.text.PageSize 类定义:

    Rectangle rec2 = new Rectangle(PageSize.A4);
    

    下面是内建的版面大小。. 完整的页面大小说明链接 Documentation of
    Page
    Size:

    • _11X17
    • A0
    • A1
    • A10
    • A2
    • A3
    • A4
    • A4_LANDSCAPE
    • A5
    • A6
    • A7
    • A8
    • A9
    • ARCH_A
    • ARCH_B
    • ARCH_C
    • ARCH_D
    • ARCH_E
    • B0
    • B1
    • B10
    • B2
    • B3
    • B4
    • B5
    • B6
    • B7
    • B8
    • B9
    • CROWN_OCTAVO
    • CROWN_QUARTO
    • DEMY_OCTAVO
    • DEMY_QUARTO
    • EXECUTIVE
    • FLSA
    • FLSE
    • HALFLETTER
    • ID_1
    • ID_2
    • ID_3
    • LARGE_CROWN_OCTAVO
    • LARGE_CROWN_QUARTO
    • LEDGER
    • LEGAL
    • LEGAL_LANDSCAPE
    • LETTER
    • LETTER_LANDSCAPE
    • NOTE
    • PENGUIN_LARGE_PAPERBACK
    • PENGUIN_SMALL_PAPERBACK
    • POSTCARD
    • ROYAL_OCTAVO
    • ROYAL_QUARTO
    • SMALL_PAPERBACK
    • TABLOID
  • 第三种定义版面大小的方式:
    反转文档的高度变成宽度&反之亦然:

    Rectangle rec3 = new Rectangle(PageSize.A4.Rotate());
    

现在,把刚刚的这个 iTextSharp.text.Rectangle 对象
(任意一个)也就是上面的 ‘rec’,或者 ‘rec2’或者
‘rec3’加入iTextSharp.text.Document‘s 的构造函数中:

Document doc = new Document(rec);

Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));

设置PDF文档背景色:

有几种方式来设置背景色:

  • 第一种方法:
    需要使用对象 iTextSharp.text.BaseColor. 实例化BaseColor
    采用System.Drawing.Color
    (.NET)对象或者你也可以用传递RGB值的形式来定义:

    rec.BackgroundColor = new BaseColor(System.Drawing.Color.WhiteSmoke);
    
  • 第二种方法:
    需要使用对象 iTextSharp.text.pdf.CMYKColor. CMYKColor 通过传递
    CMYK 值的方式来构造:

    rec2.BackgroundColor = new CMYKColor(25, 90, 25, 0);
    

.单元格分离
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add( oMissing,
ref oMissing, ref oMissing);

设置PDF文档边距:

页边距可以像设置版面大小一样来定义
加入我们设置如下的页边距:

  • Left : 0.5 inch
  • Right : 1 inch
  • Top : 1.5 inch
  • Bottom : 2.5 inch

所以我们需要分别设置页面的 Left, Right, Top, Bottom
页边距使用point单位,因为我们知道 iTextSharp
中是使用point作为单位的,并且 72 points = 1 inch.

  • Left : 36pt => 0.5 inch
  • Right : 72pt => 1 inch
  • Top : 108pt => 1.5 inch
  • Bottom : 180pt => 2.5 inch

实现如下:

Document doc = new Document(PageSize.A4, 36, 72, 108, 180);

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

设置PDF文档文字对齐方式:

AlignmentiTextSharp.text.Paragraph对象的属性. iTextSharp
提供了各种对齐方式. 可以通过iTextSharp.text.Element
类设置对其.以下是iTextSharp提供的对齐方式:

  • ALIGN_BASELINE[^]
  • ALIGN_BOTTOM[^]
  • ALIGN_CENTER[^]
  • ALIGN_JUSTIFIED[^]
  • ALIGN_JUSTIFIED_ALL[^]
  • ALIGN_LEFT[^]
  • ALIGN_MIDDLE[^]
  • ALIGN_RIGHT[^]
  • ALIGN_TOP[^]
  • ALIGN_UNDEFINED[^]

我们已经知道在 iTextSharp.text.Document
的构造函数中需要iTextSharp.text.Paragraph
对象,所以在创建Paragraph对象以后我们可以设置它的对齐方式,我们可以在Document创建过程把Prargraph传递进去.

实现如下:

Paragraph para = new Paragraph("Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World");
// Setting paragraph's text alignment using iTextSharp.text.Element class
para.Alignment = Element.ALIGN_JUSTIFIED;
// Adding this 'para' to the Document object
doc.Add(para);

Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);

设置PDF文档的元信息或属性:

下面这些PDF文档的元信息
你可以通过iTextSharp.text.Document创建的对象doc(上文的doc)里面的方法来设置:

  • Author
    Name[^)]
  • Creation
    Date[^)]
  • Creator
    Name[^)]
  • Header Name &
    Content[^)]
  • Keywords[^)]
  • Langugae[^)]
  • Producer[^)]
  • Subject[^)]
  • Title[^)]

下面是他们的一些实现:

// Setting Document properties e.g.
// 1. Title
// 2. Subject
// 3. Keywords
// 4. Creator
// 5. Author
// 6. Header
doc.AddTitle("Hello World example");
doc.AddSubject("This is an Example 4 of Chapter 1 of Book 'iText in Action'");
doc.AddKeywords("Metadata, iTextSharp 5.4.4, Chapter 1, Tutorial");
doc.AddCreator("iTextSharp 5.4.4");
doc.AddAuthor("Debopam Pal");
doc.AddHeader("Nothing", "No Header");

现在,打开一个PDF文档后,右键->属性,你会看到刚才设置的信息:
威尼斯官方网站登录 2

Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));

创建多页文档:

我们可以通过iTextSharp.text.DocumentNewPage()方法创建新页面,我们来创建5个PDF文档(页面)
:

for (int i = 1; i <= 5; i++)
{
    doc.NewPage();
    doc.Add(new Paragraph(string.Format("This is a page {0}", i)));
}

object Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);

从已有文档创建新的PDF文档:

我们可以使用iTextSharp.text.pdf.PdfReader对象读取一个PDF文档,然后使用
iTextSharp.text.pdf.PdfStamper对象来把它写到另一个PDF文档。实现如下:

string originalFile = "Original.pdf";
string copyOfOriginal = "Copy.pdf";
using (FileStream fs = new FileStream(originalFile, FileMode.Create, FileAccess.Write, FileShare.None))
using (Document doc = new Document(PageSize.LETTER))
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
{
    doc.Open();
    doc.Add(new Paragraph("Hi! I'm Original"));
    doc.Close();
}
PdfReader reader = new PdfReader(originalFile);
using (FileStream fs = new FileStream(copyOfOriginal, FileMode.Create, FileAccess.Write, FileShare.None))
// Creating iTextSharp.text.pdf.PdfStamper object to write
// Data from iTextSharp.text.pdf.PdfReader object to FileStream object
using (PdfStamper stamper = new PdfStamper(reader, fs)) { }

通过段落控制插入
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = “endofdoc”; /**//* endofdoc is a
predefinedbookmark */

使用Layer为PDF文档添加水印:

iTextSharp中,PDF文档创建后可以添加水印,在这里我将使用iTextSharp.text.pdf.PdfLayer为已有的PDF文档(Original.pdf)添加水印。实现如下:

string watermarkedFile = "Watermarked.pdf";
// Creating watermark on a separate layer
// Creating iTextSharp.text.pdf.PdfReader object to read the Existing PDF Document
PdfReader reader1 = new PdfReader(originalFile);
using (FileStream fs = new FileStream(watermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None))
// Creating iTextSharp.text.pdf.PdfStamper object to write Data from iTextSharp.text.pdf.PdfReader object to FileStream object
using (PdfStamper stamper = new PdfStamper(reader1, fs))
{
    // Getting total number of pages of the Existing Document
    int pageCount = reader1.NumberOfPages;

    // Create New Layer for Watermark
    PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
    // Loop through each Page
    for (int i = 1; i <= pageCount; i++)
    {
        // Getting the Page Size
        Rectangle rect = reader1.GetPageSize(i);

        // Get the ContentByte object
        PdfContentByte cb = stamper.GetUnderContent(i);

        // Tell the cb that the next commands should be "bound" to this new layer
        cb.BeginLayer(layer);
        cb.SetFontAndSize(BaseFont.CreateFont(
          BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);

        PdfGState gState = new PdfGState();
        gState.FillOpacity = 0.25f;
        cb.SetGState(gState);

        cb.SetColorFill(BaseColor.BLACK);
        cb.BeginText();
        cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, 45f);
        cb.EndText();

        // Close the layer
        cb.EndLayer();
    }
}

实现结果如下:
威尼斯官方网站登录 3

//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

使用Removing Layer移除刚刚创建Layer水印的PDF文档:

Whenever we add Layer in PDF Document, then the content of the Layer
resides under OCG Group. So if I remove this Layer we can remove the
content of the Layer also e.g. here it is Watermark Text. To remove all
the Layers from PDF Document, you have to remove OCG Group completely
from the Document usingreader.Catalog.Remove(PdfName.OCPROPERTIES).
Now follow the Steps below to remove the Watermark Text from Layer:

  • Read the existing watermarked document using
    iTextSharp.text.pdf.PdfReader‘s object
  • Taking each Page in the iTextSharp.text.pdf.PdfDictionary‘s object
    using GetPageN(int pageNumber) method of
    iTextSharp.text.pdf.PdfReader‘s object.
  • Taking the Content of the Page in the
    iTextSharp.text.pdf.PdfArray‘s object
    usingGetAsArray(PdfName.CONTENTS) method of
    iTextSharp.text.pdf.PdfDictionary‘s object
  • Loop through this array and Get each element as
    iTextSharp.text.pdf.PRStream‘s object
    usingGetAsStream(int arrayIndex) method of
    iTextSharp.text.pdf.PdfArray‘s object
  • Convert each stream into Bytes using Static method
    GetStreamBytes(PRStream stream) ofiTextSharp.text.pdf.PdfReader
    class
  • Convert these Bytes into String using
    System.Text.Encoding.ASCII.GetString(byte[] bytes)method
  • Search for the String “/OC” and also the Watermarked Text. If
    found then remove it by giving it zero length and zero data using
    two methods: Put() & SetData() of iTextSharp.text.pdf.PRStream
    class
  • Write this modified document exists in the reader to a new
    document usingiTextSharp.text.pdf.PdfStamper‘s object

Lets Implement it:

// Removing the layer created above
// 1. First we bind a reader to the watermarked file
// 2. Then strip out a branch of things
// 3. Finally use a simple stamper to write out the edited reader
PdfReader reader2 = new PdfReader(watermarkedFile);

// NOTE: This will destroy all layers in the Document, only use if you don't have any addtional layers
// Remove the OCG group completely from the Document: reader2.Catalog.Remove(PdfName.OCPROPERTIES);

// Clean up the reader, optional
reader2.RemoveUnusedObjects();

// Placeholder variables
PRStream stream;
string content;
PdfDictionary page;
PdfArray contentArray;

// Get the number of pages
int pageCount2 = reader2.NumberOfPages;

// Loop through each page
for (int i = 1; i <= pageCount2; i++)
{
    // Get the page
    page = reader2.GetPageN(i);

    // Get the raw content
    contentArray = page.GetAsArray(PdfName.CONTENTS);

    if (contentArray != null)
    {
        // Loop through content
        for (int j = 0; j < contentArray.Size; j++)
        {
            stream = (PRStream)contentArray.GetAsStream(j);

            // Convert to a String, NOTE: you might need a different encoding here
            content = System.Text.Encoding.ASCII.GetString(PdfReader.GetStreamBytes(stream));

            //Look for the OCG token in the stream as well as our watermarked text
            if (content.IndexOf("/OC") >= 0 && content.IndexOf(watermarkText) >= 0)
            {
                //Remove it by giving it zero length and zero data
                stream.Put(PdfName.LENGTH, new PdfNumber(0));
                stream.SetData(new byte[0]);
            }
        }
    }
}

// Write the content out
using (FileStream fs = new FileStream(unwatermarkedFile, 
          FileMode.Create, FileAccess.Write, FileShare.None))
using (PdfStamper stamper = new PdfStamper(reader2, fs)) { }

//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = “Heading 1”;
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 ptspacing after paragraph.
oPara1.Range.InsertParagraphAfter();

在创建过程为每一页添加水印:

Now, we already know that, watermark cannot be add during Page creation,
it have to add after document creation. So, I’ve created a class
PDFWriterEvents which implements the interface
iTextSharp.text.pdf.IPdfPageEventand modify the event OnStartPage.
This interface contains a set of events from the Openning & to Closing
the PDF Document. The events are following:

  • public void OnOpenDocument(PdfWriter writer, Document document)
  • public void OnCloseDocument(PdfWriter writer, Document document)
  • public void OnStartPage(PdfWriter writer, Document document)
  • public void OnEndPage(PdfWriter writer, Document document)
  • public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition)
  • public void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition)
  • public void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
  • public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition)
  • public void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title)
  • public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition)
  • public void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text)

You may modify other events accroding to your needs which occured
against a particular action. Let see the which I’ve created:

// Creating Watermark inside OnStartPage Event by implementing IPdfPageEvent interface
// So that, dusring Page Creation, Watermark will be create
class PDFWriterEvents : IPdfPageEvent
{
    string watermarkText;
    float fontSize = 80f;
    float xPosition = 300f;
    float yPosition = 800f;
    float angle = 45f;

    public PDFWriterEvents(string watermarkText, float fontSize = 80f, 
       float xPosition = 300f, float yPosition = 400f, float angle = 45f)
    {
        this.watermarkText = watermarkText;
        this.xPosition = xPosition;
        this.yPosition = yPosition;
        this.angle = angle;
    }

    public void OnOpenDocument(PdfWriter writer, Document document) { }
    public void OnCloseDocument(PdfWriter writer, Document document) { }
    public void OnStartPage(PdfWriter writer, Document document)
    {
        try
        {
            PdfContentByte cb = writer.DirectContentUnder;
            BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
            cb.BeginText();
            cb.SetColorFill(BaseColor.LIGHT_GRAY);
            cb.SetFontAndSize(baseFont, fontSize);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
            cb.EndText();
        }
        catch (DocumentException docEx)
        {
            throw docEx;
        }
    }
    public void OnEndPage(PdfWriter writer, Document document) { }
    public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition) { }
    public void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) { }
    public void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) { }
    public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition) { }
    public void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) { }
    public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition) { }
    public void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) { }
}

Lets see how & when you use the object of this class:

using (FileStream fs = new FileStream(
      "Watermark_During_Page_Creation.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
using (Document doc = new Document(PageSize.LETTER))
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
{
    writer.PageEvent = new PDFWriterEvents("This is a Test");
    doc.Open();
    doc.Add(new Paragraph("This is a page 1"));
    doc.Close();
}

See, OnStartPage event called during adding a new paragraph. So I
don’t need to add watermark
later威尼斯官方网站登录 4

 

在不存盘的情况下,导出/打印/输出PDF文件到客户端:

We can create PDF File in memory by creatig System.IO.MemorySystem‘s
object. Lets see:

using (MemoryStream ms = new MemoryStream())
using(Document document = new Document(PageSize.A4, 25, 25, 30, 30))
using(PdfWriter writer = PdfWriter.GetInstance(document, ms))
{
    document.Open();
    document.Add(new Paragraph("Hello World"));
    document.Close();
    writer.Close();
    ms.Close();
    Response.ContentType = "pdf/application";
    Response.AddHeader("content-disposition", "attachment;filename=First_PDF_document.pdf");
    Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
}

C#word类库:

设置PDF浏览参数:

The values of the different ViewerPreferences were originally stored
in iTextSharp.text.pdf.PdfWriter class as an integer constant. You can
set the ViewerPreferences by following two ways:

  • By setting property ViewerPreferences of
    iTextSharp.text.pdf.PdfWriter class. To know all
    theViewerPreferences and its purpose, please read
    this)
    first. E.g.-

    writer.ViewerPreferences = PdfWriter.HideMenubar;
    
  • 威尼斯官方网站登录,By calling method
    AddViewerPreference(PdfName key, PdfObject value)
    ofiTextSharp.text.pdf.PdfWriter‘s object. To know which value is
    appropiate for which key, read
    this)first.
    E.g.-

    writer.AddViewerPreference(PdfName.HIDEMENUBAR, new PdfBoolean(true));
    

加密PDF文档:

By SetEncryption() method of iTextSharp.text.pdf.PdfWriter‘s object,
we can encrypt a PDF document. Read full documentation of this method
here.
To know all the encryption types, click
here.
E.g.-

writer.SetEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.ALLOW_COPY);

 

声明

Please download the source code for detail. I hope you'll understand as the source code is documented. If any doubt, just post your comment below. Thank you.

二、C# Excel操作

参考文献

  • iText Official Website
  • iText in Action, Second
    Edition
  • Core iText API Documentation
  • RubyPdf Blog – iText# in
    Action
  • Create PDFs in ASP.NET – getting started with
    iTextSharp
  • Basic PDF Creation Using iTextSharp – Part
    I

引入Excel COM组件
菜单=》项目=》添加引用=》COM=》Microsoft Excel 11.0 Object Library
using Excel = Microsoft.Office.Interop.Excel;

历史

25th Nov, 2013: PART-I Release. PART-II will release soon
威尼斯官方网站登录 5

 

许可

This article, along with any associated source code and files, is
licensed under The Code Project Open License
(CPOL)

 

/// <summary>
/// 执行完Excel操作方法后执行资源回收操作,必须与Excel操作的方法分开
/// </summary>
private void ExcelWrite()
{
try
{
ExcelOperation();
}
finally
{
System.GC.Collect();
}
}

/// <summary>
/// Excel操作,只是例子
/// </summary>
private void ExcelOperation()
{
//
//定义一个缺少的object对象
object oMis = System.Reflection.Missing.Value;

//
//定义一个Excel程序对象
Excel.ApplicationClass excelApp =
newMicrosoft.Office.Interop.Excel.ApplicationClass();

//
//由Execl程序创建一个工作薄对象
Excel.Workbook excelWorkBook
=excelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

//
//由工作薄对象创建一个工作表
Excel.Worksheet excelWorkSheet
=(Excel.Worksheet)excelWorkBook.Worksheets.Add(oMis, oMis, 1, oMis);

//
//设置工作的表的名字
excelWorkSheet.Name = “测试项目”;

//
//定义一个Excel区域对象,用于保存选择的区域
Excel.Range selectRange;

//
//设置第1行第1列的值
((Excel.Range)excelWorkSheet.Cells[1, 1]).set_Item(1, 1, “日期”);

//
//设置第1行第2列的值
((Excel.Range)excelWorkSheet.Cells[1, 1]).set_Item(1, 2, “时间”);

//
//选择第2行第2列至第2行第2列,设置值与格式
selectRange = excelWorkSheet.get_Range(excelWorkSheet.Cells[2,
2],excelWorkSheet.Cells[2, 2]);
selectRange.set_Item(1, 1, “11:30”);
selectRange.NumberFormatLocal = @”h:mm;@”;

//
//选择第3行第2列至第2行第2列,设置值与格式
selectRange = excelWorkSheet.get_Range(excelWorkSheet.Cells[3,
2],excelWorkSheet.Cells[3, 2]);
selectRange.set_Item(1, 1, “12:30”);
selectRange.NumberFormatLocal = @”h:mm;@”;

//
//选择第4行第2列至第2行第2列,设置值与格式
selectRange = excelWorkSheet.get_Range(excelWorkSheet.Cells[4,
2],excelWorkSheet.Cells[4, 2]);
selectRange.set_Item(1, 1, “13:30”);
selectRange.NumberFormatLocal = @”h:mm;@”;

//
//选择第5行第2列至第2行第2列,设置值与格式
selectRange = excelWorkSheet.get_Range(excelWorkSheet.Cells[5,
2],excelWorkSheet.Cells[5, 2]);
selectRange.set_Item(1, 1, “14:30”);
selectRange.NumberFormatLocal = @”h:mm;@”;

//
//选择第6行第2列至第2行第2列,设置值与格式
selectRange = excelWorkSheet.get_Range(excelWorkSheet.Cells[6, 2],
excelWorkSheet.Cells[6,2]);
selectRange.set_Item(1, 1, “15:30”);
selectRange.NumberFormatLocal = @”h:mm;@”;

//
//选择第2行第1列至第2行第1列,
selectRange = excelWorkSheet.get_Range(excelWorkSheet.Cells[2,
1],excelWorkSheet.Cells[2, 1]);
//
//选择是必须的,因设置了选择区域后并未选中此区域
selectRange.Select();

//
//冻结窗体
excelApp.ActiveWindow.FreezePanes = true;

//
//选择第2行第1列至第6行第1列,
selectRange = excelWorkSheet.get_Range(excelWorkSheet.Cells[2,
1],excelWorkSheet.Cells[6, 1]);

//
//合并单元格
selectRange.Merge(oMis);

selectRange.EntireColumn.AutoFit(); //全部列自适应宽度
selectRange.EntireRow.AutoFit(); //全部行自适应高度

selectRange.HorizontalAlignment
=Excel.XlHAlign.xlHAlignCenter;//水平居中
selectRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
//垂直居中

//
//设置单元格的格式
selectRange.NumberFormatLocal = @”yyyy-m-d;@”;
/*
NumberFormatLocal = “¥#,##0.00_);[红色](¥#,##0.00)”//货币
NumberFormatLocal = “0.00%” //百分比
NumberFormatLocal = “# ??/??” //分数
* */

//
//设置单元格的值
selectRange.set_Item(1, 1, “2007-02-27”);

//selectRange.ColumnWidth = 50; //固定宽度
selectRange.Columns.AutoFit(); //自适应宽度

selectRange.Borders.LineStyle =BorderStyle.FixedSingle; //设置边框样式
selectRange.Borders.Weight = Excel.XlBorderWeight.xlThin; //边框粗细
selectRange.Borders.ColorIndex =
Excel.XlColorIndex.xlColorIndexAutomatic; //颜色

excelWorkBook.Saved = true; //将工作薄的保存标志置为True

//捕捉异常,以防止文件为只读或已打开,保存时会出错
try
{
excelWorkBook.SaveCopyAs(@”d:/aa.xls”);
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}

//Excel程序退出,注意:要在外面释放内存资源
excelApp.Quit();
}

 

三、C# pdf操作

Adobe 可移植文档格式 (PDF) 由 Adobe Systems 创立, 并在超过 15
年的时间里日甄完善。使用 Adobe 可移植文档格式,
可以捕获和查看来自任何应用程序、任何计算机系统的丰富强大的信息,
并将这些信息与全球各地的任何人共享。
世界各地的个人、企业和政府机构都很信赖并依靠 Adobe® PDF
来交流思想和视野。

将信息解放, 思想自由交流

开放格式 — 进行更安全可靠的电子信息交流的事实标准,
已为世界各地的各个行业部门和政府机构所认可。 符合行业标准 (包括
PDF/A、PDF/X 和 PDF/E)。

多平台 — 可在任何平台 (Macintosh、Microsoft® Windows®、UNIX®
和许多移动平台) 上进行查看和打印。

可扩展性 — 全世界有超过 1,800 家供应商提供基于 PDF 的解决方案,
包括生成工具、插件、咨询、培训和支持工具。

可信可靠 — 现今网络上的超过 2 亿份 PDF 文档, 证明依靠 Adobe PDF
来捕获信息的组织机构数量可观。

维护信息完整性 — 无论通过什么应用程序创建, Adobe PDF
文件在外观上都与原始文件完全一致, 并且会保留源文件
(文本、绘图、3D、全色图像、照片, 甚至业务逻辑) 的信息。

This entry was posted in 北美历史 and tagged , . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用*标注