2010-09-28 17:06:49 +00:00

414 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using VEPROMS.CSLA.Library;
using Volian.Svg.Library;
using iTextSharp.text.factories;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using LBWordLibrary;
using System.Drawing;
using System.Drawing.Imaging;
using Volian.Controls.Library;
namespace Volian.Print.Library
{
public delegate void PromsPrinterStatusEvent(object sender,PromsPrintStatusArgs args);
public class PromsPrintStatusArgs
{
private string _MyStatus;
public string MyStatus
{
get { return _MyStatus; }
}
private DateTime _When = DateTime.Now;
public DateTime When
{
get { return _When; }
}
private PromsPrinterStatusType _Type;
public PromsPrinterStatusType Type
{
get { return _Type; }
set { _Type = value; }
}
private int _Progress = 0;
public int Progress
{
get { return _Progress; }
set { _Progress = value; }
}
public PromsPrintStatusArgs(string myStatus, PromsPrinterStatusType type)
{
_MyStatus = myStatus;
_Type = type;
}
public PromsPrintStatusArgs(string myStatus, PromsPrinterStatusType type, int progress)
{
_MyStatus = myStatus;
_Type = type;
_Progress = progress;
}
}
public enum PromsPrinterStatusType
{
Start,
General,
MSWordToPDF,
PageList,
Watermark,
Read16,
Merge16,
Open16,
ReadMSWord,
MergeMSWord,
OpenMSWord,
OpenPDF,
Merge,
Total,
CloseDocument,
NewPage,
BuildSVG,
SetSVG,
SetPageEvent,
GetSection,
Before,
BuildStep,
Progress,
ProgressSetup
}
public class PromsPrinter
{
public event PromsPrinterStatusEvent StatusChanged;
private void OnStatusChanged(object sender, PromsPrintStatusArgs args)
{
if (StatusChanged != null)
StatusChanged(sender, args);
}
private void OnStatusChanged(string myStatus, PromsPrinterStatusType type)
{
OnStatusChanged(this, new PromsPrintStatusArgs(myStatus, type));
}
private void OnStatusChanged(string myStatus, PromsPrinterStatusType type, int progress)
{
OnStatusChanged(this, new PromsPrintStatusArgs(myStatus, type, progress));
}
private string _Rev;
private string _RevDate;
private ItemInfo _MyItem;
private string _Watermark;
private bool _DebugOutput;
public bool DebugOutput
{
get { return _DebugOutput; }
set { _DebugOutput = value; }
}
private string _BackgroundFolder;
public string BackgroundFolder
{
get { return _BackgroundFolder; }
set { _BackgroundFolder = value; }
}
private bool _OpenPDF;
public bool OpenPDF
{
get { return _OpenPDF; }
set { _OpenPDF = value; }
}
private bool _OverWrite;
public bool OverWrite
{
get { return _OverWrite; }
set { _OverWrite = value; }
}
private ChangeBarDefinition _MyChangeBarDefinition;
public ChangeBarDefinition MyChangeBarDefinition
{
get { return _MyChangeBarDefinition; }
set { _MyChangeBarDefinition = value; }
}
public PromsPrinter(ItemInfo myItem, string rev, string revDate, string watermark, bool debugOutput, string backgroundFolder,bool openPDF, bool overWrite, ChangeBarDefinition cbd)
{
_MyItem = myItem;
_Rev = rev;
_RevDate = revDate;
_Watermark = watermark;
_DebugOutput = debugOutput;
_BackgroundFolder = backgroundFolder;
_OpenPDF = openPDF;
_OverWrite = overWrite;
_MyChangeBarDefinition = cbd;
}
public string Print(string pdfFolder)
{
if (_MyItem is ProcedureInfo)
return Print(_MyItem as ProcedureInfo, pdfFolder);
return "";
}
private string BuildMSWordPDF(SectionInfo section)
{
DateTime tStart = DateTime.Now;
string MSWordFile = null;
if (section.MyContent.ContentEntryCount == 1)
{
MSWordFile = MSWordToPDF.GetDocPdf(section, PrintOverride.TextColor);
OnStatusChanged("MSWord converted to PDF " + MSWordFile, PromsPrinterStatusType.MSWordToPDF);
}
return MSWordFile;
}
private static void AddImportedPageToLayer(PdfContentByte cb, PdfLayer layer, PdfImportedPage page, float xOff, float yOff)
{
cb.BeginLayer(layer);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(page);
image.SetAbsolutePosition(xOff, yOff);
cb.AddImage(image);
cb.EndLayer();
}
private PdfLayer _TextLayer;
private PdfLayer _BackgroundLayer;
private PdfLayer _MSWordLayer;
private PdfLayer _PagelistLayer;
private PdfLayer _DebugLayer;
private PdfLayer _WatermarkLayer;
private void CreateLayers(PdfContentByte cb)
{
if (DebugOutput)
{
_BackgroundLayer = new PdfLayer("16-Bit", cb.PdfWriter);
_MSWordLayer = new PdfLayer("32-Bit MSWord", cb.PdfWriter);
_PagelistLayer = new PdfLayer("32-Bit Pagelist", cb.PdfWriter);
_TextLayer = new PdfLayer("32-Bit Text", cb.PdfWriter);
_DebugLayer = new PdfLayer("Debug", cb.PdfWriter);
_WatermarkLayer = new PdfLayer("Watermark", cb.PdfWriter);
_WatermarkLayer.SetPrint("Watermark", true);
}
}
private void CloseDocument(PdfContentByte cb, string fileName)
{
cb.PdfDocument.Close();
OnStatusChanged("CloseDocument", PromsPrinterStatusType.CloseDocument);
if (OpenPDF)
System.Diagnostics.Process.Start(fileName);
OnStatusChanged("OpenPDF", PromsPrinterStatusType.OpenPDF);
}
private PdfContentByte OpenDoc(string outputFileName)
{
PdfWriter writer=null;
iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER);
try
{
writer = PdfWriter.GetInstance(document, new FileStream(outputFileName, FileMode.Create));
}
catch (Exception ex)
{
MessageBox.Show("Could not create " + outputFileName + ". If it is open, close and retry", "Error on CreatePdf");
return null;
}
document.Open();
// Create Layers
CreateLayers(writer.DirectContent);
PrintOverride.Reset();
if (DebugOutput)
{
PrintOverride.TextColor = System.Drawing.Color.Red;
PrintOverride.SvgColor = System.Drawing.Color.LawnGreen;
PrintOverride.BoxColor = System.Drawing.Color.Red;
PrintOverride.ChangeBarColor = System.Drawing.Color.Red;
PrintOverride.DebugColor = System.Drawing.Color.CadetBlue;
}
return writer.DirectContent;
}
private string CreateFileName(string procNumber, string sectNumber, string sectTitle)
{
return FixFileName(procNumber + "_" + ((sectNumber ?? "") != "" ? sectNumber : sectTitle));
}
private string CreateFileName(string procNumber)
{
return FixFileName(procNumber);
}
private string FixFileName(string name)
{
return Regex.Replace(name, "[ .,]", "_") + ".pdf";
}
int _StepPageNumber = 0;
private VlnSvgPageHelper _MyHelper = null;
private string Print(ProcedureInfo myProcedure, string pdfFolder)
{
OnStatusChanged("Print " + myProcedure.DisplayNumber, PromsPrinterStatusType.Start);
string outputFileName = pdfFolder + "\\" + CreateFileName(myProcedure.DisplayNumber);
if (!OverWrite && File.Exists(outputFileName))
{
if (MessageBox.Show(outputFileName + " exists. Overwrite file?", "File Exists", MessageBoxButtons.YesNo) == DialogResult.No)
return null;
}
// Create an MSWord Pdf
// Setup a pdf Document for printing
OnStatusChanged("Before OpenDoc", PromsPrinterStatusType.Before);
PdfContentByte cb = OpenDoc(outputFileName);
if (cb == null) return null;
OnStatusChanged("Before NewPage", PromsPrinterStatusType.Before);
cb.PdfDocument.NewPage();
OnStatusChanged("After NewPage", PromsPrinterStatusType.NewPage);
OnStatusChanged(myProcedure.DisplayNumber, PromsPrinterStatusType.ProgressSetup,myProcedure.Sections.Count);
int progress = 0;
foreach (SectionInfo mySection in myProcedure.Sections)
{
OnStatusChanged((mySection.DisplayNumber??"") == "" ? mySection.DisplayText : mySection.DisplayNumber, PromsPrinterStatusType.Progress, progress++);
// Set up Helper for the particular Section
if (_MyHelper == null)
{
cb.PdfWriter.PageEvent = _MyHelper = new VlnSvgPageHelper(mySection);
_MyHelper.MyPdfWriter = cb.PdfWriter;
_MyHelper.MyPdfContentByte = cb;
if (DebugOutput)
{
// 16-bit background
string procedureFileName = BackgroundFolder + "\\" + CreateFileName(myProcedure.DisplayNumber);
FileInfo VEPromsFile = new FileInfo(procedureFileName);
if (VEPromsFile.Exists)
{
_MyHelper.BackgroundFile = procedureFileName;
//if (Environment.UserName.ToUpper() == "KATHY")
// _MyHelper.BackgroundOffset = new PointF(12, -13.5F); // non-KBR Needs this to be -9.5 to line up!
//else
_MyHelper.BackgroundOffset = new PointF(12, -9.5F); // KBR Needs this to be -13.5 to line up!
_MyHelper.BackgroundPageOffset = 0;
}
_MyHelper.WatermarkLayer = _WatermarkLayer;
_MyHelper.PageListLayer = _PagelistLayer;
_MyHelper.TextLayer = _TextLayer;
_MyHelper.BackgroundLayer = _BackgroundLayer;
_MyHelper.DebugLayer = _DebugLayer;
}
_MyHelper.Rev = _Rev;
_MyHelper.RevDate = _RevDate;
_MyHelper.Watermark = _Watermark;
OnStatusChanged("After Set PageEvent", PromsPrinterStatusType.SetPageEvent);
}
else
{
_MyHelper.MySection = mySection;
OnStatusChanged("After Set Svg", PromsPrinterStatusType.SetSVG);
}
PdfReader readerWord = null;
string myPdfFile = null;
if (mySection.IsStepSection)
CreateStepPdf(mySection, cb);
else
{
myPdfFile = BuildMSWordPDF(mySection);
readerWord = myPdfFile != null ? new PdfReader(myPdfFile) : null;
OnStatusChanged("Get Section", PromsPrinterStatusType.GetSection);
if (mySection.MyContent.MyEntry.MyDocument.DocumentConfig.Printing_Length !=0) //.SectionConfig.Section_NumPages != "")
{
int sectPageCount = (int)(Math.Ceiling(mySection.MyContent.MyEntry.MyDocument.DocumentConfig.Printing_Length)); //decimal.Parse(mySection.MyContent.MyEntry.MyDocument.DocumentConfig.Printing_Length)));
for (int ii = 0; ii < sectPageCount; ii++)
{
int pageNumber = 1 + ii;
if (readerWord != null)
{
bool doimport2 = true;
PdfImportedPage fgPage = null;
try
{
fgPage = cb.PdfWriter.GetImportedPage(readerWord, ii + 1);
}
catch (Exception ex)
{
Console.WriteLine(ex);
doimport2 = false;
}
OnStatusChanged("Read MSWord", PromsPrinterStatusType.ReadMSWord);
if (doimport2) AddImportedPageToLayer(cb.PdfWriter.DirectContent, _MSWordLayer, fgPage, 0, 0);
OnStatusChanged("Merge MSWord", PromsPrinterStatusType.MergeMSWord);
}
OnStatusChanged("Before NewPage", PromsPrinterStatusType.Before);
cb.PdfDocument.NewPage();
OnStatusChanged("After NewPage", PromsPrinterStatusType.NewPage);
}
}
}
}
if (_MyHelper.BackgroundFile != null)
{
_MyHelper.MySvg = null;
while (cb.PdfWriter.CurrentPageNumber <= _MyHelper.BackgroundPageCount)
{
PrintTextMessage(cb, "No PROMS2010 Output");
cb.PdfDocument.NewPage();
}
}
OnStatusChanged(myProcedure.DisplayNumber + " PDF Creation Completed", PromsPrinterStatusType.Progress, progress);
CloseDocument(cb, outputFileName);
// Return the fileName;
return outputFileName;
}
private void PrintTextMessage(PdfContentByte cb, string message)
{
if (_TextLayer != null) cb.BeginLayer(_TextLayer);
float fontSize = 30;
ColumnText ct = new ColumnText(cb);
iTextSharp.text.Font font = FontFactory.GetFont("Arial", fontSize, new iTextSharp.text.Color(PrintOverride.TextColor));
Chunk chk = new Chunk(message, font);
float xCenter = cb.PdfDocument.PageSize.Width / 2;
float yCenter = cb.PdfDocument.PageSize.Height / 2;
float width = chk.GetWidthPoint() * 1.01F;
float height = fontSize * 1.5F;
ct.SetSimpleColumn(xCenter - width / 2, yCenter - height / 2, xCenter + width / 2, yCenter + height / 2);
Phrase ph = new Phrase(chk);
ct.AddElement(ph);
cb.SetColorFill(new iTextSharp.text.Color(PrintOverride.TextColor));
ct.Go();
if (_TextLayer != null) cb.EndLayer();
}
private void CreateStepPdf(SectionInfo section, PdfContentByte cb)
{
iTextSharp.text.pdf.PdfWriter writer = cb.PdfWriter;
ItemInfo myItemInfo = section as ItemInfo;
// 792: 72 * 11 inches - TopRow - Top is high value
float _PointsPerPage = 792;
float yTopMargin = _PointsPerPage - (float)myItemInfo.MyDocStyle.Layout.TopRow;
float yBottomMargin = yTopMargin - (float)myItemInfo.MyDocStyle.Layout.PageLength - 2 * vlnPrintObject.SixLinesPerInch;
vlnParagraph.Prefix = myItemInfo.Path;
Rtf2Pdf.PdfDebug = true;
Rtf2Pdf.Offset = new PointF(24, 2.5F);
_MyHelper.ChangeBarDefinition = MyChangeBarDefinition;
vlnParagraph myParagraph = new vlnParagraph(null, cb, myItemInfo, (float)myItemInfo.MyDocStyle.Layout.LeftMargin, 0, 0, myItemInfo.ColumnMode, myItemInfo.ActiveFormat);
if (myItemInfo.HasChildren)
myParagraph.ToPdf(cb, yTopMargin, yTopMargin, yBottomMargin);
else
PrintTextMessage(cb, "No Section Content");
cb.PdfDocument.NewPage();
OnStatusChanged("StepSection converted to PDF " + section.ShortPath, PromsPrinterStatusType.BuildStep);
}
private SvgPart PageItemToSvgText(PageItem pageItem, string text, float yOffset)
{
SvgText svgText = new SvgText();
svgText.Text = text;
E_Justify justify = pageItem.Justify ?? E_Justify.PSLeft;
if ((justify & E_Justify.PSLeft) == E_Justify.PSLeft)
svgText.Justify = SvgJustify.Left;
else if ((justify & E_Justify.PSRight) == E_Justify.PSRight)
svgText.Justify = SvgJustify.Right;
else
svgText.Justify = SvgJustify.Center;
svgText.Font = pageItem.Font.WindowsFont;
svgText.X = new SvgMeasurement((float)(pageItem.Col ?? 0), E_MeasurementUnits.PT);
svgText.Y = new SvgMeasurement((float)(yOffset + pageItem.Row ?? 0), E_MeasurementUnits.PT);
if (svgText.Font.Underline && svgText.Text.EndsWith(" ")) svgText.Text = svgText.Text.Substring(0, svgText.Text.Length - 1) + "\xA0";// replace last space with a hardspace
return svgText;
}
}
}