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. jquery中on绑定事件

    之前项目中动态创建的标签元素  在绑定事件的时候  都是无效  无论如何都不能触发 eg:在页面加载完成之后   再由脚本动态创建的<div>元素  在绑定事件的时候 例如click事件 ...

  2. java项目上线过程

    关于如何将Javaweb上线,部署到公网,让全世界的人都可以访问的问题.小编将作出系列化,完整的流程介绍. 1.在myeclipse中开发好项目,打包成war格式,不会的同学参考以下 http://z ...

  3. 【Django】--Model字段

    参考地址:http://www.cnblogs.com/wupeiqi/articles/6216618.html 所有字段 AutoField(Field) --int自增列,必须填入参数prima ...

  4. Ubuntu 12.04 安装 Apache2+PHP5+MySQL

    LAMP是Linux web服务器组合套装的缩写,分别是Apache+MySQL+PHP.此教程教大家如何在Ubuntu 12.04 LTS server 上安装Apache2服务器,包括PHP5(m ...

  5. iOS支付宝支付总结

    1.按照http://doc.open.alipay.com/doc2/detail?spm=0.0.0.0.SWdJgo&treeId=59&articleId=103676& ...

  6. iOS-APP提交上架流程(新手必看!2016年3月1日最新版)

    自己的经验总结,有错的话请留言,第一时间更改. 先大概说一下iOSAPP上架的几个步骤(详细步骤见下图): 创建证书请求文件 登录苹果开发者中心生成发布者证书(下载下来要双击一下) 设置APPID(要 ...

  7. Java Static关键字详解

    提起static关键字,相信大家绝对不会陌生,但是,想要完全说明白,猛的一想,发现自己好像又说不太明白... ...比方说,昨天被一个同学问起的时候... ... 当然,不是所有人都像我一样学艺不精的 ...

  8. 图文介绍如何在Eclipse统计代码行数(转)

    使用Eclipse可以方便的统计工程或文件的代码行数,方法如下: 1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File... 2.选中正则表达式(Regular expressi ...

  9. Good Bye 2016 - C

    题目链接:http://codeforces.com/contest/750/problem/C 题意:在CF中,每个人都有个Rank值. 当Rank>=1900时,为DIV1.Rank< ...

  10. BZOJ 3144 [Hnoi2013]切糕 ——网络流

    [题目分析] 网络流好题! 从割的方面来考虑问题往往会得到简化. 当割掉i,j,k时,必定附近的要割在k-D到k+D上. 所以只需要建两条inf的边来强制,如果割不掉强制范围内的时候,原来的边一定会换 ...