gpt4 book ai didi

c# - XpsDocument.GetFixedDocumentSequence 抛出 XamlParseException

转载 作者:行者123 更新时间:2023-11-30 18:28:42 31 4
gpt4 key购买 nike

我必须将 .xlsx 文件导入我的 WPF 应用程序并查看它。我将文档转换为 .xps 然后加载。之后我调用 GetFixedDocumentSequence() 并在那里我得到了这个异常

XamlParseException:

{"UnicodeString property does not contain enough characters to correspond to the contents of Indices property."}.

这是我的代码:

private void LoadData()
{
string xpsPath = ViewDocumentViewer("D:\\test.xlsx");
DisplayXPSFile(xpsPath);
}

private string ViewDocumentViewer(string path)
{
try
{
string xpsPath;
var excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.DisplayAlerts = false;
excelApp.Visible = false;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(path);
xpsPath = ExportXPS(excelWorkbook, path);
excelWorkbook.Close(false, null, null);
excelApp.Quit();
Marshal.ReleaseComObject(excelApp);
excelApp = null;

return xpsPath;
}
catch
{
}

return string.Empty;
}

string ExportXPS(Microsoft.Office.Interop.Excel.Workbook excelWorkbook, string path)
{
string xpsFileName;
xpsFileName = (new DirectoryInfo(path)).FullName;
xpsFileName = xpsFileName.Replace(new FileInfo(path).Extension, "") + ".xps";
excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS,
Filename: xpsFileName,
OpenAfterPublish: false);

return xpsFileName;
}

void DisplayXPSFile(string xpsFileName)
{
XpsDocument xpsPackage = new XpsDocument(xpsFileName, FileAccess.Read, CompressionOption.NotCompressed);
FixedDocumentSequence fixedDocumentSequence = xpsPackage.GetFixedDocumentSequence();
DocView.Document = fixedDocumentSequence;
}

最佳答案

看起来在将 Excel 转换为 XPS 时出现了问题。这通常与字形或字体问题有关。您可以打开 XPS 文件(我在 Visual Studio 上使用此扩展名:http://visualstudiogallery.msdn.microsoft.com/450a00e3-5a7d-4776-be2c-8aa8cec2a75b?SRC=VSIDE)来查看问题出在哪里。导航到产生错误的页面并找到包含问题的字形。

<Canvas Clip="F 1 M137.710006714,208.58001709 L476.861022949,208.58001709 L476.861022949,208.58001709 L476.861022949,219.83001709 L476.861022949,219.83001709 L137.710006714,219.83001709 Z">
<Glyphs OriginX="0" OriginY="0" UnicodeString="D" Indices=",55.6" Fill="#FF000000" FontRenderingEmSize="1" FontUri="/Resources/076a5115-0000-0000-0000-000000000000.odttf" RenderTransform="8.949999809,0,0,8.949999809,185.458496094,218.330078125" />
</Canvas>

您还可以找到有关此 ar MSDN 的更多文档:http://msdn.microsoft.com/en-us/library/ms748985.aspx

由于使用 Excel Interop 生成 XPS,您几乎无法控制转换过程,因此我建议查看 Excel 文档中使用的字体。可能使用的字体有问题。

问候

关于c# - XpsDocument.GetFixedDocumentSequence 抛出 XamlParseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24410317/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com