新版 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
}
}
上一篇:十分钟学会python


下一篇:HDU Exponentiation 1063 Java大数题解