288 lines
10 KiB
C#
288 lines
10 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()
|
|
{
|
|
_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;
|
|
}
|
|
}
|
|
//public static iTextSharp.text.Font GetFont(string fontName)
|
|
//{
|
|
// RegisterFont(fontName);
|
|
// //if(fontName.Contains("Sym"))
|
|
// return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
|
|
// //else
|
|
// // return iTextSharp.text.FontFactory.GetFont(fontName,BaseFont.CP1252 ,BaseFont.EMBEDDED);
|
|
//}
|
|
|
|
//public static iTextSharp.text.Font GetFont(string fontName)
|
|
//{
|
|
// RegisterFont(fontName);
|
|
// return iTextSharp.text.FontFactory.GetFont(fontName);
|
|
//}
|
|
public static void RegisterFont(string fontName)
|
|
{
|
|
if (!iTextSharp.text.FontFactory.IsRegistered(fontName))
|
|
{
|
|
foreach (string name in FontKey.GetValueNames())
|
|
{
|
|
if (name.StartsWith(fontName))
|
|
{
|
|
string fontFile = (string)FontKey.GetValue(name);
|
|
iTextSharp.text.FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFolder + "\\" + fontFile);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
private static RegistryKey _FontKey = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows NT").OpenSubKey("CurrentVersion").OpenSubKey("Fonts");
|
|
public static RegistryKey FontKey
|
|
{ get { return _FontKey; } }
|
|
private static string _FontFolder = (String)Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("Shell Folders").GetValue("Fonts");
|
|
public static string FontFolder
|
|
{ get { return _FontFolder; } }
|
|
private static iTextSharp.text.Font GetFont(string fontName, int size, int style)
|
|
{
|
|
RegisterFont(fontName);
|
|
return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, size / 2, style);
|
|
}
|
|
public static iTextSharp.text.Font GetFont(System.Drawing.Font font) // fontName, int size, int style)
|
|
{
|
|
Rtf2iTextSharp.RegisterFont(font.Name);
|
|
int style = (font.Bold ? iTextSharp.text.Font.BOLD : 0) + (font.Italic ? iTextSharp.text.Font.ITALIC : 0);
|
|
return iTextSharp.text.FontFactory.GetFont(font.Name, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, font.Size, style);
|
|
}
|
|
protected override void DoVisitText(IRtfVisualText visualText)
|
|
{
|
|
//Console.WriteLine("'{0}' {1} {2} {3} {4}", ShowSpecialCharacters(visualText.Text), visualText.Kind, visualText.Format.Font.Name, visualText.Format.FontSize, visualText.Format.SuperScript);
|
|
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));
|
|
//Console.WriteLine("after font = {0} {1} {2}", font.Familyname, font.Size, font.Style);
|
|
//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(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(.45F * chk.Font.Size);
|
|
else if (visualText.Format.SuperScript < 0)
|
|
chk.SetTextRise(-.25F * chk.Font.Size);
|
|
//else
|
|
// chk.SetTextRise(0);
|
|
//Console.WriteLine("\"RTF FontSize\",{0},{1}", visualText.Format.FontSize / 2,chk.Font.Size);
|
|
|
|
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
|
|
|
|
}
|
|
}
|