490 lines
18 KiB
C#
490 lines
18 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;
|
|
using System.Text.RegularExpressions;
|
|
|
|
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; }
|
|
}
|
|
private string _MyDebugID = null;
|
|
public string MyDebugID
|
|
{
|
|
get { return _MyDebugID; }
|
|
set { _MyDebugID = 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)) : new Chunk("".PadLeft(200));
|
|
_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;
|
|
}
|
|
}
|
|
private bool ContainsAllSymbols(string p)
|
|
{
|
|
foreach (char c in p)
|
|
if (c <= '\x7F') return false;
|
|
return true;
|
|
}
|
|
private bool ContainsAnySymbols(string p)
|
|
{
|
|
foreach (char c in p)
|
|
if (c > '\x7F' && c != '\xA0' && c != '\xB0') return true;
|
|
return false;
|
|
}
|
|
// The funcction below was used for the first few lines of DoVisitText which have been commented-out
|
|
//private object FixText(string str)
|
|
//{
|
|
// StringBuilder sb = new StringBuilder();
|
|
// foreach (char c in str)
|
|
// if (c < ' ' || c > '\xFF')
|
|
// sb.Append(string.Format("[{0}]", (int)c));
|
|
// else
|
|
// sb.Append(c);
|
|
// return sb.ToString();
|
|
//}
|
|
protected override void DoVisitText(IRtfVisualText visualText)
|
|
{
|
|
//Code to find text (non-symbol) being output with a symbol font
|
|
//if(visualText.Format.Font.Name=="VESymbFix" && visualText.Text.Length > 1)
|
|
//{
|
|
// string s = visualText.Text;
|
|
// s=s.Replace("a","").Replace("b","");
|
|
// if(s!=visualText.Text)
|
|
// Console.WriteLine("{0}-{1}-{2}-{3}", visualText.Format.Font.Name, (int)(visualText.Text[0]),visualText.Text.Length,FixText(visualText.Text));
|
|
//}
|
|
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;
|
|
iTextSharp.text.pdf.FontSelector fs = new FontSelector();
|
|
// DEBUG
|
|
//if (visualText.Format.Font.Name != "VESymbFix" && ContainsAnySymbols(visualText.Text))
|
|
//{
|
|
// Console.WriteLine("Font: {0} TEXT: {1}", visualText.Format.Font.Name, ShowSpecialCharacters(visualText.Text));
|
|
//}
|
|
if (visualText.Format.Font.Name == "VESymbFix" && ContainsAllSymbols(visualText.Text))
|
|
{
|
|
fs.AddFont(font);
|
|
// if the symbol character cannot be found in VESymbFix then check/use the Consolas font
|
|
// We are using the VESymbFix font first because not all of the symbols that we use are in the Consolas font
|
|
// The Consolas font will give us other symbols that we don't have in VESymbFix (ex the Omega)
|
|
fs.AddFont(FontFactory.GetFont("Consolas", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, (visualText.Format.FontSize * 1.1F) / 2,
|
|
(visualText.Format.IsBold ? iTextSharp.text.Font.BOLD : 0) +
|
|
(visualText.Format.IsItalic ? iTextSharp.text.Font.ITALIC : 0), font.Color));
|
|
// added the FreeMono font because when were was a backslash symbol (\u9568?) it was not found and thus removed from the chunk B2014-108 backslash in table
|
|
fs.AddFont(FontFactory.GetFont("FreeMono", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, (visualText.Format.FontSize * 1.1F) / 2,
|
|
(visualText.Format.IsBold ? iTextSharp.text.Font.BOLD : 0) +
|
|
(visualText.Format.IsItalic ? iTextSharp.text.Font.ITALIC : 0), font.Color));
|
|
Phrase ph = fs.Process(visualText.Text);
|
|
foreach (Chunk chk in ph.Chunks)
|
|
{
|
|
AdjustChunk(visualText, font, chk);
|
|
_MyParagraph.Add(chk);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// The bullet character for some unknown reason was entered in a text font rather than a symbol font
|
|
// switch from x25cf ot x2022 moves to a valid bullet character in the text font
|
|
// B2017-171 Mcguire reported their bullet substeps print a smaller bullet than before
|
|
if (font.Familyname == "Prestige Elite Tall")
|
|
ProcessMeans(visualText, visualText.Text.Replace("\u25cf", "\u2022"), font);
|
|
else
|
|
ProcessMeans(visualText, visualText.Text, font);
|
|
}
|
|
ProfileTimer.Pop(profileDepth);
|
|
}
|
|
/// <summary>
|
|
/// Method to process means from text. Draws a line above the content of the range included in the
|
|
/// square brackets. [Mean X]
|
|
/// Handles everything except font style changes within the expression.
|
|
/// </summary>
|
|
/// <param name="visualText"></param>
|
|
/// <param name="txt"></param>
|
|
/// <param name="font"></param>
|
|
private void ProcessMeans(IRtfVisualText visualText, string txt,iTextSharp.text.Font font)
|
|
{
|
|
Match m = Regex.Match(txt, "(.*)[[]Mean (.+)[]](.*)", RegexOptions.IgnoreCase);
|
|
Chunk chk;
|
|
if (m.Groups.Count == 4) // If the text contains [Mean ...] then follow the code to add a line above the text.
|
|
{
|
|
//The first group is the text before the last [Mean ...]
|
|
ProcessMeans(visualText, m.Groups[1].Value, font);// Recursively process the first group.
|
|
// The second group is the text within the [Mean ...] range
|
|
chk = new Chunk(m.Groups[2].Value, font);
|
|
float offset = .8f;// This is just above the character.
|
|
// If the content is lower case and does not have ascenders then move the line down.
|
|
if (Regex.IsMatch(m.Groups[2].Value, "^[acgmnopqrsuvwxyz]+$"))
|
|
offset = .6F;
|
|
//PrintOverride.CompressPropSubSup = true;
|
|
if (visualText.Format.SuperScript < 0)// Adjust line for subscript
|
|
{
|
|
offset -= .2f;
|
|
if (PrintOverride.CompressPropSubSup) offset += .2f;// Adjust line for shrinking font
|
|
}
|
|
if (visualText.Format.SuperScript > 0)//Adjust line for superscript
|
|
{
|
|
offset += .3f;
|
|
if (PrintOverride.CompressPropSubSup) offset += .2f;// Adjust line for shrinking font
|
|
}
|
|
// Draw the line
|
|
chk.SetUnderline(font.Color, 0, .05F, 0, offset, PdfContentByte.LINE_CAP_ROUND);
|
|
AdjustChunk(visualText, font, chk);
|
|
_MyParagraph.Add(chk);
|
|
// output the text following the [Mean ...]
|
|
chk = new Chunk(m.Groups[3].Value, font);
|
|
AdjustChunk(visualText, font, chk);
|
|
_MyParagraph.Add(chk);
|
|
}
|
|
else
|
|
{
|
|
chk = new Chunk(txt, font);
|
|
AdjustChunk(visualText, font, chk);
|
|
_MyParagraph.Add(chk);
|
|
}
|
|
}
|
|
private void AdjustChunk(IRtfVisualText visualText, iTextSharp.text.Font font, Chunk chk)
|
|
{
|
|
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)
|
|
{
|
|
if (PrintOverride.CompressSub)
|
|
{
|
|
float yoffundx = (_MyParagraph.Leading < 12 && font.Size >= 12) ? -0.06F : -0.18F;
|
|
chk.SetUnderline(font.Color, 0, 0.07F, 0, yoffundx, 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 (visualText.Format.SuperScript > 0 && PrintOverride.CompressSuper)
|
|
{
|
|
float yoffundx = (_MyParagraph.Leading < 12 && font.Size >= 12) ? -0.06F : -0.18F;
|
|
chk.SetUnderline(font.Color, 0, 0.07F, 0, yoffundx, PdfContentByte.LINE_CAP_ROUND);
|
|
}
|
|
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
|
|
{
|
|
// if the subscript is not compressed or if it is compress but not underlined, then move it down
|
|
if (!PrintOverride.CompressSub || !visualText.Format.IsUnderline) 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;
|
|
}
|
|
}
|
|
|
|
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>"));
|
|
//DateTime dt1 = DateTime.Now;
|
|
//System.Drawing.Image img2 = visualImage.ImageForDrawing;
|
|
//Console.WriteLine("1 Time Span {0}", TimeSpan.FromTicks(DateTime.Now.Ticks - dt1.Ticks).TotalMilliseconds);
|
|
//img2.Save(@"c:\datacvrt\x.png");
|
|
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
|
|
|
//Console.WriteLine("2 Time Span {0}", TimeSpan.FromTicks(DateTime.Now.Ticks - dt1.Ticks).TotalMilliseconds);
|
|
//Console.WriteLine("Size {0}", visualImage.ImageForDrawing.Size);
|
|
visualImage.ImageForDrawing.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
//Console.WriteLine("3 Time Span {0}", TimeSpan.FromTicks(DateTime.Now.Ticks - dt1.Ticks).TotalMilliseconds);
|
|
ms.Seek(0, System.IO.SeekOrigin.Begin);
|
|
//Console.WriteLine("4 Time Span {0}", TimeSpan.FromTicks(DateTime.Now.Ticks - dt1.Ticks).TotalMilliseconds);
|
|
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(ms);
|
|
//Console.WriteLine("5 Time Span {0}", TimeSpan.FromTicks(DateTime.Now.Ticks - dt1.Ticks).TotalMilliseconds);
|
|
_MyParagraph.Add(img);
|
|
//Console.WriteLine("6 Time Span {0}", TimeSpan.FromTicks(DateTime.Now.Ticks - dt1.Ticks).TotalMilliseconds);
|
|
} // DoVisitImage
|
|
|
|
}
|
|
}
|