using System;
using System.Collections;
using System.Drawing;
namespace HH.WMS.Utils.Gios.Pdf
{
///
/// Describes a Flow Text Area for writing text without use Pdf positioning.
///
public class PdfFlowTextArea : PdfObject
{
private double posX,posY;
private double endX,endY;
private double startX,startY;
private double interline;
internal ArrayList Fonts;
Font ActualFont;Color ActualColor;
internal string Stream;
///
/// gets the Actual X position of the writer.
///
public double PosX
{
get
{
return this.posX;
}
}
///
/// gets the Actual Y position of the writer.
///
public double PosY
{
get
{
return this.posY;
}
}
///
/// Creates a new Flow Text Area.
///
/// the PdfArea which will contains the Text.
/// the starting Font of the writings.
/// the starting Color of the writings.
public PdfFlowTextArea(PdfArea TextArea,Font Font,Color Color)
{
this.Stream="";
this.posX=TextArea.PosX;
this.posY=TextArea.PosY;
this.startX=TextArea.PosX;
this.startY=TextArea.PosY;
this.endX=TextArea.BottomRightCornerX;
this.endY=TextArea.BottomRightCornerY;
this.interline=Font.Size*1.6;
this.Fonts=new ArrayList();
this.SetColor(Color);
this.SetFont(Font);
}
///
/// Creates a new Flow Text Area.
///
/// the PdfArea which will contains the Text.
/// the starting Font of the writings.
/// the starting Color of the writings.
/// the starting X position of the writing cursor.
/// the starting Y position of the writing cursor.
public PdfFlowTextArea(PdfArea TextArea,Font Font,Color Color,double StartX,double StartY)
{
this.Stream="";
this.posX=TextArea.PosX;
this.posY=TextArea.PosY;
this.startX=StartX;
this.startY=StartY;
this.endX=TextArea.BottomRightCornerX;
this.endY=TextArea.BottomRightCornerY;
if (this.startXthis.endX) throw new Exception("Starting Point cannot be outside the FlowTextArea.");
if (this.startYthis.endY) throw new Exception("Starting Point cannot be outside the FlowTextArea.");
this.Fonts=new ArrayList();
this.SetColor(Color);
this.SetFont(Font);
this.interline=Font.Size*1.6;
}
///
/// Creates a new Flow Text Area.
///
/// the PdfArea which will contains the Text.
/// the starting Font of the writings.
/// the starting Color of the writings.
/// the starting X position of the writing cursor.
/// the starting Y position of the writing cursor.
/// the Interline Size.
public PdfFlowTextArea(PdfArea TextArea,Font Font,Color Color,double StartX,double StartY,double Interline)
{
this.Stream="";
this.posX=TextArea.PosX;
this.posY=TextArea.PosY;
this.startX=StartX;
this.startY=StartY;
this.endX=TextArea.BottomRightCornerX;
this.endY=TextArea.BottomRightCornerY;
if (this.startXthis.endX) throw new Exception("Starting Point cannot be outside the FlowTextArea.");
if (this.startYthis.endY) throw new Exception("Starting Point cannot be outside the FlowTextArea.");
this.Fonts=new ArrayList();
this.SetColor(Color);
this.SetFont(Font);
this.interline=Interline;
}
///
/// Creates a new Flow Text Area.
///
/// the PdfArea which will contains the Text.
/// the starting Font of the writings.
/// the starting Color of the writings.
/// the Interline Size.
public PdfFlowTextArea(PdfArea TextArea,Font Font,Color Color,double Interline)
{
this.Stream="";
this.posX=TextArea.PosX;
this.posY=TextArea.PosY;
this.startX=TextArea.PosX;
this.startY=TextArea.PosY;
this.endX=TextArea.BottomRightCornerX;
this.endY=TextArea.BottomRightCornerY;
this.Fonts=new ArrayList();
this.SetColor(Color);
this.SetFont(Font);
this.interline=Interline;
}
///
/// Writes a string to the FlowTextArea width the current font style, color and size.
///
/// The string to be written.
/// The Substring rejected by the method (text that doesn't fit inside the area.
public string Write(string text)
{
if (text==null) throw new Exception("Text string cannot be null.");
return this.Write(text,ActualFont,ActualColor);
}
///
/// Writes a string to the FlowTextArea width the current font style, color and size and
/// applies a carriage return "\n"
///
/// The line to be written.
/// The Substring rejected by the method (text that doesn't fit inside the area.
public string WriteLine(string text)
{
if (text==null) throw new Exception("Text string cannot be null.");
return this.Write(text+"\n",ActualFont,ActualColor);
}
///
/// Sets the foreground color for the following writings.
///
///
public void SetColor(Color Color)
{
this.Stream+=Utility.ColorrgLine(Color);
this.ActualColor=Color;
}
///
/// Sets the font style for the following writings.
///
///
public void SetFont(Font Font)
{
if (this.ActualFont==null) this.ActualFont=Font;
Font f=new Font(Font.Name,ActualFont.Size,Font.Style);
this.ActualFont=Font;
this.Fonts.Add(Font);
this.Stream+=Utility.FontToFontLine(Font);
}
private string Write(string text,Font Font,Color Color)
{
char[] textchars=text.ToCharArray();
ArrayList words=new ArrayList();
string aWord="";
for (int index=0;indexthis.endX)
{
this.Stream+=LineToStream(Color,Font,oldLine,startLine,this.posY);
this.posY+=this.interline;
newLineWidth=WordWidth;
startLine=this.startX;
if (word!=" ") newLine=word; else newLine="";
posX=this.startX;
}
if (wordIndex==words.Count-1)
{
this.Stream+=LineToStream(Color,Font,newLine,startLine,this.posY);
this.posX=startLine+newLineWidth;
}
if (this.posY+this.interline>this.endY)
{
string s="";
for (int wordIndex2=wordIndex+1;wordIndex2>\n";
s1+="stream\n";
string s3="\nendstream\n";
s3+="endobj\n";
Byte[] part1=System.Text.ASCIIEncoding.ASCII.GetBytes(s1);
Byte[] part3=System.Text.ASCIIEncoding.ASCII.GetBytes(s3);
stream.Write(part1,0,part1.Length);
stream.Write(part2,0,part2.Length);
stream.Write(part3,0,part3.Length);
return part1.Length+part2.Length+part3.Length;
}
private string LineToStream(Color Color,Font Font,string Line,double PosX,double PosY)
{
System.Text.StringBuilder sb=new System.Text.StringBuilder();
sb.Append("1 0 0 1 ");
sb.Append(PosX.ToString("0.##").Replace(",","."));
sb.Append(" ");
sb.Append((this.PdfDocument.PH-PosY-(Font.Height*0.525)).ToString("0.##").Replace(",","."));
sb.Append(" Tm (");
sb.Append(Utility.TextEncode(Utility.TextEncode(Line)));
sb.Append(") Tj\n");
return sb.ToString();
}
}
}