- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一个 WPF 应用程序,允许用户根据需要输入文本并设置文本样式。然后将他们的文本转换为 XPS,然后使用 ABCPdf 将其插入到 PDF 中。
我们最近切换到 .Net 4,现在 XPS 生成有时会生成格式错误的 XPS。
只有在用户使用重叠两个字符的字体(例如 Sevillana、available on Google Fonts)时,才会生成格式错误的 XPS。在 .Net 4 中,字符重叠,而在 .Net 3.5 中,它们不重叠。来自 another SO question我猜这是因为 .Net 4.0 改变了它的字体渲染引擎。
.Net 3.5 :
.Net 4.0:(注意“d”和“A”是如何重叠的)
看起来 .Net 4 没有考虑撇号的间距,甚至否定了它。如果我删除撇号,间距会变宽。我创建了一个简单的测试项目来演示这个问题:https://github.com/tbroust-trepia/wpf-4-font-rendering
我们将 FlowDocument 保存到 XPS 的方法与 creating an XPS Document from a FlowDocument and attach it on the fly 中的方法几乎相同。 (我怀疑原始开发人员只是复制/粘贴了该代码);它只是将 XPS 保存到文件而不是流中。
The MS tool IsXPS表示特定节点无效。事实上,在Documents\1\Pages\1.fpage中,Indices的值为;,-16;,84;;;;,30
。我可以看出“0.-16”并不是真正的数字。
所以,我有很多问题:
I have opened a bug at Microsoft ,但我对那里期望不高。我想我会尝试修改生成的 XPS。它很脏,但它可能有用。
最佳答案
在等待 MS 为所欲为的过程中,我找到了修复损坏的 XPS 的方法。
public class XpsFile
{
/// <summary>
/// Regex to validate XPS "indices" property (gotten from ABCPDF)
/// </summary>
private static readonly string IndicesRegex = @"(((\(([1-9][0-9]*)(:([1-9][0-9]*))?\))?([0-9]+))?(,(\+?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?)?(,((\-|\+)?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?)?(,((\-|\+)?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?))?)?)?)(;((\(([1-9][0-9]*)(:([1-9][0-9]*))?\))?([0-9]+))?(,(\+?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?)?(,((\-|\+)?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?)?(,((\-|\+)?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))((e|E)(\-|\+)?[0-9]+)?))?)?)?)*";
/// <summary>
/// Fixes the XPS problems it encounters and knows about
/// </summary>
public static void FixXps(string filePath)
{
// first we'll load the XPS file
using (var currentPackage = Package.Open(filePath, FileMode.Open))
{
var pageUri = new Uri("/Documents/1/Pages/1.fpage", UriKind.Relative);
// check that the file we'll modify exists
if (!currentPackage.PartExists(pageUri))
{
throw new Exception(string.Format("Unable to find first page in XPS {0} - unable to fix this XPS !", filePath));
}
// assume the broken part is in the first page
var firstPage = currentPackage.GetPart(pageUri);
var relationships = firstPage.GetRelationships();
var pageContent = XDocument.Load(firstPage.GetStream());
// then we'll look up each glyph and check if their "Indices" property is valid
XNamespace ns = pageContent.Root.GetDefaultNamespace();
var glyphs = (from g in pageContent.Descendants(ns + "Glyphs")
where g.Attribute("Indices") != null
select g).ToList();
for (var i = 0; i < glyphs.Count(); i ++)
{
glyphs[i] = FixGlyph(glyphs[i]);
}
// remove the current (corrupted) file from the package
currentPackage.DeletePart(pageUri);
// add the new (shiny) file to the package
var newPage = currentPackage.CreatePart(pageUri, "application/vnd.ms-package.xps-fixedpage+xml", CompressionOption.NotCompressed);
using (var ms = new MemoryStream())
{
// we need to remove XML declaration, so we need to use the XmlWriter
var settings = new XmlWriterSettings();
settings.Indent = false;
settings.NewLineChars = string.Empty;
settings.NewLineHandling = NewLineHandling.Replace;
settings.OmitXmlDeclaration = true;
using (var xw = XmlWriter.Create(ms, settings))
{
pageContent.WriteTo(xw);
}
ms.Seek(0, SeekOrigin.Begin);
CopyStream(newPage.GetStream(), ms);
}
// now we need to re-create the relationships between the Page file and the fonts
foreach (var relation in relationships)
{
newPage.CreateRelationship(relation.TargetUri, relation.TargetMode, relation.RelationshipType, relation.Id);
}
}
}
/// <summary>
/// Tries to load the XPS, and returns false if it fails
/// </summary>
public static bool IsValidXps(string filePath)
{
try
{
using (var xpsOld = new XpsDocument(filePath, FileAccess.Read))
{
var unused = xpsOld.GetFixedDocumentSequence();
}
return true;
}
catch (System.Windows.Markup.XamlParseException)
{
return false;
}
}
/// <summary>
/// Writes the whole content of a stream into another
/// </summary>
/// <remarks>
/// http://stackoverflow.com/a/18885954/2354542
/// </remarks>
private static void CopyStream(Stream target, Stream source)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
{
target.Write(buf, 0, bytesRead);
}
}
/// <summary>
/// Fixes the glyph, if necessary
/// </summary>
private static XElement FixGlyph(XElement g)
{
var matchAttribute = Regex.Match(g.Attribute("Indices").Value, IndicesRegex);
if (!matchAttribute.Success)
{
return g;
}
var hasProblem = false;
foreach (var token in matchAttribute.Value.Split(";".ToCharArray()))
{
if (token == ",")
{
hasProblem = true;
break;
}
}
if (hasProblem)
{
// the Indices attribute is not well-formed: let's try to fix the one(s) that are wrong
var fixedTokens = new List<string>();
foreach (var token in g.Attribute("Indices").Value.Split(";".ToCharArray()))
{
var newToken = token;
var matchToken = Regex.Match(token, @",(-\d+)");
if (matchToken.Success) // negative number, yay ! it's not allowed :-(
{
newToken = ",0"; // it should be zero, I believe
}
fixedTokens.Add(newToken);
}
g.Attribute("Indices").Value = string.Join(";", fixedTokens);
}
return g;
}
}
关于c# - XPS 编写器因 .Net 4/字体渲染错误而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23246254/
有没有办法在使用 WPF XPS 查看器打印 XPS 时设置默认布局方向? 我的固定文档 XPS 的页面方向设置为横向,页面媒体大小的宽度大于其高度,并且在查看器中正确显示为横向。 只是当您点击打印按
我正在将 WPF FlowDocument 保存到文件系统,使用此代码和带有 xps 扩展名的文件名: // Save FlowDocument to file system as XPS docum
我正在研究批量打印解决方案。 要打印的文件将采用各种(典型)格式,例如 PDF、Word、Excel 等。根据我收集(和测试)的结果,打印 XPS 文件是在 Windows 平台上进行的方式。 但是,
使用 Microsoft XPS Document Writer 打印 pdf 文档: string filename = "C:\\1.pdf"; Process process = new Pro
在他的博客条目中 Printing documents to Microsoft XPS Document Writer without user interaction冯远说 If you're p
我们的工具允许导出为 PNG,效果非常好。 现在,我想将导出添加到某种矢量格式。我尝试了 XPS,但结果根本不令人满意。 看看对比http://www.jakubmaly.cz/xps-vs-png.
我正在尝试在 XPS 文档中添加注释。是否可以使用 .Net API 并读取它们?我需要添加嵌入在 XPS 文件中的隐藏文本。 最佳答案 您可以使用 XpsDocument class 将元数据添加到
当我阅读有关 WPF 的书籍时,我看到作者提到 XPS 就像它很重要一样。 Windows 还包括它的 XPS 查看器,我已经看到它被列为 Windows 的“功能”。 但为什么?重点是什么?谁他妈的
XPS 文档的安全性如何?从XPS内部查看后文档中,找到了 Unicode-string 属性。有人可以注入(inject) e.x.吗?将脚本写入 Unicode 字符串属性? XPS 查看器如何处
我喜欢 DocumentViewer 在 WPF 应用程序中显示 XPS 文档。但我还需要编辑 XPS 文档。我找到了用于编辑 XPS 的独立应用程序 (.exe)。我需要的是一个用于查看和编辑 XP
我认为,XPS 文件类似于 PDF 文件,但 XPS 文件的结构是什么?就像 PDF 文件一样? 最佳答案 XPS vs. PDF on CNET 摘录: XPS 文档更像是 ZIP 存档文件,包含在
我正在用 C# 创建一个 asp.net web 应用程序来将一堆 .xps 文件转换为 .pdf 我想验证输入文件是出于安全原因,按其内容类型而不是文件扩展名转换。 最佳答案 According t
我尝试打印到 XPS 打印机(它不是我的默认打印机),但程序打开了一个对话框。我可以跳过对话框吗?这是代码: pSettings = new PrinterSettings()
我将使用 PrintDocument 显示我的 C# WinForms 应用程序的一部分,这几乎完成了,但是我的打印机出现了问题。我使用以下代码捕获表单图像,然后打印该图像,最后我使用 PrintPr
首先,我想指出,我已将此问题作为一个错误提交给 Microsoft,但他们此时不愿修复它。我正在寻找一种解决方法或更好的方法来实现我正在尝试做的事情,因为我们的客户认为这是一个相当重要的问题。 代码
如果用户打印报告,并且他们碰巧正在使用 微软 XPS 打印机 ,我希望默认文件名有意义。 我原以为 XPS 打印机会采用打印作业的名称,并将其用作默认文件名 - 但事实并非如此。 当我打印到该打印机时
我有一个 C# WinForms 应用程序。用户上传一个 .XPS 文件并指定一些打印机设置(份数、纸盒等)。该程序需要以编程方式打印具有这些设置的文档。即,不能有要打印的用户交互。 我可以接近 Sy
我的代码生成一个 saveFileDialog 来保存一个 .xps 文件,而不是实际打印任何东西。我的代码有问题吗?如果没有,我如何配置我的机器才能使此代码正常工作? class Printer {
我需要用 C# 保护 XPS 文档,但我找不到任何库,请帮助我... 谢谢。 最佳答案 我不完全确定“保护 XPS 文档”是什么意思。 如果您的意思是添加签名以便读者可以验证文档是否由您生成,我建议您
我需要拆分现有的 XPS 文档并创建一个只有原始文档的一页的新 XPS 文档。我尝试复制文档并从复制的文档中删除页面,但速度很慢。有没有更有效的方法来做到这一点?请使用 C#。 谢谢。 已解决: pu
我是一名优秀的程序员,十分优秀!