Kathy 330f36250d If font is not proportional, set default font to VESymbFix to be used in pdf generation code (without this, the default was set to Helvetica)
If 1st character output for chunk, in a table cell, is a hard space (or various other special characters), set font to default (VESymbFix) font. (For HLP)
2015-07-13 11:37:56 +00:00

354 lines
12 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;
using Volian.Base.Library;
namespace Volian.Print.Library
{
public partial class PrintOverride
{
public static bool CompressSub = false;
public static bool CompressSuper = false;
public static bool CompressPropSubSup = false;
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 bool _HasIndent = false;
public bool HasIndent
{
get { return _HasIndent; }
set { _HasIndent = value; }
}
private static bool _DoingComparison = false;
public static bool DoingComparison
{
get { return Rtf2iTextSharp._DoingComparison; }
set { Rtf2iTextSharp._DoingComparison = value; }
}
private IRtfDocument _RtfDoc;
private Paragraph _MyParagraph = new Paragraph();
private iTextSharp.text.Font _MyFont;
// Allow for definition of a default font: if the font is not proportional, this gets set.
// It is needed to set the chunk's font if the first character of a cell is a hardspace (or various
// other special cases (see its use below). Without this, pdfs were using the Helvetica font,
// which is a default itextsharp font (this was causing a problem for HLP, their pdfs could not
// contain a Helvetica font when putting them in their production environment).
private iTextSharp.text.Font _DefaultFont;
public iTextSharp.text.Font DefaultFont
{
get { return _DefaultFont; }
set { _DefaultFont = value; }
}
// public Rtf2iTextSharp(IRtfDocument rtfDoc, Document doc, PdfWriter writer)
public Rtf2iTextSharp(IRtfDocument rtfDoc)
{
if (rtfDoc == null)
throw new ArgumentNullException("rtfDoc");
_RtfDoc = rtfDoc;
}
public Paragraph Convert()
{
int profileDepth = ProfileTimer.Push(">>>> Rtf2ITextSharp.Convert");
_MyParagraph.Clear();
_MyFont = null;
foreach (IRtfVisual visual in _RtfDoc.VisualContent)
{
visual.Visit(this);
}
//_MyParagraph.SetLeading(0, 1);
ProfileTimer.Pop(profileDepth);
return _MyParagraph;
}
// ----------------------------------------------------------------------
protected override void DoVisitBreak(IRtfVisualBreak visualBreak)
{
switch (visualBreak.BreakKind)
{
case RtfVisualBreakKind.Line:
Chunk ck = HasIndent ? new Chunk("".PadLeft(200)) : Chunk.NEWLINE;
_MyParagraph.Add(ck);
break;
case RtfVisualBreakKind.Page:
break;
case RtfVisualBreakKind.Paragraph:
Chunk ck1 = HasIndent ? new Chunk("".PadLeft(200)) : Chunk.NEWLINE;
_MyParagraph.Add(ck1);
break;
case RtfVisualBreakKind.Section:
break;
default:
break;
}
//_MyParagraph.Add(string.Format("<{0}>", visualBreak.BreakKind.ToString()));
}
private void AddChunk(string str, iTextSharp.text.Font font)
{
if (font == null)
_MyParagraph.Add(new Chunk(str));
else
_MyParagraph.Add(new Chunk(str, font));
}
protected override void DoVisitSpecial(IRtfVisualSpecialChar visualSpecialChar)
{
//_MyParagraph.Add(string.Format("<special {0}>", visualSpecialChar.CharKind.ToString()));
iTextSharp.text.Font activeFont = _MyFont ?? DefaultFont;
switch (visualSpecialChar.CharKind)
{
case RtfVisualSpecialCharKind.Bullet:
AddChunk("\u2022", activeFont);
break;
case RtfVisualSpecialCharKind.EmDash:
AddChunk("\u2014", activeFont);
break;
case RtfVisualSpecialCharKind.EmSpace:
AddChunk("\u2003", activeFont);
break;
case RtfVisualSpecialCharKind.EnDash:
AddChunk("\u2013", activeFont);
break;
case RtfVisualSpecialCharKind.EnSpace:
AddChunk(" ", activeFont);
break;
case RtfVisualSpecialCharKind.LeftDoubleQuote:
AddChunk("\u201C", activeFont);
break;
case RtfVisualSpecialCharKind.LeftSingleQuote:
AddChunk("\u2018", activeFont);
break;
case RtfVisualSpecialCharKind.NonBreakingHyphen:
AddChunk("\u2011", activeFont);
break;
case RtfVisualSpecialCharKind.NonBreakingSpace:
//iTextSharp.text.Font font = Volian.Svg.Library.VolianPdf.GetFont(sdf);
AddChunk("\u00A0", activeFont);
break;
case RtfVisualSpecialCharKind.OptionalHyphen:
AddChunk("\u00AD", activeFont);
break;
case RtfVisualSpecialCharKind.ParagraphNumberBegin:
break;
case RtfVisualSpecialCharKind.ParagraphNumberEnd:
break;
case RtfVisualSpecialCharKind.QmSpace:
break;
case RtfVisualSpecialCharKind.RightDoubleQuote:
AddChunk("\u201D", activeFont);
break;
case RtfVisualSpecialCharKind.RightSingleQuote:
AddChunk("\u2019", activeFont);
break;
case RtfVisualSpecialCharKind.Tabulator:
break;
default:
break;
}
}
protected override void DoVisitText(IRtfVisualText visualText)
{
int profileDepth = ProfileTimer.Push(">>>> DoVisitText");
if (visualText.Format.IsHidden)
{
ProfileTimer.Pop(profileDepth);
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));
// The following line of code was added to allow for comparisons between the 16bit and 32bit pdf files. Without
// the size change, the overlays did not match up. It seems that the 16bit font size may be inaccurate
// and the 16bit conversion to be pdf may be off.
//if (font.Familyname.StartsWith("Arial") && font.Size == 11 && DoingComparison) font.Size = 11.15f;
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)
if (visualText.Format.SuperScript < 0)
;// Don't Underline Subscripts
//if (PrintOverride.CompressSub)
// chk.SetUnderline(font.Color, 0, 0.0666F, 0, -.425F, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size
//else
// chk.SetUnderline(font.Color, 0, 0.05F, 0, -.381F, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size
else
{
// If on a compressed printed step (Leading < 12) and a larger underlined font, then set the underlining
// a little less far down the page. Without this, the underline was touching the 2nd line of text, if it existed.
// Any plant that had compressed steps with the font size/underline would have this problem. An example
// was Braidwood FSG-6, high level steps.
float yoffund = (_MyParagraph.Leading < 12 && font.Size >= 12) ? -0.06F : -0.131F;
chk.SetUnderline(font.Color, 0, 0.05F, 0, yoffund, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size
}
if (visualText.Format.SuperScript > 0)
{
if (PrintOverride.CompressPropSubSup)
{
chk.SetTextRise(.33F * chk.Font.Size);
chk.Font.Size *= .75f;
}
else
{
chk.SetTextRise(.25F * chk.Font.Size);
if (PrintOverride.CompressSuper) chk.Font.Size = 9;
}
}
else if (visualText.Format.SuperScript < 0)
{
if (PrintOverride.CompressPropSubSup)
chk.Font.Size *= .75f;
else
{
chk.SetTextRise(-.25F * chk.Font.Size);
if (PrintOverride.CompressSub) chk.Font.Size = 9;
}
}
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);
ProfileTimer.Pop(profileDepth);
}
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
}
}