新版 itextsharp pdf code
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的更多相关文章
- WebStorm 2016 最新版激活(activation code方式)
WebStorm 2016 最新版激活(activation code方式) WebStorm activation code WebStorm 最新版本激活方式: 今天下载最新版本的WebStorm ...
- 【译】Asp.net mvc 使用ITextSharp PDF to HTML (解决img标签问题)
前言:因项目需求,需要将HTML代码转成PDF.大致上已经实现了,可以是发现使用ITextSharp(我现在的版本是5.5.9)的时候,img标签中的src只能跟绝对路径. 在百度上找了一个上午,有一 ...
- C# ITextSharp pdf 自动打印
PDF生成后直接进入打印预览不用下载 using iTextSharp.text; using iTextSharp.text.pdf; Document pdfDoc = new Document( ...
- .net通过iTextSharp.pdf操作pdf文件实现查找关键字签字盖章
之前这个事情都CA公司去做的,现在给客户做demo,要模拟一下签字盖章了,我们的业务PDF文件是动态生成的所以没法通过坐标定位,只能通过关键字查找定位了. 之前在网上看了许多通多通过查询关键字,然后图 ...
- 【转】WebStorm 2016 最新版激活(activation code方式)
作者:=金刚=博客地址:http://www.cnblogs.com/woaic WebStorm 最新版本激活方式:今天下载最新版本的WebStorm,发现原来的通过license server激活 ...
- [.Net] - 使用 iTextSharp 生成基于模板的 PDF,生成新文件并保留表单域
背景 基于 PDF Template 预填充表单项,生成一份新的 PDF 文件,并保留表单域允许继续修改. 代码段 using iTextSharp.text.pdf; /* Code Snippet ...
- 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版(转)
[译]在Asp.Net中操作PDF - iTextSharp - 利用列进行排版 在使用iTextSharp通过ASP.Net生成PDF的系列文章中,前面的文章已经讲述了iTextSharp所涵盖 ...
- Travis-CI与Latex构建开源中文PDF
博主有一本开源书籍,用 latex 排版,托管在Github上.但用 latex 不像是 Markdown,当tex文本更新时,用于最终浏览的PDF文件很难得到及时的更新, 所以博主一直想找到一套工具 ...
- 利用jpedal进行pdf转换成jpeg,jpg,png,tiff,tif等格式的图片
项目中运用到pdf文件转换成image图片,开始时使用pdfbox开源库进行图片转换,但是转换出来的文件中含有部分乱码的情况.下面是pdfBox 的pdf转换图片的代码示例. try{ String ...
随机推荐
- mongodb安装、启动、远程连接
1.现在mongodb安装包 mongodb-linux-x86_64-3.0.6.tgz 2.解压缩安装包 tar zxvf mongodb-linux-x86_64-3.0.6.tgz /opt ...
- 51nod1134(最长递增子序列)
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1134 题意: 中文题诶~ 思路: 直接暴力的话时间复杂度为 ...
- webuploader在IE8/9下上传遇到的两个问题
最近在做图片上传功能. 点击一个按钮,弹出一个iframe,它是百度的webuploader插件 在点击关闭按钮时,IE9下总是会报错: __flash__removeCallback未定义错误 解决 ...
- 缓存淘汰算法--LRU算法
1. LRU1.1. 原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是"如果数据最近被访问过,那么将来被访问的几率也 ...
- PHP socket上传文件图片
最近了解了下下socket方面的东西,想做一个socket上传文件的例子. 在网上搜了搜代码执行后,图片数据传输了一半,图片的下半部分是灰色的.然后就自己仿着搜来的代码和php.net 中socket ...
- net-force.nl/steganography writeup
做CTF题好长一段时间了,真的可以学到很多东西.这次,我们开启 net-force.nl 的 Steganography之旅,所谓的隐写术. level 801: Training - Can you ...
- 多个supplemental logging的说明与删除
有时候我们做ogg的时候,查看info trandata schema.table 无法查看到对应的supplement log信息, 但是我们查看table script的时候可以看到supplem ...
- 前端试题本(Javascript篇)
JS1. 下面这个JS程序的输出是什么:JS2.下面的JS程序输出是什么:JS3.页面有一个按钮button id为 button1,通过原生的js如何禁用?JS4.页面有一个按钮button id为 ...
- BFS_最短路径
已知若干个城市的地图,求从一个城市到另一个城市的路径,要求路径中经过的城市最少. #include<iostream> #include<cstring> using name ...
- ajax删除DB数据
1.前台js $().ready(function () { $('[name="checkAll"]').click(function () { if ("checke ...