2011-04-06 14:26:28 +00:00

254 lines
8.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.factories;
using Itenso.Rtf;
using Itenso.Rtf.Parser;
using Itenso.Rtf.Interpreter;
//using Itenso.Rtf.Model;
using Itenso.Rtf.Support;
using Microsoft.Win32;
namespace Volian.Print.Library
{
public partial class PrintOverride
{
private static System.Drawing.Color _TextColor = System.Drawing.Color.Empty;
public static System.Drawing.Color TextColor
{
get { return _TextColor; }
set { _TextColor = value; }
}
public static System.Drawing.Color OverrideTextColor(System.Drawing.Color color)
{
if (_TextColor == System.Drawing.Color.Empty)
return color;
return _TextColor;
}
private static System.Drawing.Color _SvgColor = System.Drawing.Color.Empty;
public static System.Drawing.Color SvgColor
{
get { return _SvgColor; }
set { _SvgColor = value; Volian.Svg.Library.Svg.OverrideColor = value; }
}
public static System.Drawing.Color OverrideSvgColor(System.Drawing.Color color)
{
if (_SvgColor == System.Drawing.Color.Empty)
return color;
return _SvgColor;
}
private static System.Drawing.Color _BoxColor = System.Drawing.Color.Empty;
public static System.Drawing.Color BoxColor
{
get { return _BoxColor; }
set { _BoxColor = value; }
}
public static System.Drawing.Color OverrideBoxColor(System.Drawing.Color color)
{
if (_BoxColor == System.Drawing.Color.Empty)
return color;
return _BoxColor;
}
private static System.Drawing.Color _ChangeBarColor = System.Drawing.Color.Empty;
public static System.Drawing.Color ChangeBarColor
{
get { return _ChangeBarColor; }
set { _ChangeBarColor = value; }
}
public static System.Drawing.Color OverrideChangeBarColor(System.Drawing.Color color)
{
if (_ChangeBarColor == System.Drawing.Color.Empty)
return color;
return _ChangeBarColor;
}
private static System.Drawing.Color _DebugColor = System.Drawing.Color.Empty;
public static System.Drawing.Color DebugColor
{
get { return _DebugColor; }
set { _DebugColor = value; }
}
public static System.Drawing.Color OverrideDebugColor(System.Drawing.Color color)
{
if (_DebugColor == System.Drawing.Color.Empty)
return color;
return _DebugColor;
}
public static void Reset()
{
DebugColor = System.Drawing.Color.Empty;
TextColor = System.Drawing.Color.Empty;
SvgColor = System.Drawing.Color.Empty;
BoxColor = System.Drawing.Color.Empty;
ChangeBarColor = System.Drawing.Color.Empty;
}
}
public class Rtf2iTextSharp : RtfVisualVisitorBase
{
private IRtfDocument _RtfDoc;
private Paragraph _MyParagraph = new Paragraph();
private iTextSharp.text.Font _MyFont;
// 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();
_MyFont = null;
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:
_MyParagraph.Add(Chunk.NEWLINE);
break;
case RtfVisualBreakKind.Page:
break;
case RtfVisualBreakKind.Paragraph:
_MyParagraph.Add(Chunk.NEWLINE);
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("<special {0}>", 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;
}
}
protected override void DoVisitText(IRtfVisualText visualText)
{
if (visualText.Format.IsHidden) return;
iTextSharp.text.Font font = Volian.Svg.Library.VolianPdf.GetFont(visualText.Format.Font.Name, visualText.Format.FontSize,
(visualText.Format.IsBold ? iTextSharp.text.Font.BOLD : 0) +
(visualText.Format.IsItalic ? iTextSharp.text.Font.ITALIC : 0));
font.Color = new iTextSharp.text.Color(PrintOverride.OverrideTextColor(visualText.Format.ForegroundColor.AsDrawingColor));
Chunk chk = new Chunk(visualText.Text, font);
if(visualText.Format.BackgroundColor.AsDrawingColor.ToArgb() != System.Drawing.Color.White.ToArgb())
chk.SetBackground(new iTextSharp.text.Color(visualText.Format.BackgroundColor.AsDrawingColor));
if (visualText.Format.IsStrikeThrough)
chk.SetUnderline(font.Color, 0, 0.05F, 0, .3F, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size
if (visualText.Format.IsUnderline)
chk.SetUnderline(font.Color, 0, 0.05F, 0, -.131F, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size
if (visualText.Format.SuperScript > 0)
chk.SetTextRise(.25F * chk.Font.Size);
else if (visualText.Format.SuperScript < 0)
chk.SetTextRise(-.25F * chk.Font.Size);
else
chk.SetTextRise(0);
switch (visualText.Format.Alignment)
{
case RtfTextAlignment.Center:
_MyParagraph.Alignment = Element.ALIGN_CENTER;
break;
case RtfTextAlignment.Justify:
_MyParagraph.Alignment = Element.ALIGN_JUSTIFIED;
break;
case RtfTextAlignment.Left:
_MyParagraph.Alignment = Element.ALIGN_LEFT;
break;
case RtfTextAlignment.Right:
_MyParagraph.Alignment = Element.ALIGN_RIGHT;
break;
default:
break;
}
if (_MyFont == null)
{
_MyFont = font;
_MyParagraph.Font = _MyFont;
}
_MyParagraph.Add(chk);
}
private string ShowSpecialCharacters(string p)
{
StringBuilder sb = new StringBuilder();
foreach (char c in p)
{
int i = (int)c;
if (i < 20 || i > 127)
sb.Append(string.Format("<{0:x}>", i));
else
sb.Append(c);
}
return sb.ToString();
}// DoVisitText
// ----------------------------------------------------------------------
protected override void DoVisitImage(IRtfVisualImage visualImage)
{
_MyParagraph.Add(new Chunk("<Image>"));
//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("image", visualImage.ImageDataHex.ToString());
//WriteEndElement();
} // DoVisitImage
}
}