XPS 打印 字体异常。求各位老大解决
问题1:
异常信息:......xxxxx.ttf file does not conform to the expected file format specification.
原因:我在xps中加入了中文。
//代码片段1 foreach (XpsFont font in fixedPageReader.Fonts) { string name = Helper.GetFileName(font.Uri); string guid = new Guid(GetFileName(font.Uri).Split('.')[0]).ToString("N"); name = System.IO.Path.Combine(guid, System.IO.Path.GetFileNameWithoutExtension(name) + ".ttf"); string path = string.Format(@"{0}\{1}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), name); if (!fontList.ContainsKey(font.Uri.OriginalString)) { Helper.SaveToDisk(font, path); fontList.Add(font.Uri.OriginalString, path); } }//代码片段2 foreach (KeyValuePair<string, string> val in fontList) { page = Helper.ReplaceAttribute(page, "FontUri", val.Key, val.Value); //page = Helper.ReplaceAttribute(page, "FontUri", val.Key, "D:\\simsunb.ttf"); }//代码片段3 FixedPage fp = XamlReader.Load(new MemoryStream(Encoding.Default.GetBytes(page))) as FixedPage;
private void Print_Click(object sender, RoutedEventArgs e) { DataGrid dg = new DataGrid(); dg.ItemsSource = getdata().DefaultView; StackPanel sp = new StackPanel(); sp.Children.Add(dg); PrintDialog dialog = new PrintDialog(); if (dialog.ShowDialog() == true) { dialog.PrintVisual(sp, "Print Test"); } VisualPrintDialog printDlg = new VisualPrintDialog(sp); printDlg.ShowDialog(); } double width = 11; double height = 8.5; private DataTable getdata() { DataTable dt = new DataTable(); dt.Columns.Add("aa", typeof(string)); dt.Columns.Add("BB", typeof(string)); dt.Columns.Add("中文", typeof(string)); for (int j = 0; j < 10; j++) { string[] objVals = new string[3]; for (int i = 0; i < 3; i++) { string obj = "tets" + j; objVals[i] = obj; } dt.Rows.Add(objVals); } return dt; }