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. 【转】Caffe初试(七)其它常用层及参数

    本文讲解一些其它的常用层,包括:softmax-loss层,Inner Product层,accuracy层,reshape层和dropout层及它们的参数配置. 1.softmax-loss sof ...

  2. 1280*720P和1920*1080P的视频在25帧30帧50帧60帧时的参数

  3. GruntJS学习(转)

    GruntJS 是基于JavaScript的命令行构建工具,它可以帮助开发者们自动化重复性的工作.你可以把它看成是JavaScript下的Make或者Ant.它可以完成诸如精简.编译.单元测试.lin ...

  4. 分布式消息总线,基于.NET Socket Tcp的发布-订阅框架之离线支持,附代码下载

    一.分布式消息总线以及基于Socket的实现 在前面的分享一个分布式消息总线,基于.NET Socket Tcp的发布-订阅框架,附代码下载一文之中给大家分享和介绍了一个极其简单也非常容易上的基于.N ...

  5. windows server 2008禁止ping

    在cmd命令行中通过ping一个主机名.域名或ip地址可以获取到该机器的ip.操作系统等很多信息,处于安全考虑,很多时候不希望服务器被ping,在windows server 2008 r2可以这样操 ...

  6. Linux学习笔记(10)-信号

    所谓信号(singal),在我的理解来说,其实和单片机开发中的中断差不多,但是它并非是由系统硬件所提供的,而是软件操作系统的支持的一种提醒机制. 收到信号之后的处理方法,一般由三种: (1)第一种是类 ...

  7. Mysql数据库的使用总结之Innodb简介

     最近在对开发的软件的服务器部分制作安装包,但服务器部分需要有mysql数据库的支持.因此,采用免安装版的mysql策略:将mysql数据库需要的文件在安装程序中进行设置和打包即可.但也遇到了很多问题 ...

  8. SQL 将2张不相关的表拼接成2列,批量更新至另一张表

    update SO_Master set LotteryNo=t2.LotteryNo,UpdateTime=GETDATE() --select sm.LotteryNo,sm.SysNo,t2.L ...

  9. 【APICloud】利用sublimetext3编写apicloud

    下载sublime text 3 安装插件 使用模糊搜索apicloud有三个插件全部下载下来 安装海马玩模拟器,这是一个安卓的模拟器,进入官网下载后直接安装就可以了. 打开sublime text ...

  10. 【MongoDB初识】-其他操作

    又发现一种查询写法$wheredb.class.find({$}}) 排重db.class.distinct("stuCount") 一.MapReduce(摘录MongoDB实战 ...