188 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			188 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Text;
 | 
						|
using iTextSharp.text;
 | 
						|
using iTextSharp.text.pdf;
 | 
						|
using Volian.Svg.Library;
 | 
						|
 | 
						|
namespace Volian.Print.Library
 | 
						|
{
 | 
						|
	public class PageCount
 | 
						|
	{
 | 
						|
		private bool _CanIncrement = true;
 | 
						|
		public bool CanIncrement
 | 
						|
		{
 | 
						|
			get { return _CanIncrement; }
 | 
						|
			set { _CanIncrement = value; }
 | 
						|
		}
 | 
						|
		private int Increment()
 | 
						|
		{
 | 
						|
			if (CanIncrement) Total++;
 | 
						|
			CanIncrement = false;
 | 
						|
			return Total;
 | 
						|
		}
 | 
						|
		private int _Total;
 | 
						|
		public int Total
 | 
						|
		{
 | 
						|
			get { return _Total; }
 | 
						|
			set { _Total = value; }
 | 
						|
		}
 | 
						|
		private string _FinalPageMessage;
 | 
						|
 | 
						|
		public string FinalPageMessage
 | 
						|
		{
 | 
						|
			get { return _FinalPageMessage; }
 | 
						|
			set { _FinalPageMessage = value; }
 | 
						|
		}
 | 
						|
		private PageCountTemplates _MyTemplates; //  (for each page that has this key)
 | 
						|
		internal PageCountTemplates MyTemplates
 | 
						|
		{
 | 
						|
			get 
 | 
						|
			{
 | 
						|
				if (_MyTemplates == null) _MyTemplates = new PageCountTemplates(); 
 | 
						|
				return _MyTemplates;
 | 
						|
			}
 | 
						|
			set 
 | 
						|
			{
 | 
						|
				_MyTemplates = value; 
 | 
						|
			}
 | 
						|
		}
 | 
						|
		public void DrawTemplates()
 | 
						|
		{
 | 
						|
			int cnt = 0;
 | 
						|
			foreach (PageCountTemplate pct in MyTemplates)
 | 
						|
			{
 | 
						|
				cnt++;
 | 
						|
				string fstr = pct.Text.Replace("{OF}", _Total.ToString());
 | 
						|
				if (pct.Text.Contains("{TOCPAGE}"))
 | 
						|
				{
 | 
						|
					fstr = pct.Text.Replace("{TOCPAGE}", _Total.ToString());
 | 
						|
				}
 | 
						|
				if (fstr.Contains("{FINALPAGE}"))
 | 
						|
				{
 | 
						|
					fstr = fstr.Replace("{FINALPAGE}", (cnt == MyTemplates.Count) ? FinalPageMessage : "");
 | 
						|
				}
 | 
						|
				// use font from pct
 | 
						|
				string fontFace = pct.MyFont.Name;
 | 
						|
				int fontStyle = (pct.MyFont.Bold ? iTextSharp.text.Font.BOLD : 0) + (pct.MyFont.Italic ? iTextSharp.text.Font.ITALIC : 0);
 | 
						|
				Font itextFont = Volian.Svg.Library.Svg.GetFont(fontFace, pct.MyFont.Size, fontStyle, pct.MyColor);
 | 
						|
				ColumnText columnText = new ColumnText(pct.MyTemplate);
 | 
						|
				float textSize = itextFont.BaseFont.GetWidthPoint(fstr, pct.MyFont.Size);
 | 
						|
				float flly = itextFont.BaseFont.GetDescentPoint("g", pct.MyFont.Size);
 | 
						|
				float lead = columnText.Leading;
 | 
						|
				columnText.Alignment = pct.Alignment;
 | 
						|
				Chunk chk = new Chunk(fstr, itextFont);
 | 
						|
				float width = chk.GetWidthPoint();
 | 
						|
				// justification:
 | 
						|
				float left = 0;
 | 
						|
				float right = width * 1.01F;
 | 
						|
				switch (pct.Alignment)
 | 
						|
				{
 | 
						|
					case Element.ALIGN_CENTER:
 | 
						|
						left = -.5F * width;
 | 
						|
						right = .51F * width;
 | 
						|
						break;
 | 
						|
					case Element.ALIGN_RIGHT:
 | 
						|
						left = -1.01F * width;
 | 
						|
						right = 0;
 | 
						|
						break;
 | 
						|
				}
 | 
						|
				pct.MyTemplate.BoundingBox = new Rectangle(left, 2 * flly, right, columnText.Leading); //2*flly account for descenders
 | 
						|
				columnText.SetSimpleColumn(left, 0, right, columnText.Leading);
 | 
						|
				columnText.SetText(new Phrase(chk));
 | 
						|
				columnText.Go();
 | 
						|
			}
 | 
						|
		}
 | 
						|
		public PdfTemplate AddToTemplateList(PdfWriter pdfWriter, string text, System.Drawing.Font myFont, int alignment, System.Drawing.Color color)
 | 
						|
		{
 | 
						|
			Increment();
 | 
						|
			PageCountTemplate retTemplate = new PageCountTemplate(pdfWriter, text.Replace("{PAGE}",Total.ToString()), myFont, alignment, color);
 | 
						|
			MyTemplates.Add(retTemplate);
 | 
						|
			
 | 
						|
			return retTemplate.MyTemplate;
 | 
						|
		}
 | 
						|
	}
 | 
						|
	public class PageCounts : Dictionary<string, PageCount>
 | 
						|
	{
 | 
						|
		public bool CanIncrement
 | 
						|
		{
 | 
						|
			set 
 | 
						|
			{
 | 
						|
				foreach (PageCount pc in this.Values)
 | 
						|
				{
 | 
						|
					pc.CanIncrement = value;
 | 
						|
				}
 | 
						|
			}
 | 
						|
		}
 | 
						|
		public PdfTemplate AddToTemplateList(string key, PdfWriter pdfWriter, string text, System.Drawing.Font myFont, int alignment, System.Drawing.Color color)
 | 
						|
		{
 | 
						|
			if (!this.ContainsKey(key))
 | 
						|
			{
 | 
						|
				this.Add(key, new PageCount());
 | 
						|
				if (key.StartsWith("TOC")) this[key].CanIncrement = false;
 | 
						|
			}
 | 
						|
			return (this[key].AddToTemplateList(pdfWriter, text, myFont, alignment,color));
 | 
						|
		}
 | 
						|
		public PdfTemplate AddToTemplateList(string key, PdfWriter pdfWriter, string text, string finalPageMessage, System.Drawing.Font myFont, int alignment, System.Drawing.Color color)
 | 
						|
		{
 | 
						|
			if (!this.ContainsKey(key)) this.Add(key, new PageCount());
 | 
						|
			this[key].FinalPageMessage = finalPageMessage;
 | 
						|
			return (this[key].AddToTemplateList(pdfWriter, text, myFont, alignment, color));
 | 
						|
		}
 | 
						|
		public void DrawTemplates()
 | 
						|
		{
 | 
						|
			foreach (PageCount pc in this.Values)
 | 
						|
			{
 | 
						|
				pc.DrawTemplates();
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	public class PageCountTemplate
 | 
						|
	{
 | 
						|
		private string _Text;
 | 
						|
		public string Text				// "Page 1 of {OF}"
 | 
						|
		{
 | 
						|
			get { return _Text; }
 | 
						|
			set { _Text = value; }
 | 
						|
		}
 | 
						|
		private System.Drawing.Font _MyFont;
 | 
						|
		public System.Drawing.Font MyFont
 | 
						|
		{
 | 
						|
			get { return _MyFont; }
 | 
						|
			set { _MyFont = value; }
 | 
						|
		}
 | 
						|
		private int _Alignment;		// iTextSharp Element.<XYZ>
 | 
						|
		public int Alignment
 | 
						|
		{
 | 
						|
			get { return _Alignment; }
 | 
						|
			set { _Alignment = value; }
 | 
						|
		}
 | 
						|
		private System.Drawing.Color _MyColor;
 | 
						|
		public System.Drawing.Color MyColor
 | 
						|
		{
 | 
						|
			get { return _MyColor; }
 | 
						|
			set { _MyColor = value; }
 | 
						|
		}
 | 
						|
		private PdfTemplate _MyTemplate;
 | 
						|
		public PdfTemplate MyTemplate
 | 
						|
		{
 | 
						|
			get { return _MyTemplate; }
 | 
						|
			set { _MyTemplate = value; }
 | 
						|
		}
 | 
						|
		public PageCountTemplate(PdfWriter pdfWriter, string text, System.Drawing.Font myFont, int alignment, System.Drawing.Color color)
 | 
						|
		{
 | 
						|
			// Create Template can be called with a small, i.e. 1, width/height because when
 | 
						|
			// it is actually drawn, the bounding box overrides the CreateTemplate values.
 | 
						|
			_MyTemplate = pdfWriter.DirectContent.CreateTemplate(1, 1);
 | 
						|
			_Text = text;
 | 
						|
			_MyFont = myFont;
 | 
						|
			_MyColor = color;
 | 
						|
			_Alignment = alignment;
 | 
						|
		}
 | 
						|
	}
 | 
						|
	public class PageCountTemplates : List<PageCountTemplate>
 | 
						|
	{
 | 
						|
	}
 | 
						|
}
 |