189 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			189 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Xml;
 | |
| using System.Drawing;
 | |
| using System.Windows.Forms;
 | |
| //using C1.C1Pdf;
 | |
| 
 | |
| namespace fmtxml
 | |
| {
 | |
| 	/// <summary>
 | |
| 	/// Summary description for XmlToPdf.
 | |
| 	/// </summary>
 | |
| 	public class XmlToPdf
 | |
| 	{
 | |
| 		private float ConvertToPoints=72f;
 | |
| 		private XmlDocument xmlDoc;
 | |
| 		private string FileName;
 | |
| 		private string FilePath;
 | |
| 		//private C1.C1Pdf.C1PdfDocument _c1pdf;
 | |
| 		
 | |
| 		private string [] FontChoice = 
 | |
| 		   {"Times New Roman",
 | |
| 			"VESymb XXXXXX",
 | |
| 			"VolianDraw XXXXXX",
 | |
| 			"Prestige Elite Tall",
 | |
| 			"Courier New",
 | |
| 			"Arial",
 | |
| 			"Letter Gothic",
 | |
| 			"Times New Roman",
 | |
| 			"Letter Gothic Tall",
 | |
| 			"Letter Gothic Tall",
 | |
| 			"Gothic Ultra",
 | |
| 			"VolianScript"
 | |
| 		};
 | |
| 		private float LeftMargin;				//todo: define in page info
 | |
| 		private float VerticalOffset;			//todo: define in page info
 | |
| 		private bool Portrait;					//todo: define in page info
 | |
| 
 | |
| 		public XmlToPdf(string path, string fname)
 | |
| 		{
 | |
| 			LeftMargin=0.5f * ConvertToPoints;	// KBR: TODO
 | |
| 			VerticalOffset=0.5f * ConvertToPoints;	// KBR: TODO
 | |
| 			Portrait = true; // KBR: TODO
 | |
| 			FileName = fname;
 | |
| 			FilePath = path;
 | |
| 			xmlDoc = new XmlDocument();
 | |
| 			XmlTextReader reader = new XmlTextReader(FilePath+"\\"+FileName);
 | |
| 			xmlDoc.Load(reader);
 | |
| 			reader.Close();
 | |
| 			try
 | |
| 			{
 | |
| 				GeneratePDFForFile();
 | |
| 			}
 | |
| 			catch (Exception e)
 | |
| 			{
 | |
| 				MessageBox.Show("Error processing: " + fname, e.Message);
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		private void GeneratePDFForFile()
 | |
| 		{
 | |
| 			//_c1pdf = new C1.C1Pdf.C1PdfDocument();
 | |
| 			//string rootname = FileName.Substring(0,FileName.Length-4);
 | |
| 			//// create a pdf file & for each macro (i.e. macro element in xml tree), 
 | |
| 			//// put it into the pdf file.
 | |
| 			//XmlElement top = (XmlElement) xmlDoc.SelectSingleNode(rootname + "_GENMAC");
 | |
| 			//XmlNodeList xmlNds = top.SelectNodes("MACRO");
 | |
| 			//foreach (XmlNode nd in xmlNds)
 | |
| 			//{	
 | |
| 			//    XmlNode name = nd.SelectSingleNode("NAME");
 | |
| 			//    XmlElement elm = (XmlElement) name;
 | |
| 			//    XmlNode definition = nd.SelectSingleNode("DEFINITION");
 | |
| 			//    MacroGeneratePDF(definition);
 | |
| 			//    _c1pdf.NewPage();
 | |
| 			//}
 | |
| 			//string outname = "E:\\ve-proms.net\\genmac.xml\\testpdf\\" + rootname + ".pdf";
 | |
| 			//_c1pdf.Save(outname);
 | |
| 		}
 | |
| 
 | |
| 		private void MacroGeneratePDF(XmlNode definition)
 | |
| 		{
 | |
| 			XmlElement elm = (XmlElement) definition;
 | |
| 			string def = elm.InnerText;
 | |
| 			int lindx = 0;
 | |
| 			int indx = def.IndexOf(";");
 | |
| 			while (indx>-1)
 | |
| 			{
 | |
| 				string command = def.Substring(lindx,indx-lindx).Trim();
 | |
| 				DoPDF(command);
 | |
| 				lindx = indx+1;
 | |
| 				indx = def.IndexOf(";",lindx);
 | |
| 			}
 | |
| 			
 | |
| 		}
 | |
| 
 | |
| 		private void DoPDF(string cmdline)
 | |
| 		{
 | |
| 			string cmd = cmdline.Substring(0,cmdline.IndexOf(" "));
 | |
| 			int stx, sty, ex, ey = 0;
 | |
| 			float fstx, fsty, fex, fey = 0;
 | |
| 			int lw=0;
 | |
| 			string restOfIt=null;
 | |
| 			string [] parts;
 | |
| 
 | |
| 			Pen pn = null;
 | |
| 			switch (cmd)
 | |
| 			{
 | |
| 				case "BOX":
 | |
| 					restOfIt = cmdline.Substring(4,cmdline.Length-4);
 | |
| 					parts = restOfIt.Split(" ".ToCharArray(),5);
 | |
| 					fstx = (float) System.Convert.ToDouble(parts[0]);
 | |
| 					fsty = (float) System.Convert.ToDouble(parts[1]);
 | |
| 					fex = (float) System.Convert.ToDouble(parts[2]);
 | |
| 					fey = (float) System.Convert.ToDouble(parts[3]);
 | |
| 					float flw = (float) System.Convert.ToDouble(parts[4]);
 | |
| 					pn = new Pen(Brushes.Black,flw);
 | |
| 					//KBR: TODO - storing page stuff such as portland, vertical offset, tab, etc.
 | |
| 					RectangleF rc = new RectangleF(LeftMargin+fstx,VerticalOffset+fsty,fex,fey);
 | |
| 					//_c1pdf.DrawRectangle(pn,rc);
 | |
| 					break;
 | |
| 				case "LINE":   // command is LINE startx, starty, endx, endy, lnwidth
 | |
| 					restOfIt = cmdline.Substring(5,cmdline.Length-5);
 | |
| 					parts = restOfIt.Split(" ".ToCharArray(),5);
 | |
| 					fstx = (float) System.Convert.ToDouble(parts[0]);
 | |
| 					fsty = (float) System.Convert.ToDouble(parts[1]);
 | |
| 					fex = (float) System.Convert.ToDouble(parts[2]);
 | |
| 					fey = (float) System.Convert.ToDouble(parts[3]);
 | |
| 					float flw1 = (float) System.Convert.ToDouble(parts[4]);
 | |
| //KBR: TODO - storing page stuff such as portland, vertical offset, tab, etc.
 | |
| //					startx = portland ? (vof + cy + adjy) : (cx + tab + adjx);
 | |
| //					starty = portland ? (tab - (cx + adjx)) : (cy + vof + adjy);
 | |
| 					pn = new Pen(Brushes.Black,flw1);
 | |
| //KBR: TODO - move ablsolute?				
 | |
| //					if (MoveAbsolute)
 | |
| //						{tab = 0;vof = 0;}
 | |
| 					//_c1pdf.DrawLine(pn, LeftMargin+fstx, VerticalOffset+fsty, LeftMargin+fstx+fex, VerticalOffset+fsty+fey);
 | |
| 					break;
 | |
| 				case "TEXT":
 | |
| 					int quote = cmdline.IndexOf("\"");
 | |
| 					string txt = cmdline.Substring(quote+1,cmdline.LastIndexOf("\"")-quote-1);
 | |
| 					restOfIt = cmdline.Substring(5,quote-6);
 | |
| 					parts = restOfIt.Split(" ".ToCharArray(),5);
 | |
| 					fstx = (float) System.Convert.ToDouble(parts[0]);
 | |
| 					fsty = (float) System.Convert.ToDouble(parts[1]);
 | |
| 					int FontSize = System.Convert.ToInt32(parts[2]);
 | |
| 					FontSize = FontSize/2;
 | |
| 					int FontFamily = System.Convert.ToInt32(parts[3]);
 | |
| 					int bui = System.Convert.ToInt32(parts[4]);		//kbr todo: use bui
 | |
| 					Font myfont = new Font(this.FontChoice[FontFamily],FontSize);
 | |
| 					rc = new Rectangle();
 | |
| 					//rc.Size = _c1pdf.MeasureStringRtf(txt,myfont,500); // kbr todo: 500?
 | |
| 					rc.X = LeftMargin+fstx;
 | |
| 					rc.Y = VerticalOffset+fsty-FontSize; //-(rc.Size.Height/2);
 | |
| 					//_c1pdf.DrawStringRtf(txt,myfont,Brushes.Black,rc);
 | |
| 					break;
 | |
| 				case "GDIADJ":
 | |
| 					break;
 | |
| 				case "ELLIPSE":
 | |
| 					restOfIt = cmdline.Substring(8,cmdline.Length-8);
 | |
| 					parts = restOfIt.Split(" ".ToCharArray(),5);
 | |
| 					stx = System.Convert.ToInt32(parts[0]);
 | |
| 					sty = System.Convert.ToInt32(parts[1]);
 | |
| 					ex = System.Convert.ToInt32(parts[2]);
 | |
| 					ey = System.Convert.ToInt32(parts[3]);
 | |
| 					lw = System.Convert.ToInt32(parts[4]);
 | |
| 					//_c1pdf.DrawEllipse(pn,LeftMargin+stx, VerticalOffset+sty, LeftMargin+ex, VerticalOffset+ey);
 | |
| 					break;
 | |
| 				case "BITMAP":
 | |
| 					restOfIt = cmdline.Substring(8,cmdline.Length-7);
 | |
| 					parts = restOfIt.Split(" ".ToCharArray(),3);
 | |
| 					fstx = (float) System.Convert.ToDouble(parts[0]);
 | |
| 					fsty = (float) System.Convert.ToDouble(parts[1]);
 | |
| 					string fname = parts[2];
 | |
| 					Image img = Image.FromFile(fname);
 | |
| 					rc = new RectangleF();
 | |
| 					rc.Height = img.Height;
 | |
| 					rc.Width = img.Width;
 | |
| 					rc.X = LeftMargin+fstx;
 | |
| 					rc.Y = VerticalOffset+fsty-(rc.Size.Height/2);
 | |
| 					//_c1pdf.DrawImage(img, rc, ContentAlignment.MiddleCenter, ImageSizeModeEnum.Scale);
 | |
| 					break;
 | |
| 				case "ABSOLUTE":
 | |
| 					break;
 | |
| 				default:
 | |
| 					break;
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| }
 |