Rich c1d863e1ac Changed VlnBorders so that XML is not saved twice
Changed MyBorderDetailString so that XML is saved once
Adjust the vertical location of the Grid to match 16 bit.
2011-04-12 13:49:01 +00:00

180 lines
6.4 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.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,"",36);
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, float yBottomMargin)
{
VlnSvgPageHelper _MyPageHelper = cb.PdfWriter.PageEvent as VlnSvgPageHelper;
PdfLayer textLayer = _MyPageHelper == null ? null : _MyPageHelper.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, yBottomMargin);
myColumnText.AddElement(iParagraph);
float pos = myColumnText.YLine; // Save the position to be used if the paragraph fits
int status = myColumnText.Go(true); // Check to see if it will fit on the page.
if (ColumnText.HasMoreText(status))
return 0;// Paragraph won't fit. Return 0;
myColumnText.YLine = pos; // restore the location on the page
myColumnText.AddElement(iParagraph); // add in paragraph
if (textLayer != null) cb.BeginLayer(textLayer);
myColumnText.Go(false); // Draw the paragraph
if (textLayer != null) cb.EndLayer();
// Approximate Descent by using Leading divided by 5.
float yDescent = iParagraph.Leading / 5;
// If the BaseFont is available, calculate yAdj on the basis of the Descent
if (iParagraph.Font.BaseFont != null)
yDescent = -iParagraph.Font.BaseFont.GetDescentPoint("Almg", iParagraph.Font.Size);
if (PdfDebug)
DrawPdfDebug(cb, left, top, left + width, myColumnText.YLine, debugText, yDescent);
return myColumnText.YLine;
}
public static float FigureAt(PdfContentByte cb, iTextSharp.text.Image image, float x, float y, float width, float height, string debugText, float yBottommargin, bool hasBorder)
{
VlnSvgPageHelper _MyPageHelper = cb.PdfWriter.PageEvent as VlnSvgPageHelper;
PdfLayer textLayer = _MyPageHelper == null ? null : _MyPageHelper.TextLayer;
float left = x + Offset.X;
float top = y + Offset.Y;
float bottom = top - height;
image.ScaleAbsoluteWidth(width);
image.ScaleAbsoluteHeight(height);
image.SetAbsolutePosition(left, bottom);
if (textLayer != null) cb.BeginLayer(textLayer);
cb.AddImage(image);
if (hasBorder)
{
iTextSharp.text.Color boxColor = new iTextSharp.text.Color(System.Drawing.Color.Black); // (PrintOverride.OverrideBoxColor(System.Drawing.Color.Black));
cb.SetColorStroke(boxColor);
cb.SetLineWidth(.85F);
cb.Rectangle(left-1.5F, bottom-1.5F, width+3, height+3);
cb.Stroke();
}
if (textLayer != null) cb.EndLayer();
return bottom;
}
private static float _GridTopAdjust = -10;
public static float GridTopAdjust
{
get { return _GridTopAdjust; }
set { _GridTopAdjust = value; }
}
public static float GridAt(PdfContentByte cb, vlnTable myGrid, float x, float y, float width, float height, string debugText, float yBottomMargin, bool hasBorder)
{
VlnSvgPageHelper _MyPageHelper = cb.PdfWriter.PageEvent as VlnSvgPageHelper;
PdfLayer textLayer = _MyPageHelper == null ? null : _MyPageHelper.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, yBottomMargin);
if (textLayer != null) cb.BeginLayer(textLayer);
myGrid.ToPdf(myColumnText, left, top + GridTopAdjust);
if (textLayer != null) cb.EndLayer();
return bottom;
}
private static void DrawPdfDebug(PdfContentByte cb, float left, float top, float right, float bottom, string debugText, float yDescent)
{
VlnSvgPageHelper _MyPageHelper = cb.PdfWriter.PageEvent as VlnSvgPageHelper;
PdfLayer debugLayer = _MyPageHelper == null ? null : _MyPageHelper.DebugLayer;
if (debugLayer == null) return;
System.Drawing.Color sysColor = PrintOverride.OverrideDebugColor(System.Drawing.Color.Gray);
cb.SaveState();
cb.BeginLayer(debugLayer);
cb.SetColorStroke(new Color(sysColor));
cb.SetLineWidth(.1F);
cb.MoveTo(left, top - yDescent);
cb.LineTo(right, top - yDescent);
cb.LineTo(right, bottom - yDescent);
cb.LineTo(left, bottom - yDescent);
cb.LineTo(left, top - yDescent);
cb.Stroke();
if (debugText != "")
{
ColumnText ct = new ColumnText(cb);
ct.SetSimpleColumn(left, 4 + top - yDescent, right, top - yDescent - 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();
}
cb.EndLayer();
cb.RestoreState();
}
}
}