⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmltranslator.cs

📁 xhtml到rtf的转换程序!带源码!
💻 CS
📖 第 1 页 / 共 2 页
字号:
									}
									else if (token[0].ToLower().Trim() == "text-decoration")
									{
										if (token[1].ToLower().Trim() == "none")
										{
											rtfDocument.AppendText("\\ul0\\strike0 ");
										}
										else if (token[1].ToLower().Trim() == "underline")
										{
											rtfDocument.AppendText("\\ul ");
										}
										else if (token[1].ToLower().Trim() == "line-through")
										{
											rtfDocument.AppendText("\\strike ");
										}
									}
									else if (token[0].ToLower().Trim() == "color")
									{
										int index = 0;
										index = rtfDocument.UseColor(TranslateXhtmlColorIntoColorFromArgb(token[1].ToLower().Trim()));
										rtfDocument.AppendText("\\cf" + index + " ");
									}
									else if (token[0].ToLower().Trim() == "line-height")
									{
										int rtfFontSize = TranslateStyleFontSizeIntoRtfFontSize(token[1].Trim(), baseFontSize);
										rtfDocument.AppendText("\\sl" + (10 * rtfFontSize)  + "\\slmult1 ");
									}
								}
							}
						}

						// Recursively translates descendent nodes
						foreach (XmlNode child in x.ChildNodes)
						{
							TranslateXmlNodeIntoRtfGroup(child, baseFontSize);
						}
					}

					// Because of peculiarities of the RTF format, some control
					// words are placed at the end of their groups.
					if (x.Name.ToLower() == "p")
					{
						foreach (XmlAttribute attribute in x.Attributes)
						{
							if (attribute.Name.ToLower() == "align")
							{
								if (attribute.Value == "right")
								{
									rtfDocument.AppendText("\\qr ");
								}
								else if (attribute.Value == "center")
								{
									rtfDocument.AppendText("\\qc ");
								}
								else if (attribute.Value == "justify")
								{
									rtfDocument.AppendText("\\qj ");
								}
								else
								{
									rtfDocument.AppendText("\\ql ");
								}
							}
						}

						rtfDocument.AppendText("\\par");
					}
					else if ((x.Name.ToLower() == "h1")
						|| (x.Name.ToLower() == "h2")
						|| (x.Name.ToLower() == "h3")
						|| (x.Name.ToLower() == "h4")
						|| (x.Name.ToLower() == "h5")
						|| (x.Name.ToLower() == "h6"))
					{
						rtfDocument.AppendText("\\par");
					}
					
					rtfDocument.AppendText("}");

					// This terminates the effects of a ul "bullet" tag.
					if (x.Name.ToLower() == "ul")
					{
						rtfDocument.AppendText("\\pard");
					}
				}
			}
		}

		/// <summary>
		/// Translates an XHTML font size (1-7) into an RTF font size in half-points.
		/// Any errors result in a font size of 12 pts.
		/// </summary>
		private int TranslateXhtmlFontSizeIntoRtfFontSize(int xhtmlFontSize)
		{
			switch (xhtmlFontSize)
			{
				case 1: return 15;
				case 2: return 20;
				case 3: return 24;
				case 4: return 27;
				case 5: return 36;
				case 6: return 48;
				case 7: return 72;
				default: errors.Add("Unrecognized XHTML font size. Defaulting to 12 pt."); 
					return 24;
			}
		}

		/// <summary>
		/// This inverts the previous method.
		/// </summary>
		private int TranslateRtfFontSizeIntoXhtmlFontSize (int rtfFontSize)
		{
			if (rtfFontSize <= 15)
			{
				return 1;
			}
			else if (rtfFontSize <= 20)
			{
				return 2;
			}
			else if (rtfFontSize <= 24)
			{
				return 3;
			}
			else if (rtfFontSize <= 27)
			{
				return 4;
			}
			else if (rtfFontSize <= 36)
			{
				return 5;
			}
			else if (rtfFontSize <= 48)
			{
				return 6;
			}
			else
			{
				return 7;
			}
		}

		/// <summary>
		/// Translates a style sheet font size, which may have one of several different font
		/// measurements, into an RTF font size in half-points. The baseFontSize is required
		/// for measurements given in a form relative to the current font size. Any errors
		/// result in a font size of 12 pts.
		/// </summary>
		private int TranslateStyleFontSizeIntoRtfFontSize(string styleFontSize, int baseFontSize)
		{
			Font convertSize;

			if (styleFontSize.ToLower() == "xx-small")
			{
				return TranslateXhtmlFontSizeIntoRtfFontSize(1);
			}
			else if (styleFontSize.ToLower() == "x-small")
			{
				return TranslateXhtmlFontSizeIntoRtfFontSize(2);
			}
			else if (styleFontSize.ToLower() == "small")
			{
				return TranslateXhtmlFontSizeIntoRtfFontSize(3);
			}
			else if (styleFontSize.ToLower() == "medium")
			{
				return TranslateXhtmlFontSizeIntoRtfFontSize(4);
			}
			else if (styleFontSize.ToLower() == "large")
			{
				return TranslateXhtmlFontSizeIntoRtfFontSize(5);
			}
			else if (styleFontSize.ToLower() == "x-large")
			{
				return TranslateXhtmlFontSizeIntoRtfFontSize(6);
			}
			else if (styleFontSize.ToLower() == "xx-large")
			{
				return TranslateXhtmlFontSizeIntoRtfFontSize(7);
			}
			else if (styleFontSize.ToLower() == "smaller")
			{
				return TranslateXhtmlFontSizeIntoRtfFontSize(baseFontSize - 1);
			}
			else if (styleFontSize.ToLower() == "bigger")
			{
				return TranslateXhtmlFontSizeIntoRtfFontSize(baseFontSize + 1);
			}
			else
			{
				try
				{
					if (styleFontSize.ToLower().EndsWith("%"))
					{
						return (int)((TranslateXhtmlFontSizeIntoRtfFontSize(baseFontSize)
							* Int32.Parse(styleFontSize.Substring(0, styleFontSize.ToLower().IndexOf("%")))) 
							/ 100.0);
					}
					else if (styleFontSize.ToLower().EndsWith("em"))
					{
						return (int)(TranslateXhtmlFontSizeIntoRtfFontSize(baseFontSize)
							* Double.Parse(styleFontSize.Substring(0, styleFontSize.ToLower().IndexOf("em"))));
					}
					else if (styleFontSize.ToLower().EndsWith("ex"))
					{
						return (int)((TranslateXhtmlFontSizeIntoRtfFontSize(baseFontSize) / 2.0)
							* Double.Parse(styleFontSize.Substring(0, styleFontSize.ToLower().IndexOf("ex"))));
					}
					else if (styleFontSize.ToLower().EndsWith("pt"))
					{
						return (int)(2.0
							* Double.Parse(styleFontSize.Substring(0, styleFontSize.ToLower().IndexOf("pt"))));
					}
					else if (styleFontSize.ToLower().EndsWith("in"))
					{
						convertSize = new Font("", 
							(float)(Double.Parse(styleFontSize.Substring(0, styleFontSize.ToLower().IndexOf("in")))),
							System.Drawing.GraphicsUnit.Inch);
						return (int)(2.0 * convertSize.SizeInPoints);
					}
					else if (styleFontSize.ToLower().EndsWith("cm"))
					{
						convertSize = new Font("", 
							(float)((Double.Parse(styleFontSize.Substring(0, styleFontSize.ToLower().IndexOf("cm")))) / 10),
							System.Drawing.GraphicsUnit.Millimeter);
						return (int)(2.0 * convertSize.SizeInPoints);
					}
					else if (styleFontSize.ToLower().EndsWith("mm"))
					{
						convertSize = new Font("", 
							(float)(Double.Parse(styleFontSize.Substring(0, styleFontSize.ToLower().IndexOf("mm")))),
							System.Drawing.GraphicsUnit.Millimeter);
						return (int)(2.0 * convertSize.SizeInPoints);
					}
					else if (styleFontSize.ToLower().EndsWith("pc"))
					{
						convertSize = new Font("", 
							(float)((Double.Parse(styleFontSize.Substring(0, styleFontSize.ToLower().IndexOf("pc")))) * 12),
							System.Drawing.GraphicsUnit.Point);
						return (int)(2.0 * convertSize.SizeInPoints);
					}
					else if (styleFontSize.ToLower().EndsWith("px"))
					{
						convertSize = new Font("", 
							(float)(Double.Parse(styleFontSize.Substring(0, styleFontSize.ToLower().IndexOf("px")))),
							System.Drawing.GraphicsUnit.Pixel);
						return (int)(2.0 * convertSize.SizeInPoints);
					}
					else
					{
						errors.Add("Invalid font unit. Defaulting to 12 pt.");
						return 24;
					}
				}
				catch (Exception e)
				{
					errors.Add("Invalid font size. Defaulting to 12 pt.");
					return 24;
				}
			}
		}

		/// <summary>
		/// Translates color arguments into Color object representations.
		/// The argument may either be a hexidecimal number #RRGGBB,
		/// one of sixteen predefined, case-sensitive names (these are 
		/// used by the font element with the color attribute).
		/// NOTE: Predefined color names for style sheets are NOT supported.
		/// Style sheet colors must either be entered in as a hex number or
		/// in the form of: rgb float float float, where each float is a value
		/// between 0 and 1, representing relative amounts of red, green, and blue,
		/// respectively. Any errors result in a default of black (0, 0, 0).
		/// </summary>
		/// <param name="xhtmlColor"></param>
		/// <returns></returns>
		private Color TranslateXhtmlColorIntoColorFromArgb(string xhtmlColor)
		{
			if (xhtmlColor == "Black")
			{
				return Color.FromArgb(0, 0, 0);
			}
			else if (xhtmlColor == "Silver")
			{
				return Color.FromArgb(192, 192, 192);
			}
			else if (xhtmlColor == "Gray")
			{
				return Color.FromArgb(128, 128, 128);
			}
			else if (xhtmlColor == "White")
			{
				return Color.FromArgb(255, 255, 255);
			}
			else if (xhtmlColor == "Maroon")
			{
				return Color.FromArgb(128, 0, 0);
			}
			else if (xhtmlColor == "Red")
			{
				return Color.FromArgb(255, 0, 0);
			}
			else if (xhtmlColor == "Purple")
			{
				return Color.FromArgb(128, 0, 128);
			}
			else if (xhtmlColor == "Fuchsia")
			{
				return Color.FromArgb(255, 0, 255);
			}
			else if (xhtmlColor == "Green")
			{
				return Color.FromArgb(0, 128, 0);
			}
			else if (xhtmlColor == "Lime")
			{
				return Color.FromArgb(0, 255, 0);
			}
			else if (xhtmlColor == "Olive")
			{
				return Color.FromArgb(128, 128, 0);
			}
			else if (xhtmlColor == "Yellow")
			{
				return Color.FromArgb(255, 255, 0);
			}
			else if (xhtmlColor == "Navy")
			{
				return Color.FromArgb(0, 0, 128);
			}
			else if (xhtmlColor == "Blue")
			{
				return Color.FromArgb(0, 0, 255);
			}
			else if (xhtmlColor == "Teal")
			{
				return Color.FromArgb(0, 128, 128);
			}
			else if (xhtmlColor == "Aqua")
			{
				return Color.FromArgb(0, 255, 255);
			}
			else if (xhtmlColor.StartsWith("#"))
			{
				try
				{
					int color = Int32.Parse(xhtmlColor.Substring(1), 
						System.Globalization.NumberStyles.HexNumber);
					byte red = (byte)((color & 16711680) / 65536);
					byte green = (byte)((color & 65280) / 256);
					byte blue = (byte)(color & 255);
					return Color.FromArgb(red, green, blue);
				}
				catch (Exception e) 
				{
					errors.Add("Invalid color value. Defaulting to black.");
					return Color.FromArgb(0, 0, 0);
				}
			}
			else if (xhtmlColor.StartsWith("rgb"))
			{
				try
				{
					char[] space = {' '};
					string[] tokens = xhtmlColor.Split(space);

					int i = 0;

					while (tokens[++i] == "") { }
                    byte red = (byte)(Double.Parse(tokens[i].Trim()) * 255);

					while (tokens[++i] == "") { }
					byte green = (byte)(Double.Parse(tokens[i].Trim()) * 255);

					while (tokens[++i] == "") { }
					byte blue = (byte)(Double.Parse(tokens[i].Trim()) * 255);

					return Color.FromArgb(red, green, blue);
				}
				catch (Exception e)
				{
					errors.Add("Invalid color value. Defaulting to black.");
					return Color.FromArgb(0, 0, 0);
				}
			}
			else
			{
				errors.Add("Invalid color format. Defaulting to black.");
				return Color.FromArgb(0, 0, 0);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -