using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace iTextSharp.Demo
{
/**//// <summary>
/// Making a document with a header containing 'page x of y' and with a watermark on every page.
/// </summary>
public class PageNumbersWatermark : IPdfPageEvent
{
/**//** An Image that goes in the header. */
public Image headerImage;
/**//** The headertable. */
public PdfPTable table;
/**//** The Graphic state */
public PdfGState gstate;
/**//** A template that will hold the total number of pages. */
public PdfTemplate tpl;
/**//** The font that will be used. */
public BaseFont helv;
public PageNumbersWatermark()
{
try
{
// step 1: creating the document
Document doc = new Document(PageSize.A4, , , , );
// step 2: creating the writer PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("pageNumbersWatermark.pdf",FileMode.Create));
// step 3: initialisations + opening the document
writer.PageEvent =new PageNumbersWatermark();
doc.Open();
// step 4: adding content
string text = "some padding text ";
for (int k = ; k < ; ++k)
text += text;
Paragraph p = new Paragraph(text);
p.Alignment=(Element.ALIGN_JUSTIFIED);
doc.Add(p);
// step 5: closing the document
doc.Close();
}
catch ( Exception e )
{
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}
}
IPdfPageEvent Members#region IPdfPageEvent Members
public void OnOpenDocument(PdfWriter writer, Document document)
{
try
{
// initialization of the header table
headerImage = Image.GetInstance("logo.gif");
table = new PdfPTable();
Phrase p = new Phrase();
Chunk ck = new Chunk("lowagie.com\n", new Font(Font.TIMES_ROMAN, , Font.BOLDITALIC, Color.BLUE));
p.Add(ck);
ck = new Chunk("Ghent\nBelgium", new Font(Font.HELVETICA, , Font.NORMAL, Color.DARK_GRAY));
p.Add(ck);
table.DefaultCell.BackgroundColor=(Color.YELLOW);
table.DefaultCell.BorderWidth=();
table.AddCell(p);
table.DefaultCell.HorizontalAlignment=(Element.ALIGN_RIGHT);
table.AddCell(new Phrase(new Chunk(headerImage, , )));
// initialization of the Graphic State
gstate = new PdfGState();
gstate.FillOpacity=(0.3f);
gstate.StrokeOpacity=(0.3f);
// initialization of the template
tpl = writer.DirectContent.CreateTemplate(, );
tpl.BoundingBox=(new Rectangle(-, -, , ));
// initialization of the font
helv = BaseFont.CreateFont("Helvetica", BaseFont.WINANSI, false);
}
catch(Exception e)
{
throw e;
}
}
public void OnCloseDocument(PdfWriter writer, Document document)
{
tpl.BeginText();
tpl.SetFontAndSize(helv, );
tpl.SetTextMatrix(, );
tpl.ShowText("" + (writer.PageNumber - ));
tpl.EndText();
}
public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition)
{
// TODO: Add PageNumbersWatermark.OnParagraph implementation
}
public void OnEndPage(PdfWriter writer, Document document)
{
PdfContentByte cb = writer.DirectContent;
cb.SaveState();
// write the headertable
table.TotalWidth=(document.Right - document.Left);
table.WriteSelectedRows(, -, document.Left, document.PageSize.Height - , cb);
// compose the footer
String text = "Page " + writer.PageNumber + " of ";
float textSize = helv.GetWidthPoint(text, );
float textBase = document.Bottom - ;
cb.BeginText();
cb.SetFontAndSize(helv, );
// for odd pagenumbers, show the footer at the left
if ((writer.PageNumber & ) == )
{
cb.SetTextMatrix(document.Left, textBase);
cb.ShowText(text);
cb.EndText();
cb.AddTemplate(tpl, document.Left + textSize, textBase);
}
// for even numbers, show the footer at the right
else
{
float adjust = helv.GetWidthPoint("", );
cb.SetTextMatrix(document.Right - textSize - adjust, textBase);
cb.ShowText(text);
cb.EndText();
cb.AddTemplate(tpl, document.Right - adjust, textBase);
}
cb.SaveState();
// draw a Rectangle around the page
cb.SetColorStroke(Color.ORANGE);
cb.SetLineWidth();
cb.Rectangle(, , document.PageSize.Width - , document.PageSize.Height - );
cb.Stroke();
cb.RestoreState();
// starting on page 3, a watermark with an Image that is made transparent
if (writer.PageNumber >= )
{
cb.SetGState(gstate);
cb.SetColorFill(Color.RED);
cb.BeginText();
cb.SetFontAndSize(helv, );
cb.ShowTextAligned(Element.ALIGN_CENTER, "Watermark Opacity " + writer.PageNumber, document.PageSize.Width / , document.PageSize.Height / , );
cb.EndText();
try
{
cb.AddImage(headerImage, headerImage.Width, , , headerImage.Height, , );
}
catch(Exception e)
{
throw e;
}
cb.RestoreState();
}
}
public void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title)
{
// TODO: Add PageNumbersWatermark.OnSection implementation
}
public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition)
{
// TODO: Add PageNumbersWatermark.OnSectionEnd implementation
}
public void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition)
{
// TODO: Add PageNumbersWatermark.OnParagraphEnd implementation
}
public void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, string text)
{
// TODO: Add PageNumbersWatermark.OnGenericTag implementation
}
public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition)
{
// TODO: Add PageNumbersWatermark.OnChapterEnd implementation
}
public void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
{
// TODO: Add PageNumbersWatermark.OnChapter implementation
}
public void OnStartPage(PdfWriter writer, Document document)
{
if (writer.PageNumber < )
{
PdfContentByte cb = writer.DirectContentUnder;
cb.SaveState();
cb.SetColorFill(Color.PINK);
cb.BeginText();
cb.SetFontAndSize(helv, );
cb.ShowTextAligned(Element.ALIGN_CENTER, "My Watermark Under " + writer.PageNumber, document.PageSize.Width / , document.PageSize.Height / , );
cb.EndText();
cb.RestoreState();
}
}
#endregion
}
}

新版 itextsharp pdf code的更多相关文章

  1. WebStorm 2016 最新版激活(activation code方式)

    WebStorm 2016 最新版激活(activation code方式) WebStorm activation code WebStorm 最新版本激活方式: 今天下载最新版本的WebStorm ...

  2. 【译】Asp.net mvc 使用ITextSharp PDF to HTML (解决img标签问题)

    前言:因项目需求,需要将HTML代码转成PDF.大致上已经实现了,可以是发现使用ITextSharp(我现在的版本是5.5.9)的时候,img标签中的src只能跟绝对路径. 在百度上找了一个上午,有一 ...

  3. C# ITextSharp pdf 自动打印

    PDF生成后直接进入打印预览不用下载 using iTextSharp.text; using iTextSharp.text.pdf; Document pdfDoc = new Document( ...

  4. .net通过iTextSharp.pdf操作pdf文件实现查找关键字签字盖章

    之前这个事情都CA公司去做的,现在给客户做demo,要模拟一下签字盖章了,我们的业务PDF文件是动态生成的所以没法通过坐标定位,只能通过关键字查找定位了. 之前在网上看了许多通多通过查询关键字,然后图 ...

  5. 【转】WebStorm 2016 最新版激活(activation code方式)

    作者:=金刚=博客地址:http://www.cnblogs.com/woaic WebStorm 最新版本激活方式:今天下载最新版本的WebStorm,发现原来的通过license server激活 ...

  6. [.Net] - 使用 iTextSharp 生成基于模板的 PDF,生成新文件并保留表单域

    背景 基于 PDF Template 预填充表单项,生成一份新的 PDF 文件,并保留表单域允许继续修改. 代码段 using iTextSharp.text.pdf; /* Code Snippet ...

  7. 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版(转)

    [译]在Asp.Net中操作PDF - iTextSharp - 利用列进行排版   在使用iTextSharp通过ASP.Net生成PDF的系列文章中,前面的文章已经讲述了iTextSharp所涵盖 ...

  8. Travis-CI与Latex构建开源中文PDF

    博主有一本开源书籍,用 latex 排版,托管在Github上.但用 latex 不像是 Markdown,当tex文本更新时,用于最终浏览的PDF文件很难得到及时的更新, 所以博主一直想找到一套工具 ...

  9. 利用jpedal进行pdf转换成jpeg,jpg,png,tiff,tif等格式的图片

    项目中运用到pdf文件转换成image图片,开始时使用pdfbox开源库进行图片转换,但是转换出来的文件中含有部分乱码的情况.下面是pdfBox 的pdf转换图片的代码示例. try{ String ...

随机推荐

  1. File文件的使用

    线程的停止: 1.停止一个线程一般是通过一个变量来控制 2.如果需要停止一个处于一个等待状态的线程,那么需要配合interrupture方法来完成 守护线程:(后台线程):在一个进程中只剩下守护线程, ...

  2. 修复win10升级时断电inaccessible boot device

    代码提示0xc0000001 这里的解决方法对系统以及个人的没有文件丢失的影响,没有重装以及重置上丢失东西的顾虑 解决方法: WINDOWS/system32/config/RegBack/SYSTE ...

  3. HAProxy的安装与使用

    在互联网时代中,后台系统架构,经常可以听到高可用集群.负载均衡集群之类的系统架构解决方案,其中,负载均衡有基于硬件的F5.Big-IP等,也有基于软件的LVS(基于Linux操作系统实现,性能可以和基 ...

  4. MySQL和Lucene索引对比分析

    MySQL和Lucene都可以对数据构建索引并通过索引查询数据,一个是关系型数据库,一个是构建搜索引擎(Solr.ElasticSearch)的核心类库.两者的索引(index)有什么区别呢?以前写过 ...

  5. lodash

    lodash常用函数一 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  6. [Tool]Inno Setup创建软件安装程序。

    这篇博客将介绍如何使用Inno Setup创建一个软件安装程序. Inno Setup官网:http://www.jrsoftware.org/isinfo.php. 可以下载到最新的Inno Set ...

  7. MIT 6.828 JOS学习笔记18. Lab 3.2 Part B: Page Faults, Breakpoints Exceptions, and System Calls

    现在你的操作系统内核已经具备一定的异常处理能力了,在这部分实验中,我们将会进一步完善它,使它能够处理不同类型的中断/异常. Handling Page Fault 缺页中断是一个非常重要的中断,因为我 ...

  8. Git - 问题集

    1.If no other git process is currently running, this probably means a git process crashed in this re ...

  9. unity 协程

    StartCoroutine在unity3d的帮助中叫做协程,意思就是启动一个辅助的线程. 在C#中直接有Thread这个线程,但是在unity中有些元素是不能操作的.这个时候可以使用协程来完成. 使 ...

  10. 【BZOJ3943】[Usaco2015 Feb]SuperBull 最小生成树

    [BZOJ3943][Usaco2015 Feb]SuperBull Description Bessie and her friends are playing hoofball in the an ...