diff --git a/PROMS/Volian.Svg.Library/Svg.cs b/PROMS/Volian.Svg.Library/Svg.cs index d3cd6c00..4e09b9b6 100644 --- a/PROMS/Volian.Svg.Library/Svg.cs +++ b/PROMS/Volian.Svg.Library/Svg.cs @@ -9,6 +9,12 @@ namespace Volian.Svg.Library [XmlRoot("svg")] public partial class Svg : SvgGroup { + private static System.Drawing.Color _OverrrideColor = System.Drawing.Color.Empty; + public static System.Drawing.Color OverrideColor + { + get { return Svg._OverrrideColor; } + set { Svg._OverrrideColor = value; } + } #region XML Serializer Namespaces private XmlSerializerNamespaces _Namespaces = null; [XmlNamespaceDeclarations] @@ -43,18 +49,18 @@ namespace Volian.Svg.Library get { return SvgXmlConverter.GetString(_Width); } set { _Width = SvgXmlConverter.GetObject(value); } } - private string _Watermark=""; - [XmlAttribute("watermark")] - public string Watermark - { - get { return _Watermark; } - set { _Watermark = value; } - } - public void SetValidWaterMark(string token, string watermark) - { - if (token == "{" + watermark.ToUpper() + "PAGE}" ) - _Watermark = watermark; - } + //private string _Watermark=""; + //[XmlAttribute("watermark")] + //public string Watermark + //{ + // get { return _Watermark; } + // set { _Watermark = value; } + //} + //public void SetValidWaterMark(string token, string watermark) + //{ + // if (token == "{" + watermark.ToUpper() + "PAGE}" ) + // _Watermark = watermark; + //} #endregion #region Height private SvgMeasurement _Height = new SvgMeasurement(); diff --git a/PROMS/Volian.Svg.Library/SvgLinePart.cs b/PROMS/Volian.Svg.Library/SvgLinePart.cs index 9dead6ea..f0c859a7 100644 --- a/PROMS/Volian.Svg.Library/SvgLinePart.cs +++ b/PROMS/Volian.Svg.Library/SvgLinePart.cs @@ -22,13 +22,13 @@ namespace Volian.Svg.Library [XmlIgnore] protected Color LineColor { - get { return _MyLineSettings.LineColor; } + get { return Svg.OverrideColor != System.Drawing.Color.Empty ? Svg.OverrideColor : _MyLineSettings.LineColor; } set { _MyLineSettings.LineColor = value; } } [XmlIgnore] protected Color OutlineColor { - get { return _MyLineSettings.OutlineColor; } + get { return Svg.OverrideColor != System.Drawing.Color.Empty ? Svg.OverrideColor :_MyLineSettings.OutlineColor; } set { _MyLineSettings.OutlineColor = value; } } [System.ComponentModel.DefaultValueAttribute("")] diff --git a/PROMS/Volian.Svg.Library/SvgShapePart.cs b/PROMS/Volian.Svg.Library/SvgShapePart.cs index 58b4d427..36da7910 100644 --- a/PROMS/Volian.Svg.Library/SvgShapePart.cs +++ b/PROMS/Volian.Svg.Library/SvgShapePart.cs @@ -21,7 +21,7 @@ namespace Volian.Svg.Library [XmlIgnore] protected Color FillColor { - get { return _MyFillSettings.FillColor; } + get { return Svg.OverrideColor != System.Drawing.Color.Empty ? Svg.OverrideColor : _MyFillSettings.FillColor; } set { _MyFillSettings.FillColor = value; } } #endregion diff --git a/PROMS/Volian.Svg.Library/iTextSharp.cs b/PROMS/Volian.Svg.Library/iTextSharp.cs index b955de29..b2fa94aa 100644 --- a/PROMS/Volian.Svg.Library/iTextSharp.cs +++ b/PROMS/Volian.Svg.Library/iTextSharp.cs @@ -13,6 +13,9 @@ using Itenso.Rtf.Interpreter; //using Itenso.Rtf.Model; using Itenso.Rtf.Support; using Microsoft.Win32; +using System.Text.RegularExpressions; +using System.Xml; +using System.IO; namespace Volian.Svg.Library { @@ -77,6 +80,14 @@ namespace Volian.Svg.Library } public partial class Svg : SvgGroup { + public void DrawMacro(string macroDef, float x, float y, PdfContentByte cb) + { + _MyContentByte = cb; + SvgMeasurement X = new SvgMeasurement(x-6, E_MeasurementUnits.PT); // Not sure why this is? seems to be a character too far to the right? + SvgMeasurement Y = new SvgMeasurement(Height.GetSizeInPoints(72) - y, E_MeasurementUnits.PT); + SvgGroup macro = this[macroDef] as SvgGroup; + macro.SvgParts.Draw(cb, MyScale.AdjustOrigin(X, Y), this,this); + } public event SvgProcessTextEvent ProcessText; internal string OnProcessText(string myText) { @@ -126,16 +137,27 @@ namespace Volian.Svg.Library get { return _TopMargin; } set { _TopMargin = value; } } + private SvgScale _MyScale; + public SvgScale MyScale + { + get + { + if (_MyScale == null) + { + _MyScale = new SvgScale(72, new System.Drawing.RectangleF(0, 0, _MyContentByte.PdfWriter.PageSize.Width, _MyContentByte.PdfWriter.PageSize.Height), _Width, _Height, _ViewBox); + _MyScale.XLowerLimit -= _LeftMargin; + _MyScale.YLowerLimit -= _TopMargin; + } + return _MyScale; + } + } public void Draw(PdfContentByte cb) { //myPdf.Clear(); _MyContentByte = cb; _Templates = new Dictionary(); //RegisterFonts(); - SvgScale scale = new SvgScale(72, new System.Drawing.RectangleF(0, 0, cb.PdfWriter.PageSize.Width, cb.PdfWriter.PageSize.Height), _Width, _Height, _ViewBox); - scale.XLowerLimit-=_LeftMargin; - scale.YLowerLimit -= _TopMargin; - SvgParts.Draw(cb, scale, this, this); //72 - Points + SvgParts.Draw(cb, MyScale, this, this); //72 - Points } public static iTextSharp.text.Font GetFont(string fontName) { @@ -388,10 +410,10 @@ namespace Volian.Svg.Library { //cb.SetLineWidth(scale.M(LineWidth)); cb.SetLineWidth(LineWidth.Value); - //cb.SetColorStroke(new Color(LineColor)); - cb.SetColorStroke(Color.BLACK); + cb.SetColorStroke(new Color(LineColor)); + //cb.SetColorStroke(Color.BLACK); } - cb.SetColorStroke(Color.GREEN); + //cb.SetColorStroke(Color.GREEN); cb.SetLineCap(PdfContentByte.LINE_CAP_ROUND); cb.MoveTo(scale.X(X1), scale.Y(cb, Y1)); cb.LineTo(scale.X(X2), scale.Y(cb, Y2)); @@ -470,33 +492,33 @@ namespace Volian.Svg.Library tmp.RestoreState(); } } - public partial class SvgRtf : SvgShapePart - { - public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) - { - ColumnText ct = new ColumnText(cb); - IRtfDocument myRtf = RtfInterpreterTool.BuildDoc(Rtf); - Rtf2iTextSharp rtf = new Rtf2iTextSharp(myRtf); - Paragraph p = rtf.Convert(); - p.SetLeading(0, 1); - float leading = 10; - ct.SetSimpleColumn(scale.X(X), scale.Y(cb, Y) + leading, scale.X(X) + scale.M(Width), scale.Y(cb, Y) - cb.PdfDocument.PageSize.Height); - ct.AddElement(p); - ct.Go(); - } - public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) - { - ColumnText ct = new ColumnText(tmp); - IRtfDocument myRtf = RtfInterpreterTool.BuildDoc(Rtf); - Rtf2iTextSharp rtf = new Rtf2iTextSharp(myRtf); - Paragraph p = rtf.Convert(); - p.SetLeading(0, 1); - float leading = 10; - ct.SetSimpleColumn(scale.X(X), scale.Y(Y) + leading, scale.X(X) + scale.M(Width), scale.Y(Y) - tmp.PdfDocument.PageSize.Height); - ct.AddElement(p); - ct.Go(); - } - } + //public partial class SvgRtf : SvgShapePart + //{ + // public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + // { + // ColumnText ct = new ColumnText(cb); + // IRtfDocument myRtf = RtfInterpreterTool.BuildDoc(Rtf); + // Rtf2iTextSharp rtf = new Rtf2iTextSharp(myRtf); + // Paragraph p = rtf.Convert(); + // p.SetLeading(0, 1); + // float leading = 10; + // ct.SetSimpleColumn(scale.X(X), scale.Y(cb, Y) + leading, scale.X(X) + scale.M(Width), scale.Y(cb, Y) - cb.PdfDocument.PageSize.Height); + // ct.AddElement(p); + // ct.Go(); + // } + // public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + // { + // ColumnText ct = new ColumnText(tmp); + // IRtfDocument myRtf = RtfInterpreterTool.BuildDoc(Rtf); + // Rtf2iTextSharp rtf = new Rtf2iTextSharp(myRtf); + // Paragraph p = rtf.Convert(); + // p.SetLeading(0, 1); + // float leading = 10; + // ct.SetSimpleColumn(scale.X(X), scale.Y(Y) + leading, scale.X(X) + scale.M(Width), scale.Y(Y) - tmp.PdfDocument.PageSize.Height); + // ct.AddElement(p); + // ct.Go(); + // } + //} public partial class SvgText : SvgShapePart { public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) @@ -577,15 +599,14 @@ namespace Volian.Svg.Library float Offset = 0; if (Font.Underline) { - chk.SetUnderline(iTextSharp.text.Color.GREEN, 0, 0.047F, 0, -.1373F, PdfContentByte.LINE_CAP_ROUND); + chk.SetUnderline(new Color(FillColor), 0, 0.047F, 0, -.1373F, PdfContentByte.LINE_CAP_ROUND); } Phrase ph = new Phrase(chk); //Paragraph ph = new Paragraph(chk); ct.SetSimpleColumn(x, y + ph.Leading + Offset, xRight + 1F, y + ph.Leading + Offset - 2 * font.Size); ct.AddElement(ph); cb.SaveState(); - //cb.SetColorStroke(iTextSharp.text.Color.RED); - cb.SetColorFill(iTextSharp.text.Color.GREEN); + cb.SetColorFill(new Color(FillColor)); ct.Go(); cb.RestoreState(); //Console.WriteLine("'{0}',{1},{2},{3},{4}", Text, scale.X(X), yScale, X, Y); @@ -642,148 +663,148 @@ namespace Volian.Svg.Library tmp.RestoreState(); } } - public class Rtf2iTextSharp : RtfVisualVisitorBase - { - private IRtfDocument _RtfDoc; - private Paragraph _MyParagraph = new Paragraph(); - // public Rtf2iTextSharp(IRtfDocument rtfDoc, Document doc, PdfWriter writer) - public Rtf2iTextSharp(IRtfDocument rtfDoc) - { - if (rtfDoc == null) - throw new ArgumentNullException("rtfDoc"); - _RtfDoc = rtfDoc; - } - public Paragraph Convert() - { - _MyParagraph.Clear(); - foreach (IRtfVisual visual in _RtfDoc.VisualContent) - visual.Visit(this); - //_MyParagraph.SetLeading(0, 1); - return _MyParagraph; - } - // ---------------------------------------------------------------------- - protected override void DoVisitBreak(IRtfVisualBreak visualBreak) - { - switch (visualBreak.BreakKind) - { - case RtfVisualBreakKind.Line: - break; - case RtfVisualBreakKind.Page: - break; - case RtfVisualBreakKind.Paragraph: - //_MyParagraph.Add("\r\n"); - break; - case RtfVisualBreakKind.Section: - break; - default: - break; - } - //_MyParagraph.Add(string.Format("<{0}>", visualBreak.BreakKind.ToString())); - } - protected override void DoVisitSpecial(IRtfVisualSpecialChar visualSpecialChar) - { - //_MyParagraph.Add(string.Format("", visualSpecialChar.CharKind.ToString())); - switch (visualSpecialChar.CharKind) - { - case RtfVisualSpecialCharKind.Bullet: - break; - case RtfVisualSpecialCharKind.EmDash: - break; - case RtfVisualSpecialCharKind.EmSpace: - break; - case RtfVisualSpecialCharKind.EnDash: - break; - case RtfVisualSpecialCharKind.EnSpace: - break; - case RtfVisualSpecialCharKind.LeftDoubleQuote: - break; - case RtfVisualSpecialCharKind.LeftSingleQuote: - break; - case RtfVisualSpecialCharKind.NonBreakingHyphen: - break; - case RtfVisualSpecialCharKind.NonBreakingSpace: - _MyParagraph.Add(new Chunk("\u00A0")); - break; - case RtfVisualSpecialCharKind.OptionalHyphen: - break; - case RtfVisualSpecialCharKind.ParagraphNumberBegin: - break; - case RtfVisualSpecialCharKind.ParagraphNumberEnd: - break; - case RtfVisualSpecialCharKind.QmSpace: - break; - case RtfVisualSpecialCharKind.RightDoubleQuote: - break; - case RtfVisualSpecialCharKind.RightSingleQuote: - break; - case RtfVisualSpecialCharKind.Tabulator: - break; - default: - break; - } - } - //private static Dictionary _MyBaseFonts = new Dictionary(); - //private static BaseFont GetBaseFont(string fontName) - //{ - // if (!_MyBaseFonts.ContainsKey(fontName)) - // _MyBaseFonts.Add(fontName, BaseFont.CreateFont(FontFind.FullFileName(fontName), BaseFont.IDENTITY_H, BaseFont.EMBEDDED)); - // return _MyBaseFonts[fontName]; - //} - //private static Dictionary _MyFonts = new Dictionary(); - //private static iTextSharp.text.Font GetFont(string fontName, int size, int style) - //{ - // string key = string.Format("{0}.{1}.{2}", fontName, size, style); - // if (!_MyFonts.ContainsKey(key)) - // _MyFonts.Add(key, new iTextSharp.text.Font(GetBaseFont(fontName), size / 2, style)); - // return _MyFonts[key]; - //} - //protected override void DoVisitText(IRtfVisualText visualText) - //{ - // if (visualText.Format.IsHidden) return; - // //iTextSharp.text.Font font = GetFont(visualText.Format.Font.Name, visualText.Format.FontSize, - // // (visualText.Format.IsBold ? iTextSharp.text.Font.BOLD : 0) + (visualText.Format.IsItalic ? iTextSharp.text.Font.ITALIC : 0)); - // iTextSharp.text.Font font = FontFactory.GetFont(visualText.Format.Font.Name, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, - // visualText.Format.FontSize / 2F);// TODO: Don't know why this is 2.4 rather than 2. + //public class Rtf2iTextSharp : RtfVisualVisitorBase + //{ + // private IRtfDocument _RtfDoc; + // private Paragraph _MyParagraph = new Paragraph(); + // // public Rtf2iTextSharp(IRtfDocument rtfDoc, Document doc, PdfWriter writer) + // public Rtf2iTextSharp(IRtfDocument rtfDoc) + // { + // if (rtfDoc == null) + // throw new ArgumentNullException("rtfDoc"); + // _RtfDoc = rtfDoc; + // } + // public Paragraph Convert() + // { + // _MyParagraph.Clear(); + // foreach (IRtfVisual visual in _RtfDoc.VisualContent) + // visual.Visit(this); + // //_MyParagraph.SetLeading(0, 1); + // return _MyParagraph; + // } + // // ---------------------------------------------------------------------- + // protected override void DoVisitBreak(IRtfVisualBreak visualBreak) + // { + // switch (visualBreak.BreakKind) + // { + // case RtfVisualBreakKind.Line: + // break; + // case RtfVisualBreakKind.Page: + // break; + // case RtfVisualBreakKind.Paragraph: + // //_MyParagraph.Add("\r\n"); + // break; + // case RtfVisualBreakKind.Section: + // break; + // default: + // break; + // } + // //_MyParagraph.Add(string.Format("<{0}>", visualBreak.BreakKind.ToString())); + // } + // protected override void DoVisitSpecial(IRtfVisualSpecialChar visualSpecialChar) + // { + // //_MyParagraph.Add(string.Format("", visualSpecialChar.CharKind.ToString())); + // switch (visualSpecialChar.CharKind) + // { + // case RtfVisualSpecialCharKind.Bullet: + // break; + // case RtfVisualSpecialCharKind.EmDash: + // break; + // case RtfVisualSpecialCharKind.EmSpace: + // break; + // case RtfVisualSpecialCharKind.EnDash: + // break; + // case RtfVisualSpecialCharKind.EnSpace: + // break; + // case RtfVisualSpecialCharKind.LeftDoubleQuote: + // break; + // case RtfVisualSpecialCharKind.LeftSingleQuote: + // break; + // case RtfVisualSpecialCharKind.NonBreakingHyphen: + // break; + // case RtfVisualSpecialCharKind.NonBreakingSpace: + // _MyParagraph.Add(new Chunk("\u00A0")); + // break; + // case RtfVisualSpecialCharKind.OptionalHyphen: + // break; + // case RtfVisualSpecialCharKind.ParagraphNumberBegin: + // break; + // case RtfVisualSpecialCharKind.ParagraphNumberEnd: + // break; + // case RtfVisualSpecialCharKind.QmSpace: + // break; + // case RtfVisualSpecialCharKind.RightDoubleQuote: + // break; + // case RtfVisualSpecialCharKind.RightSingleQuote: + // break; + // case RtfVisualSpecialCharKind.Tabulator: + // break; + // default: + // break; + // } + // } + // //private static Dictionary _MyBaseFonts = new Dictionary(); + // //private static BaseFont GetBaseFont(string fontName) + // //{ + // // if (!_MyBaseFonts.ContainsKey(fontName)) + // // _MyBaseFonts.Add(fontName, BaseFont.CreateFont(FontFind.FullFileName(fontName), BaseFont.IDENTITY_H, BaseFont.EMBEDDED)); + // // return _MyBaseFonts[fontName]; + // //} + // //private static Dictionary _MyFonts = new Dictionary(); + // //private static iTextSharp.text.Font GetFont(string fontName, int size, int style) + // //{ + // // string key = string.Format("{0}.{1}.{2}", fontName, size, style); + // // if (!_MyFonts.ContainsKey(key)) + // // _MyFonts.Add(key, new iTextSharp.text.Font(GetBaseFont(fontName), size / 2, style)); + // // return _MyFonts[key]; + // //} + // //protected override void DoVisitText(IRtfVisualText visualText) + // //{ + // // if (visualText.Format.IsHidden) return; + // // //iTextSharp.text.Font font = GetFont(visualText.Format.Font.Name, visualText.Format.FontSize, + // // // (visualText.Format.IsBold ? iTextSharp.text.Font.BOLD : 0) + (visualText.Format.IsItalic ? iTextSharp.text.Font.ITALIC : 0)); + // // iTextSharp.text.Font font = FontFactory.GetFont(visualText.Format.Font.Name, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, + // // visualText.Format.FontSize / 2F);// TODO: Don't know why this is 2.4 rather than 2. - // font.SetStyle((visualText.Format.IsBold ? iTextSharp.text.Font.BOLD : 0) + (visualText.Format.IsItalic ? iTextSharp.text.Font.ITALIC : 0)); - // font.Color = new iTextSharp.text.Color(visualText.Format.ForegroundColor.AsDrawingColor); - // Chunk chk = new Chunk(visualText.Text, font); - // chk.SetBackground(new iTextSharp.text.Color(visualText.Format.BackgroundColor.AsDrawingColor)); - // if (visualText.Format.IsStrikeThrough) - // chk.SetUnderline(iTextSharp.text.Color.BLACK, 0, 0.05F, 0, .3F, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size - // if (visualText.Format.IsUnderline) - // chk.SetUnderline(iTextSharp.text.Color.BLACK, 0, 0.05F, 0, -.09F, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size - // if (visualText.Format.SuperScript == -1) - // chk.SetTextRise(.45F * chk.Font.Size); - // else if (visualText.Format.SuperScript == 1) - // chk.SetTextRise(-.25F * chk.Font.Size); - // //Console.WriteLine("\"RTF FontSize\",{0},{1}", visualText.Format.FontSize / 2,chk.Font.Size); + // // font.SetStyle((visualText.Format.IsBold ? iTextSharp.text.Font.BOLD : 0) + (visualText.Format.IsItalic ? iTextSharp.text.Font.ITALIC : 0)); + // // font.Color = new iTextSharp.text.Color(visualText.Format.ForegroundColor.AsDrawingColor); + // // Chunk chk = new Chunk(visualText.Text, font); + // // chk.SetBackground(new iTextSharp.text.Color(visualText.Format.BackgroundColor.AsDrawingColor)); + // // if (visualText.Format.IsStrikeThrough) + // // chk.SetUnderline(iTextSharp.text.Color.BLACK, 0, 0.05F, 0, .3F, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size + // // if (visualText.Format.IsUnderline) + // // chk.SetUnderline(iTextSharp.text.Color.BLACK, 0, 0.05F, 0, -.09F, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size + // // if (visualText.Format.SuperScript == -1) + // // chk.SetTextRise(.45F * chk.Font.Size); + // // else if (visualText.Format.SuperScript == 1) + // // chk.SetTextRise(-.25F * chk.Font.Size); + // // //Console.WriteLine("\"RTF FontSize\",{0},{1}", visualText.Format.FontSize / 2,chk.Font.Size); - // _MyParagraph.Add(chk); + // // _MyParagraph.Add(chk); - //}// DoVisitText + // //}// DoVisitText - // ---------------------------------------------------------------------- - protected override void DoVisitImage(IRtfVisualImage visualImage) - { - _MyParagraph.Add(new Chunk("")); - //WriteStartElement("rtfVisualImage"); + // // ---------------------------------------------------------------------- + // protected override void DoVisitImage(IRtfVisualImage visualImage) + // { + // _MyParagraph.Add(new Chunk("")); + // //WriteStartElement("rtfVisualImage"); - //WriteElementString("format", visualImage.Format.ToString()); - //WriteElementString("width", visualImage.Width.ToString()); - //WriteElementString("height", visualImage.Height.ToString()); - //WriteElementString("desiredWidth", visualImage.DesiredWidth.ToString()); - //WriteElementString("desiredHeight", visualImage.DesiredHeight.ToString()); - //WriteElementString("scaleWidthPercent", visualImage.ScaleWidthPercent.ToString()); - //WriteElementString("scaleHeightPercent", visualImage.ScaleHeightPercent.ToString()); - //WriteElementString("alignment", visualImage.Alignment.ToString()); + // //WriteElementString("format", visualImage.Format.ToString()); + // //WriteElementString("width", visualImage.Width.ToString()); + // //WriteElementString("height", visualImage.Height.ToString()); + // //WriteElementString("desiredWidth", visualImage.DesiredWidth.ToString()); + // //WriteElementString("desiredHeight", visualImage.DesiredHeight.ToString()); + // //WriteElementString("scaleWidthPercent", visualImage.ScaleWidthPercent.ToString()); + // //WriteElementString("scaleHeightPercent", visualImage.ScaleHeightPercent.ToString()); + // //WriteElementString("alignment", visualImage.Alignment.ToString()); - //WriteElementString("image", visualImage.ImageDataHex.ToString()); + // //WriteElementString("image", visualImage.ImageDataHex.ToString()); - //WriteEndElement(); - } // DoVisitImage + // //WriteEndElement(); + // } // DoVisitImage - } + //} public class SvgPageTotal { public SvgPageTotal(System.Drawing.Font myFont, PdfContentByte cb) @@ -833,14 +854,49 @@ namespace Volian.Svg.Library set { _MySvg = value; } } private PdfLayer _PageListLayer; + public PdfLayer PageListLayer + { + get { return _PageListLayer; } + set { _PageListLayer = value; } + } private PdfLayer _WatermarkLayer; - public SvgPageHelper(Svg mySvg, PdfLayer pageListLayer, PdfLayer watermarkLayer) + public PdfLayer WatermarkLayer + { + get { return _WatermarkLayer; } + set { _WatermarkLayer = value; } + } + private string _Watermark=string.Empty; + public string Watermark + { + get { return _Watermark; } + set { _Watermark = value.ToUpper()=="NONE"?"":value; } + } + private int _CurrentPageNumber = 0; + public int CurrentPageNumber + { + get { return _CurrentPageNumber; } + set { _CurrentPageNumber = value; } + } + public SvgPageHelper(Svg mySvg) { //_MySvg = _MyNextSvg = mySvg; _MySvg = mySvg; - _PageListLayer = pageListLayer; - _WatermarkLayer = watermarkLayer; } + public SvgPageHelper() + { + } + private int _BGPageOffset = 0; + public int BackgroundPageOffset + { + get { return _BGPageOffset; } + set { _BGPageOffset = value; } + } + //private Regex regexReplaceTokens = new Regex(@"{[^{}]}"); + protected virtual string ReplacePageListToken(Match match) + { + return string.Empty; + } + public override void OnOpenDocument(PdfWriter writer, Document document) { //base.OnOpenDocument(writer, document); @@ -851,7 +907,80 @@ namespace Volian.Svg.Library public override void OnEndPage(PdfWriter writer, Document document) { base.OnEndPage(writer, document); - PdfContentByte cb = writer.DirectContent; + DrawBackground(writer.DirectContentUnder); + DrawPageList(writer.DirectContent); + DrawWatermark(writer.DirectContentUnder); + DrawZoomOMatic(writer.DirectContent); + CurrentPageNumber++; + } + private PdfReader _BackgroundReader = null; + public PdfReader BackgroundReader + { + get + { + if (_BackgroundReader == null && _BackgroundFile != null && File.Exists(_BackgroundFile)) + _BackgroundReader = new PdfReader(_BackgroundFile); + return _BackgroundReader; + } + } + private string _BackgroundFile = null; + public string BackgroundFile + { + get { return _BackgroundFile; } + set { _BackgroundFile = value; } + } + private PdfLayer _BackgroundLayer = null; + public PdfLayer BackgroundLayer + { + get { return _BackgroundLayer; } + set { _BackgroundLayer = value; } + } + private System.Drawing.PointF _BackgroundOffset = new System.Drawing.PointF(0, 0); + public System.Drawing.PointF BackgroundOffset + { + get { return _BackgroundOffset; } + set { _BackgroundOffset = value; } + } + public int BackgroundPageCount + { + get { return BackgroundReader == null ? 0 : BackgroundReader.NumberOfPages; } + } + private PdfImportedPage GetBackgroundPage(PdfContentByte cb) + { + if (BackgroundReader == null) return null; + int page = CurrentPageNumber + BackgroundPageOffset; + if (page < 1 || page > BackgroundPageCount) return null; + return cb.PdfWriter.GetImportedPage(BackgroundReader, page); + } + private void DrawBackground(PdfContentByte cb) + { + PdfImportedPage backgroundPage = GetBackgroundPage(cb); + if (backgroundPage == null) return; + if(BackgroundLayer != null) cb.BeginLayer(BackgroundLayer); + iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(backgroundPage); + image.SetAbsolutePosition(BackgroundOffset.X,BackgroundOffset.Y); + cb.AddImage(image); + if (BackgroundLayer != null) cb.EndLayer(); + } + private void DrawZoomOMatic(PdfContentByte cb) + { + cb.SaveState(); + ZoomOMatic(cb, 18); + cb.RestoreState(); + } + private void DrawWatermark(PdfContentByte cb) + { + if (Watermark.ToLower() == "(none)" || Watermark == "") return; + cb.SaveState(); + if (_WatermarkLayer != null) cb.BeginLayer(_WatermarkLayer); + SvgWatermark myWatermark = new SvgWatermark(cb, Watermark, System.Drawing.Color.Blue, .15F); + myWatermark.SetSquareDotPattern(.7F); + myWatermark.Draw(); + if (_WatermarkLayer != null) cb.EndLayer(); + cb.RestoreState(); + } + private void DrawPageList(PdfContentByte cb) + { cb.SaveState(); if (_PageListLayer != null) cb.BeginLayer(_PageListLayer); // Do anything needed to finish off the page @@ -859,20 +988,6 @@ namespace Volian.Svg.Library //_MySvg = _MyNextSvg; // After doing to current page update the Svg for the next page. if (_PageListLayer != null) cb.EndLayer(); cb.RestoreState(); - if (_MySvg.Watermark.ToLower() != "(none)" && _MySvg.Watermark != "") - { - cb = writer.DirectContentUnder; - cb.SaveState(); - if (_WatermarkLayer != null) cb.BeginLayer(_WatermarkLayer); - SvgWatermark myWatermark = new SvgWatermark(cb, _MySvg.Watermark, System.Drawing.Color.Blue, .5F); - myWatermark.SetSquareDotPattern(.7F); - myWatermark.Draw(); - if (_WatermarkLayer != null) cb.EndLayer(); - cb.RestoreState(); - } - cb.SaveState(); - ZoomOMatic(cb, 18); - cb.RestoreState(); } private void ZoomOMatic(PdfContentByte cb, float size) { @@ -964,7 +1079,7 @@ namespace Volian.Svg.Library { _PatternPainter = _ContentByte.CreatePattern(radius * 4, radius * 2, radius * 4, radius * 2); PdfGState gState = new PdfGState(); - gState.FillOpacity = _Opacity; + gState.FillOpacity = .5f * _Opacity; _PatternPainter.SetGState(gState); _PatternPainter.SetColorFill(_Color); _PatternPainter.Rectangle(0, 0, radius, radius);