2010-05-19 15:01:06 +00:00

136 lines
4.6 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 System.IO;
namespace Volian.Print.Library
{
public class Rtf2Pdf
{
private string _Rtf;
public string Rtf
{
get { return _Rtf; }
set { _Rtf = value; }
}
private string _FileName;
public string FileName
{
get { return _FileName; }
set { _FileName = value; }
}
public Rtf2Pdf(string rtf, string fileName)
{
_Rtf = rtf;
_FileName = fileName;
}
public void Process()
{
Document document = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(FileName, FileMode.Create));
document.Open();
// Open RTF Document
IRtfDocument rtfDoc = RtfInterpreterTool.BuildDoc(Rtf);
Rtf2iTextSharp rtf2IText = new Rtf2iTextSharp(rtfDoc);
Paragraph para = rtf2IText.Convert();
para.SetLeading(12F, 0);
PdfContentByte cb = writer.DirectContent;
SampleParagraphs(para, cb, 792-36, 252, 36);
para.Add(new Chunk(" (continued)",para.Font));
SampleParagraphs(para, cb, 792 - 36, 252, 324);
// Close the document
document.Close();
}
private static void SampleParagraphs(Paragraph para, PdfContentByte cb, float yTop, float width, float x)
{
while (yTop > 0)
{
float newYTop = TextAt(cb, para, x, yTop - 12F, width, 100);
//Console.WriteLine("{0},{1},{2}", yTop, width, newYTop);
width -= 16;
yTop = newYTop;
}
}
public static float GetParagraphHeight(PdfContentByte cb, Paragraph iParagraph, float width)
{
ColumnText myColumnText = new ColumnText(cb);
myColumnText.SetSimpleColumn(0, 792F, width, 0); // Bottom margin
myColumnText.AddElement(iParagraph);
//myColumnText.UseAscender = true;// Adjusts to the top of the text box.
int status = myColumnText.Go(true); // Check to see if it will fit on the page.
if (ColumnText.HasMoreText(status)) throw(new Exception("Paragraph longer than a page"));
return 792F - myColumnText.YLine; // This gives the height of the Paragraph
}
private static bool _DrawBox = true;
public static bool DrawBox
{
get { return Rtf2Pdf._DrawBox; }
set { Rtf2Pdf._DrawBox = value; }
}
private static System.Drawing.PointF _Offset = new System.Drawing.PointF(0, 0);
public static System.Drawing.PointF Offset
{
get { return Rtf2Pdf._Offset; }
set { Rtf2Pdf._Offset = value; }
}
public static float TextAt(PdfContentByte cb, Paragraph iParagraph, float x, float y, float width, float height)
{
float left = x + Offset.X;
float top = y + Offset.Y;
float right = left + width;
float bottom = top - height;
ColumnText myColumnText = new ColumnText(cb);
//myColumnText.SetSimpleColumn(left, top, left + width, top - height);
myColumnText.SetSimpleColumn(left, top, left + width, 36); // Bottom margin
//myColumnText.YLine -= 10;
//float yOff = pp.Font.BaseFont.GetAscentPoint("Almg", pp.Font.Size);
//myColumnText.YLine = -yOff;
myColumnText.AddElement(iParagraph);
float pos = myColumnText.YLine;
//myColumnText.UseAscender = true;// Adjusts to the top of the text box.
// go processing it and if it fits, nothing to be processed is in mycolumntext.
int status = myColumnText.Go(true); // Check to see if it will fit on the page.
if (ColumnText.HasMoreText(status))
return 0;// Won't fit return 0;
myColumnText.YLine = pos; // location on page
myColumnText.AddElement(iParagraph); // add in paragraph
myColumnText.Go(false);
//float textHeight = pp.Leading * myColumnText.LinesWritten;
//Console.WriteLine("Calculated Bottom {0}, YLine {1}", top - textHeight, myColumnText.YLine);
//BoxText(cb, System.Drawing.Color.BurlyWood, left, top, left + width, top - height);
//BoxText(canvas, System.Drawing.Color.CadetBlue, left, top, left + width, top - textHeight);
if(DrawBox)BoxText(cb, System.Drawing.Color.CadetBlue, left, top, left + width, myColumnText.YLine);
//Console.WriteLine("Lines = {0}", myColumnText.LinesWritten);
return myColumnText.YLine;
}
private static void BoxText(PdfContentByte cb, System.Drawing.Color sysColor, float left, float top, float right, float bottom)
{
float yAdj = 3;
cb.SaveState();
cb.SetColorStroke(new Color(sysColor));
cb.SetLineWidth(.1F);
cb.MoveTo(left, top - yAdj);
cb.LineTo(right, top - yAdj);
cb.LineTo(right, bottom - yAdj);
cb.LineTo(left, bottom - yAdj);
cb.LineTo(left, top - yAdj);
cb.Stroke();
cb.RestoreState();
}
}
}