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; } } private static bool _PdfDebug = true; public static bool PdfDebug { get { return Rtf2Pdf._PdfDebug; } set { Rtf2Pdf._PdfDebug = 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, string debugText) { VlnSvgPageHelper _MyPageHelper = cb.PdfWriter.PageEvent as VlnSvgPageHelper; PdfLayer textLayer = _MyPageHelper == null ? null : _MyPageHelper.TextLayer; if (textLayer != null) cb.BeginLayer(textLayer); 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 (textLayer != null) cb.EndLayer(); if (PdfDebug) DrawPdfDebug(cb, System.Drawing.Color.CadetBlue, left, top, left + width, myColumnText.YLine, debugText); //Console.WriteLine("Lines = {0}", myColumnText.LinesWritten); return myColumnText.YLine; } private static void DrawPdfDebug(PdfContentByte cb, System.Drawing.Color sysColor, float left, float top, float right, float bottom, string debugText) { float yAdj = 3; cb.SaveState(); VlnSvgPageHelper _MyPageHelper = cb.PdfWriter.PageEvent as VlnSvgPageHelper; PdfLayer debugLayer = _MyPageHelper == null ? null : _MyPageHelper.DebugLayer; if (debugLayer != null) cb.BeginLayer(debugLayer); 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(); ColumnText ct = new ColumnText(cb); ct.SetSimpleColumn(left, 4 + top - yAdj, right, top - yAdj - 50); iTextSharp.text.Font font = FontFactory.GetFont("Arial", 2); Chunk chk = new Chunk(debugText, font); Phrase ph = new Phrase(chk); ct.AddElement(ph); cb.SetColorFill(new Color(sysColor)); ct.Go(); if (debugLayer != null) cb.EndLayer(); cb.RestoreState(); } } }