标签:
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, 50, 50, 100, 72); // 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 = 0; k < 10; ++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(2); Phrase p = new Phrase(); Chunk ck = new Chunk("lowagie.com\n", new Font(Font.TIMES_ROMAN, 16, Font.BOLDITALIC, Color.BLUE)); p.Add(ck); ck = new Chunk("Ghent\nBelgium", new Font(Font.HELVETICA, 12, Font.NORMAL, Color.DARK_GRAY)); p.Add(ck); table.DefaultCell.BackgroundColor=(Color.YELLOW); table.DefaultCell.BorderWidth=(0); table.AddCell(p); table.DefaultCell.HorizontalAlignment=(Element.ALIGN_RIGHT); table.AddCell(new Phrase(new Chunk(headerImage, 0, 0))); // initialization of the Graphic State gstate = new PdfGState(); gstate.FillOpacity=(0.3f); gstate.StrokeOpacity=(0.3f); // initialization of the template tpl = writer.DirectContent.CreateTemplate(100, 100); tpl.BoundingBox=(new Rectangle(-20, -20, 100, 100)); // 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, 12); tpl.SetTextMatrix(0, 0); tpl.ShowText("" + (writer.PageNumber - 1)); 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(0, -1, document.Left, document.PageSize.Height - 50, cb); // compose the footer String text = "Page " + writer.PageNumber + " of "; float textSize = helv.GetWidthPoint(text, 12); float textBase = document.Bottom - 20; cb.BeginText(); cb.SetFontAndSize(helv, 12); // for odd pagenumbers, show the footer at the left if ((writer.PageNumber & 1) == 1) { 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("0", 12); 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(2); cb.Rectangle(20, 20, document.PageSize.Width - 40, document.PageSize.Height - 40); cb.Stroke(); cb.RestoreState(); // starting on page 3, a watermark with an Image that is made transparent if (writer.PageNumber >= 3) { cb.SetGState(gstate); cb.SetColorFill(Color.RED); cb.BeginText(); cb.SetFontAndSize(helv, 48); cb.ShowTextAligned(Element.ALIGN_CENTER, "Watermark Opacity " + writer.PageNumber, document.PageSize.Width / 2, document.PageSize.Height / 2, 45); cb.EndText(); try { cb.AddImage(headerImage, headerImage.Width, 0, 0, headerImage.Height, 440, 80); } 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 < 3) { PdfContentByte cb = writer.DirectContentUnder; cb.SaveState(); cb.SetColorFill(Color.PINK); cb.BeginText(); cb.SetFontAndSize(helv, 48); cb.ShowTextAligned(Element.ALIGN_CENTER, "My Watermark Under " + writer.PageNumber, document.PageSize.Width / 2, document.PageSize.Height / 2, 45); cb.EndText(); cb.RestoreState(); } } #endregion } }
标签:
原文地址:http://www.cnblogs.com/xuzai/p/4346852.html