Convert Hard hyphens to soft hyphens so that the PDF can be searched for hyphens.

Use the ISplitCharacter Interface to treat soft hyphens as non-breaking hyphens (only word wrap on spaces)
This commit is contained in:
Rich 2015-02-09 16:07:12 +00:00
parent 93043d7fd4
commit ad300e777a
4 changed files with 109 additions and 3 deletions

View File

@ -236,6 +236,7 @@ namespace Volian.Print.Library
myPara.MultipliedLeading = 12.0f / myPara.TotalLeading;
myPara.SpacingAfter = 8; // RHM 20120925 - Add a line to properly space text from lines.
FixHyphens(myPara);
myColumnText1.AddElement(myPara);
//myColumnText1.Canvas.SetColorFill(PrintOverride.OverrideTextColor(System.Drawing.Color.Black));
float posBefore = myColumnText1.YLine;
@ -249,6 +250,75 @@ namespace Volian.Print.Library
}
}
}
public class VlnSplitCharacter : ISplitCharacter
{
public bool IsSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck)
{
return (cc[current] == ' ');
}
public bool IsSplitCharacter(char c)
{
return (c == ' ');
}
}
public static VlnSplitCharacter mySplitter = new VlnSplitCharacter();
internal static void FixHyphens(Paragraph myPara)
{
// Find chunk with hardhyphen
int hype=-1;
Font fnt = null;
foreach (Chunk chk in myPara)
{
fnt = chk.Font;
break;
}
while ((hype = GetHyphen(myPara)) > -1)
{
Chunk chk = (Chunk)myPara[hype];
string prefix = "";
if (chk.Content == "\u2011" && hype < (myPara.Count - 1))
{
myPara.RemoveAt(hype);// Remove standalone hyphen
chk = (Chunk)myPara[hype];
prefix = "-";
}
myPara.RemoveAt(hype);
myPara.Insert(hype, new Chunk(prefix + chk.Content.Replace("\u2011", "-"), fnt));
}
//bool hasXXX = false;
//foreach (Chunk chk in myPara)
//{
// if(hasXXX)
// Console.WriteLine("0,\"{0}\"\t\"{1}\"", chk.Content, chk.Font.Familyname);
// else if (chk.Content.Contains("xxx"))
// {
// hasXXX = true;
// Console.WriteLine("1,\"{0}\"\t\"{1}\"", chk.Content, chk.Font.Familyname);
// }
// else if (chk.Content.Contains("zzz")) Console.WriteLine("3,\"{0}\"\t\"{1}\"", chk.Content, chk.Font.Familyname);
// else if (chk.Content.Contains("\u2011")) Console.WriteLine("2\"{0}\"\t\"{1}\"", chk.Content, chk.Font.Familyname);
// if (chk.Attributes == null || !chk.Attributes.ContainsKey("NoSplit"))
// {
// if (chk.Attributes == null) chk.Attributes = new System.Collections.Hashtable();
// //Console.WriteLine("Oops!");
// chk.SetSplitCharacter(mySplitter);
// chk.Attributes.Add("NoSplit", false);
// }
// else
// {
// }
//}
}
private static int GetHyphen(Paragraph myPara)
{
int index=0;
foreach (Chunk chk in myPara.Chunks)
if (chk.Content.Contains("\u2011"))
return index;
else
index++;
return -1;
}
public static float CalculateHangingIndent(string rtf)
{
float chkW = 0;
@ -666,6 +736,7 @@ namespace Volian.Print.Library
float adjustTextLocation = mult * 4; // RHM 20120925 Move text down about 1 half line from the border
myColumnText1.SetSimpleColumn(1 + left + x, top - y - h , left + x + w, 1 + top - y - hAdjust - adjustTextLocation); // 2 == Default Padding
MyPara.MultipliedLeading *= _MyPageHelper.YMultiplier;
vlnCells.FixHyphens(MyPara);
myColumnText1.AddElement(MyPara);
myColumnText1.Go();
myColumnText.Canvas.RestoreState();

View File

@ -104,7 +104,14 @@ namespace Volian.Print.Library
{
// Change the chunks to only split on spaces rather than spaces and hyphens
foreach (Chunk chk in iParagraph)
chk.SetSplitCharacter(mySplitter);
{
if (chk.Attributes == null || !chk.Attributes.ContainsKey("NoSplit"))
{
if (chk.Attributes == null) chk.Attributes = new System.Collections.Hashtable();
chk.SetSplitCharacter(mySplitter);
chk.Attributes.Add("NoSplit", false);
}
}
VlnSvgPageHelper _MyPageHelper = cb.PdfWriter.PageEvent as VlnSvgPageHelper;
PdfLayer textLayer = _MyPageHelper == null ? null : _MyPageHelper.TextLayer;
float left = x + Offset.X;

View File

@ -84,6 +84,18 @@ namespace Volian.Print.Library
get { return _Rtf; }
set { _Rtf = value; }
}
public class VlnSplitCharacter : ISplitCharacter
{
public bool IsSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck)
{
return (cc[current] == ' ');
}
public bool IsSplitCharacter(char c)
{
return (c == ' ');
}
}
public static VlnSplitCharacter mySplitter = new VlnSplitCharacter();
iTextSharp.text.Paragraph _IParagraph;
public iTextSharp.text.Paragraph IParagraph
{
@ -112,7 +124,7 @@ namespace Volian.Print.Library
get
{
int profileDepth = ProfileTimer.Push(">>>> vlnPrintObject.Height");
if (_Height == 0)
if (_Height == 0)
_Height = GetParagraphHeight(MyContentByte, IParagraph, Width);
ProfileTimer.Pop(profileDepth);
return _Height;
@ -377,6 +389,16 @@ namespace Volian.Print.Library
para.IndentationLeft = chkW;
para.FirstLineIndent = -chkW;
}
// Change the chunks to only split on spaces rather than spaces and hyphens
foreach (Chunk chk in para)
{
if (chk.Attributes==null || !chk.Attributes.ContainsKey("NoSplit"))
{
if (chk.Attributes == null) chk.Attributes = new System.Collections.Hashtable();
chk.SetSplitCharacter(mySplitter);
chk.Attributes.Add("NoSplit", false);
}
}
return para;
}
public static float CalculateHangingIndent(string rtf)

View File

@ -682,11 +682,17 @@ namespace Volian.Svg.Library
ColumnText ct = new ColumnText(cb);
float x = (myParent is SvgGroup && (myParent as SvgGroup).Description.ToUpper() == "ABSOLUTE") ? scale.AbsX(X): scale.X(X);
float w = 0; // chk.GetWidthPoint();
// Change the chunks to only split on spaces rather than spaces and hyphens
foreach (Chunk chk in ph.Chunks)
{
w += chk.GetWidthPoint();
// Change the chunks to only split on spaces rather than spaces and hyphens
chk.SetSplitCharacter(mySplitter);
if (chk.Attributes == null || !chk.Attributes.ContainsKey("NoSplit"))
{
if (chk.Attributes == null) chk.Attributes = new Hashtable();
chk.SetSplitCharacter(mySplitter);
chk.Attributes.Add("NoSplit", false);
}
}
switch (Justify)
{