This commit is contained in:
244
PROMS/Volian.Print.Library/Rtf2iTextSharp.cs
Normal file
244
PROMS/Volian.Print.Library/Rtf2iTextSharp.cs
Normal file
@@ -0,0 +1,244 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user