XpsDocument.GetFixedDocumentSequence抛出XamlParseException

时间:2014-06-25 13:39:43

标签: c# wpf documentviewer xamlparseexception

我必须将.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;
}

1 个答案:

答案 0 :(得分:0)

看起来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>

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

由于使用Excel Interop生成XPS,您几乎无法控制转换过程,因此我建议您查看Excel文档中使用的字体。 也许在使用的字体方面存在一些问题。

此致