【问题标题】:XpsDocument.GetFixedDocumentSequence throws XamlParseExceptionXpsDocument.GetFixedDocumentSequence 引发 XamlParseException
【发布时间】:2015-01-02 16:43:11
【问题描述】:

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

XamlParseException:

{"UnicodeString 属性没有包含足够的字符 对应于 Indices 属性的内容。"}.

这是我的代码:

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;
}

【问题讨论】:

    标签: c# wpf documentviewer xamlparseexception


    【解决方案1】:

    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 文档中使用的字体。 可能是使用的字体有问题。

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2011-07-16
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 2019-01-26
      相关资源
      最近更新 更多