Table shrinking support

This commit is contained in:
Rich
2015-05-27 18:48:14 +00:00
parent cf79062b90
commit 37e2227919
5 changed files with 430 additions and 43 deletions

View File

@@ -11,13 +11,87 @@ using Volian.Print.Library;
using Volian.Controls.Library;
using iTextSharp.text;
using iTextSharp.text.pdf;
using VEPROMS.CSLA.Library;
namespace Volian.Print.Library
{
public class vlnTable
#region Settings // RHM20150429 - Table Scrunch
public partial class vlnTable
{
private string _DebugInfo = null; // RHM20150429 - Table Scrunch
public string DebugInfo
{
get { return _DebugInfo; }
set { _DebugInfo = value; }
}
private int _ItemID = 0;
public int ItemID
{
get { return _ItemID; }
set { _ItemID = value; }
}
private bool _ShowDetails = false;
public bool ShowDetails
{
get { return _ShowDetails; }
set { _ShowDetails = value; }
}
private bool _IsTooBig = false;
public bool IsTooBig
{
get { return _IsTooBig; }
set { _IsTooBig = value; }
}
}
public partial class vlnCells
{
private static float YAdjust_HContent = 4; // 4
private float YAdjust_SpacingAfter
{
get
{
return 8-8*MyTable.Adjustment;//0; // 8
}
}
}
public partial class vlnCell
{
public bool ShowDetails
{ get { return MyTable.ShowDetails; } }
private static float YAdjust_h = 0; // 0
private float YAdjust_TextLocation
{
get
{
return 4 - 4 * MyTable.Adjustment; ;//0; // 4
}
}
}
//public partial class vlnTable
//{
// private static float YAdjust_RowTop = 0;//8; // 0
//}
//public partial class vlnCells
//{
// private static float YAdjust_HContent = 4; // 4
// private static float YAdjust_SpacingAfter = 8;//0; // 8
//}
//public partial class vlnCell
//{
// private static float YAdjust_h = 0; // 0
// private static float YAdjust_TextLocation = 4;//0; // 4
//}
#endregion
public partial class vlnTable
{
#region Properties
private float[] _HContents;// RHM20150525 - Table Scrunch
public float[] HContents
{
get { return _HContents; }
set { _HContents = value; }
}
private float[] _RowTop;
public float[] RowTop
{
@@ -99,6 +173,106 @@ namespace Volian.Print.Library
{
get { return (LineThicknessForThick - LineThicknessForDouble) / 2; }//LineThicknessForThick - 2 * LineThicknessForDouble; }
}
private float _TotalContentHeight = 0;// RHM20150525 - Table Scrunc
public float TotalContentHeight
{
get
{
if (_TotalContentHeight == 0)
{
foreach (float h in HContents)
_TotalContentHeight += h;
}
return _TotalContentHeight;
}
}
public float SpaceAvailable// RHM20150525 - Table Scrunc
{
get { return TotalContentHeight ==0 ? 0 : RowTop[RowTop.Length - 1] - TotalContentHeight; }
}
private float _TooBig;// RHM20150525 - Table Scrunc
public float TooBig
{
get { return _TooBig; }
set
{
_TooBig = value;
//_Adjustment = Math.Min(1F, (_TooBig / (RowTop.Length - 1)) / 12);
//if (ShowDetails) Console.WriteLine("TooBig\t{0}\t{1}", value, _Adjustment);
//if(ShowDetails) Console.WriteLine("Before RowTop={0}", RowTop[RowTop.Length-1] );
float extra=(TooBig < SpaceAvailable)?(SpaceAvailable - TooBig) / HContents.Length:0;
for (int r = 0; r < RowTop.Length - 1; r++)
RowTop[r + 1] = RowTop[r] + HContents[r] + extra;
//if(ShowDetails) Console.WriteLine("After RowTop={0}", RowTop[RowTop.Length-1] );
}
}
private float _Adjustment = 0;// RHM20150525 - Table Scrunc
public float Adjustment
{
get { return _Adjustment; }
set { _Adjustment = value; }
}
//private float _YAdjust_RowTop = 0;//8; // 0
public float YAdjust_RowTop
{
get
{
return _Adjustment * 12;
}
}
private void DrawRuler(PdfContentByte cb, float x, float yTop, float height)// RHM20150525 - Table Scrunc
{
VlnSvgPageHelper myPageHelper = cb.PdfWriter.PageEvent as VlnSvgPageHelper;
if (myPageHelper.DebugLayer != null)
{
cb.BeginLayer(myPageHelper.DebugLayer);
cb.SaveState();
cb.SetLineWidth(.1F);
cb.SetColorStroke(new Color(System.Drawing.Color.Orange));
float yBottom = yTop - height;
cb.MoveTo(x, yTop);
cb.LineTo(x, yBottom);
int i = 0;
for (float y = yTop; y >= yBottom; y -= 10)
{
float w = 10;
if (i % 10 == 0) w = 30;
else if (i % 5 == 0) w = 20;
cb.SetLineWidth(w / 30);
i++;
cb.MoveTo(x + w, y);
cb.LineTo(x, y);
cb.Stroke();
}
i = 0;
cb.Stroke();
cb.RestoreState();
cb.EndLayer();
}
}
private void ShowRowTops(string location)// RHM20150525 - Table Scrunc
{
if (!ShowDetails) return;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < RowTop.Length; i++)
sb.Append("\t" + RowTop[i].ToString());
Console.WriteLine(location + "\t" + sb.ToString());
}
private void EndSetup()// RHM20150525 - Table Scrunc
{
if (ShowDetails) ShowRowTops("vlnTable.ctor.2");
if (ShowDetails) Console.WriteLine("^^^^^^vlnTable============{0}===============", ItemID);
}
private void SetupDebug(VlnFlexGrid myFlexGrid)// RHM20150525 - Table Scrunc
{
//ShowDetails = myFlexGrid.GetMyItemInfo().InList(38329);
//if(ShowDetails) Console.WriteLine("vvvvvvlnTable============{0}===============", ItemID);
ItemInfo myItemInfo = myFlexGrid.GetMyItemInfo();
ItemID = myItemInfo.ItemID;
DebugInfo = string.Format("DebugID = {0}, ID={1} Type={2} TypeName='{3}' StepLevel={4} ShortPath={5} Width={6} Left={7} YOffset={8}",
0, myItemInfo.ItemID, myItemInfo.FormatStepType, myItemInfo.FormatStepData == null ? "NoStepData" : myItemInfo.FormatStepData.Type,
myItemInfo.StepLevel, myItemInfo.ShortPath, Width, XOffset, YOffset);
}
#endregion
#region Constructors
public vlnTable(VlnFlexGrid myFlexGrid, iTextSharp.text.pdf.PdfContentByte myContentByte)
@@ -106,13 +280,16 @@ namespace Volian.Print.Library
MyFlexGrid = myFlexGrid;
MyContentByte = myContentByte;
InitializeSizes();
SetupDebug(myFlexGrid);// RHM20150525 - Table Scrunc
MyCells = new vlnCells(this, MyFlexGrid, MyContentByte);
EndSetup();// RHM20150525 - Table Scrunc
}
#endregion
#region Private Methods
private void InitializeSizes()
{
RowTop = new float[MyFlexGrid.Rows.Count + 1];
HContents = new float[MyFlexGrid.Rows.Count];// RHM20150525 - Table Scrunc
RowTop[0] = 0;
for (int r = 0; r < MyFlexGrid.Rows.Count; r++)
RowTop[r + 1] = RowTop[r] + 72 * (MyFlexGrid.Rows[r].Height == -1
@@ -120,7 +297,7 @@ namespace Volian.Print.Library
ColLeft = new float[MyFlexGrid.Cols.Count + 1];
ColLeft[0] = 0;
for (int c = 0; c < MyFlexGrid.Cols.Count; c++)
ColLeft[c + 1] = ColLeft[c] + 72 * (MyFlexGrid.Cols[c].Width == -1
ColLeft[c + 1] = 2 + ColLeft[c] + 72 * (MyFlexGrid.Cols[c].Width == -1 // RHM20150429 - Table Scrunch
? MyFlexGrid.Cols.DefaultSize : MyFlexGrid.Cols[c].Width) / (float)MyFlexGrid.DPI;
}
#endregion
@@ -134,9 +311,21 @@ namespace Volian.Print.Library
public void ToPdf(iTextSharp.text.pdf.ColumnText myColumnText, float left, float top)
{
//ZoomGrid(myColumnText, left, top);
VlnSvgPageHelper myPageHelper = myColumnText.Canvas.PdfWriter.PageEvent as VlnSvgPageHelper;
if(ShowDetails) Console.WriteLine("vvvvvvToPdf============{0}===============", ItemID);// RHM20150525 - Table Scrunc
ShowRowTops("ToPDF1");
PdfContentByte cb = myColumnText.Canvas; // RHM20150429 - Table Scrunch
VlnSvgPageHelper myPageHelper = cb.PdfWriter.PageEvent as VlnSvgPageHelper;
string myLPI = myPageHelper.YMultiplier == 1.0F ? ",6LPI" : ",7LPI";
Rtf2Pdf.DrawPdfDebug(myColumnText.Canvas, left-2, top+1, left + Width, top-myPageHelper.YMultiplier *(Height+1),
DebugInfo+", Height= " + (myPageHelper.YMultiplier * Height).ToString() + myLPI, 0);
PdfLayer textLayer = myPageHelper == null ? null : myPageHelper.TextLayer; // RHM20150429 - Table Scrunch
if (textLayer != null) cb.BeginLayer(textLayer);
myPageHelper.AddGap(top, top - Height, left, left + Width);
MyCells.ToPdf(myColumnText, left, top);
DrawRuler(myColumnText.Canvas, left + Width, top, myPageHelper.YMultiplier * (Height + 1)); // RHM20150429 - Table Scrunch
if (textLayer != null) cb.EndLayer();
ShowRowTops("ToPDF2");
if (ShowDetails) Console.WriteLine("^^^^^^ToPdf============{0}===============", ItemID);
}
private void ZoomGrid(iTextSharp.text.pdf.ColumnText myColumnText, float left, float top)
{
@@ -145,7 +334,7 @@ namespace Volian.Print.Library
}
#endregion
}
public class vlnCells : List<vlnCell>
public partial class vlnCells : List<vlnCell> // RHM20150429 - Table Scrunch
{
#region Properties
private VlnFlexGrid _MyFlexGrid;
@@ -160,6 +349,8 @@ namespace Volian.Print.Library
get { return _MyTable; }
set { _MyTable = value; }
}
public bool ShowDetails// RHM20150525 - Table Scrunc
{ get { return MyTable.ShowDetails; }}
private iTextSharp.text.pdf.PdfContentByte _MyContentByte;
public iTextSharp.text.pdf.PdfContentByte MyContentByte
{
@@ -195,6 +386,7 @@ namespace Volian.Print.Library
// Walk through
for (int r = 0; r < MyFlexGrid.Rows.Count; r++)
{
float hMax = 12; // RHM20150429 - Table Scrunch
for (int c = 0; c < MyFlexGrid.Cols.Count; c++)
{
CellRange cr = MyFlexGrid.GetMergedRange(r, c);
@@ -212,7 +404,7 @@ namespace Volian.Print.Library
str = dt.StartText;
str = PreProcessRTF(w, str);
iTextSharp.text.Paragraph myPara = RtfToParagraph(str);
myColumnText1.SetSimpleColumn(0, 0, w - 2, MyContentByte.PdfDocument.PageSize.Top); // Padding = 4
myColumnText1.SetSimpleColumn(0, 0, w-2, MyContentByte.PdfDocument.PageSize.Top); // Padding = 4
if (str.Contains(@"\'05"))
{
// if there is a hanging indent, the iTextSharp paragraph properties must be set
@@ -234,22 +426,78 @@ namespace Volian.Print.Library
// once there is valid value for TotalLeading (from previous line), you can use it to
// calculate a MultipliedLeading to give 6 LPI (12 points)
myPara.MultipliedLeading = 12.0f / myPara.TotalLeading;
myPara.SpacingAfter = 8; // RHM 20120925 - Add a line to properly space text from lines.
if(Rtf2Pdf.GetTableScrunchingStatus(TableScrunching.Phase2)) // RHM20150429 - Table Scrunch
myPara.SpacingAfter = 0;// YAdjust_SpacingAfter; // RHM 20120925 - Add a line to properly space text from lines.
FixHyphens(myPara, MyTable);
if (Rtf2Pdf.GetTableScrunchingStatus(TableScrunching.Phase3))
TrimNewlines(myPara); // RHM20150429 - Table Scrunch
myColumnText1.AddElement(myPara);
//myColumnText1.Canvas.SetColorFill(PrintOverride.OverrideTextColor(System.Drawing.Color.Black));
float posBefore = myColumnText1.YLine;
int status = myColumnText1.Go(true);
float posAfter = myColumnText1.YLine;
float hContent = 4 + posBefore - posAfter;
if (Rtf2Pdf.GetTableScrunchingStatus(TableScrunching.Phase4))// RHM20150525 - Table Scrunc
{
hContent = 0 + posBefore - posAfter;//YAdjust_HContent + posBefore - posAfter;
// if (myPara.Chunks.Count > 0 && myPara.Chunks[myPara.Chunks.Count - 1] == "\n")
// hContent -= 12;
}
//if(ShowDetails) Console.WriteLine("{0}\t{1}\t{2}",r,c, hContent/12);
if (cr.r1 == cr.r2)
hMax = Math.Max(hMax, hContent);
else
hMax = Math.Max(hMax, hContent - (cr.r2 - cr.r1) * _SixLinesPerInch);
if (hContent > h)
{ // RHM20150429 - Table Scrunch
//if (ShowDetails) Console.WriteLine("Less {0},{1},{2},{3},{4},{5}", r, c, posBefore, posAfter, hContent, hMax);
MyTable.AdjustRowTop(cr.r1, cr.r2, hContent);
Add(new vlnCell(cr.r1, cr.c1, cr.r2, cr.c2, MyTable, myPara, hContent));
} // RHM20150429 - Table Scrunch
if (Rtf2Pdf.GetTableScrunchingStatus(TableScrunching.Phase5))
{
//if (ShowDetails) Console.WriteLine("Greater{0},{1},{2},{3},{4},{5}", r, c, posBefore, posAfter, hContent, hMax);
//if (c == MyFlexGrid.Cols.Count - 1)
//MyTable.AdjustRowTop(cr.r1, cr.r2, hMax + 6);
MyTable.HContents[cr.r2] = hMax+2;
}
Add(new vlnCell(cr.r1, cr.c1, cr.r2, cr.c2, MyTable, myPara,hContent));
}
}
}
}
private void ShowChunks(System.Collections.ArrayList chunks) // RHM20150429 - Table Scrunch
{
StringBuilder sb = new StringBuilder();
foreach (Chunk chk in chunks)
{
sb.Append(BuildChunks(chk.Content));
}
//if (ShowDetails) Console.WriteLine("Chunks = '{0}'", sb.ToString());
}
private string BuildChunks(string str)
{
StringBuilder sb = new StringBuilder();
foreach(char c in str)
{
if (((int)c) < 0x20 || ((int)c) > 0x7F)
sb.Append(string.Format("<{0:X4}>",(int)c));
else
sb.Append(c);
}
return "{" + sb.ToString() + "}";
}
private void TrimNewlines(Paragraph myPara)
{
while (myPara.Chunks.Count > 0 && ParaEndsWithNewLine(myPara))
myPara.RemoveAt(myPara.Chunks.Count - 1);
}
private bool ParaEndsWithNewLine(Paragraph myPara)
{
Chunk chk = myPara.Chunks[myPara.Chunks.Count - 1] as Chunk;
if (chk.Content == "\n") return true;
return false;
}
public class VlnSplitCharacter : ISplitCharacter
{
public bool IsSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck)
@@ -418,7 +666,7 @@ namespace Volian.Print.Library
}
#endregion
}
public class vlnCell
public partial class vlnCell // RHM20150429 - Table Scrunch
{
#region Properties
private vlnTable _MyTable;
@@ -753,15 +1001,32 @@ namespace Volian.Print.Library
float w = MyTable.ColLeft[c2 + 1] - x;
float y = mult * MyTable.RowTop[r1];
float h = mult * MyTable.RowTop[r2 + 1] - y;
if (Rtf2Pdf.GetTableScrunchingStatus(TableScrunching.Phase6))// RHM20150525 - Table Scrunc
{
h = YAdjust_h + mult * MyTable.RowTop[r2 + 1] - y;
}
BordersToPdf(myColumnText, left, top, x, w, y, h );
float hAdjust = VeritcalTextAlignment(h);
float hAdjust = VeritcalTextAlignment(h,_MyPageHelper.YMultiplier);
iTextSharp.text.pdf.ColumnText myColumnText1 = new iTextSharp.text.pdf.ColumnText(myColumnText.Canvas);
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
if (Rtf2Pdf.GetTableScrunchingStatus(TableScrunching.Phase7))// RHM20150525 - Table Scrunc
{
adjustTextLocation = mult * 0;
if (mult != 1F) adjustTextLocation=1;
}
myColumnText1.SetSimpleColumn(1 + left + x, top - y - h , left + x + w - 2, 1 + top - y - hAdjust - adjustTextLocation); // 2 == Default Padding
if (ShowDetails) Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t\"{9}\""
, r1, c1, hAdjust, adjustTextLocation, y, h, top, 1 + top - y - hAdjust - adjustTextLocation,
_MyPageHelper.YMultiplier, MyPara.Content.Substring(0, Math.Min(20, MyPara.Content.Length)));
MyPara.MultipliedLeading *= _MyPageHelper.YMultiplier;
vlnCells.FixHyphens(MyPara, MyTable);
myColumnText1.AddElement(MyPara);
float posBefore = myColumnText1.YLine; // RHM20150429 - Table Scrunch
myColumnText1.Go();
float posAfter = myColumnText1.YLine; // RHM20150429 - Table Scrunch
if(ShowDetails)Console.WriteLine("ToPDF posBefore,posAfter,difference={0},{1},{2},{3}",
posBefore, posAfter, posBefore - posAfter,w);
myColumnText.Canvas.RestoreState();
}
private static float _SixLinesPerInch = 12; // twips
@@ -790,7 +1055,7 @@ namespace Volian.Print.Library
}
#endregion
#region Private Text Methods
private float VeritcalTextAlignment(float h)
private float VeritcalTextAlignment(float h, float mult)
{
float hAdjust = 0;
//CellRange cr = MyTable.MyFlexGrid.GetCellRange(r1, c1, r2, c2);
@@ -802,13 +1067,15 @@ namespace Volian.Print.Library
case TextAlignEnum.GeneralBottom:
case TextAlignEnum.LeftBottom:
case TextAlignEnum.RightBottom:
hAdjust = h - HContent;
hAdjust = Math.Max(0,h - (mult * HContent) )-1;
Console.WriteLine("\"Bottom\",{0},{1},{2}", h, HContent, hAdjust);
break;
case TextAlignEnum.CenterCenter:
case TextAlignEnum.GeneralCenter:
case TextAlignEnum.LeftCenter:
case TextAlignEnum.RightCenter:
hAdjust = (h - HContent) / 2;
hAdjust = Math.Max(0,(h - (mult * HContent)) / 2)-1;
Console.WriteLine("\"Middle\",{0},{1},{2}", h, HContent, hAdjust);
break;
default:
break;