简体   繁体   中英

XpsDocument.GetFixedDocumentSequence throws XamlParseException

I have to import an .xlsx file into my WPF application and view it. I convert the document to . xps and then loading. After that I call GetFixedDocumentSequence() and there I get this Exception

XamlParseException:

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

here is my code:

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

It looks there was something wrong with the Excel conversion to XPS. That's usually associated with a Glyphs or Font problem. You can open your XPS file (I use this extension on Visual Studio: http://visualstudiogallery.msdn.microsoft.com/450a00e3-5a7d-4776-be2c-8aa8cec2a75b?SRC=VSIDE ) to see where the problem is coming from. Navigate to the page generating the error and find the Glyphs containing the problem.

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

You can also find more documentation regarding this ar MSDN: http://msdn.microsoft.com/en-us/library/ms748985.aspx

Since use are using Excel Interop to generate the XPS you will have almost no control of the conversion procedure, so I would suggest to review the fonts used on the Excel document. Maybe there's some issue with the fonts being used.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM