none
XpsDocument.GetFixedDocumentSequence throws XamlParseException RRS feed

  • Question

  • I am getting XamlParseException - UnicodeString property does not contain enough characters to correspond to the contents of Indices property while using my below code to parse the .xlsx file to xps file. I have the xps file stored in my temp location it looks fine but when I try to show that in the doc viewer I am getting error when the code runs at xpsDocument.GetFixedDocumentSequence() . 


     Private Sub ViewDocument(ByVal ThisDocument As String)

            If String.IsNullOrEmpty(ThisDocument) OrElse Not File.Exists(ThisDocument) Then
                MessageBox.Show("The file is invalid. Please select an existing file again.")
            Else
                Dim convertedXpsDoc As String = String.Concat(Path.GetTempPath(), "\", Guid.NewGuid().ToString(), ".xps")
                Dim xpsDocument As XpsDocument
                Dim dcviewer As docViewer = New docViewer()
                If ThisDocument.Contains(".xls") Or ThisDocument.Contains(".csv") Then
                    xpsDocument = dcviewer.ConvertExcelToXps(ThisDocument, convertedXpsDoc)
                Else
                    xpsDocument = dcviewer.ConvertWordToXps(ThisDocument, convertedXpsDoc)
                End If

                If xpsDocument Is Nothing Then
                    Return
                End If
                Me.docViewer.Document = xpsDocument.GetFixedDocumentSequence()
            End If
        End Sub

    Regards,

    Kishore

    Thursday, November 24, 2016 12:19 PM

Answers

  • Hi Celeste,

    Thanks for your response, I am using similar code. I found the problem, it is the extra line space in the excel that is not happy in the xps conversion. It is very weird that all the excel format is not agreed by the xps document. So I will ask the user every time to check the excel format and then upload the file or else I have to find the alternative code to read the excel data and then convert the file to suitable format every time I guess. Thanks.

    Tuesday, November 29, 2016 11:54 AM

All replies

  • Hi Kishore,

    This is the forum to discuss questions and feedback for Microsoft Excel, I'll move your question to the MSDN forum for Excel

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=exceldev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.


    Regards,
    Emi Zhang
    TechNet Community Support

    Please remember to mark the replies as answers if they helped.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, November 25, 2016 8:05 AM
  • Hi,

    What code you are using to convert the document? Do you open it manually and check if it convert successfully?

    I am using the code below in a winform application, and it could successfully convert and show.

      Private Sub ViewDocument(ByVal ThisDocument As String)
            Dim docViewer As New System.Windows.Controls.DocumentViewer
            ElementHost1.Child = docViewer
            If String.IsNullOrEmpty(ThisDocument) OrElse Not File.Exists(ThisDocument) Then
                MessageBox.Show("The file is invalid. Please select an existing file again.")
            Else
                Dim convertedXpsDoc As String = String.Concat(Path.GetTempPath(), "\", Guid.NewGuid().ToString(), ".xps")
                Dim xpsDocument As XpsDocument
                If ThisDocument.Contains(".xls") Or ThisDocument.Contains(".csv") Then
                    xpsDocument = ConvertExcelToXps(ThisDocument, convertedXpsDoc)
                Else
                    xpsDocument = ConvertWordToXps(ThisDocument, convertedXpsDoc)
                End If
                If xpsDocument Is Nothing Then
                    Return
                End If
                docViewer.Document = xpsDocument.GetFixedDocumentSequence()
            End If
        End Sub
    
        Private Function ConvertExcelToXps(ExcelDocName As String, xpsDocName As String) As XpsDocument
            Dim excelApplication As New Microsoft.Office.Interop.Excel.Application()
            excelApplication.Workbooks.Add(ExcelDocName)
            Dim doc As Microsoft.Office.Interop.Excel.Workbook = excelApplication.ActiveWorkbook
            Try
                doc.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypeXPS, xpsDocName)
                excelApplication.Quit()
                Dim xpsDoc As New XpsDocument(xpsDocName, System.IO.FileAccess.Read)
                Return xpsDoc
            Catch exp As Exception
                Dim str As String = exp.Message
            End Try
            Return Nothing
        End Function
    
        Private Function ConvertWordToXps(wordDocName As String, xpsDocName As String) As XpsDocument
            Dim wordApplication As New Microsoft.Office.Interop.Word.Application()
            wordApplication.Documents.Add(wordDocName)
            Dim doc As Document = wordApplication.ActiveDocument
            Try
                doc.SaveAs(xpsDocName, WdSaveFormat.wdFormatXPS)
                wordApplication.Quit()
                Dim xpsDoc As New XpsDocument(xpsDocName, System.IO.FileAccess.Read)
                Return xpsDoc
            Catch exp As Exception
                Dim str As String = exp.Message
            End Try
            Return Nothing
        End Function
    

    Regards,

    Celeste



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, November 27, 2016 7:48 AM
    Moderator
  • Hi Celeste,

    Thanks for your response, I am using similar code. I found the problem, it is the extra line space in the excel that is not happy in the xps conversion. It is very weird that all the excel format is not agreed by the xps document. So I will ask the user every time to check the excel format and then upload the file or else I have to find the alternative code to read the excel data and then convert the file to suitable format every time I guess. Thanks.

    Tuesday, November 29, 2016 11:54 AM
  • Hi,

    I am glad that you have the workaround and I suggest you mark your reply as answer.

    If you want to explore more, I would suggest firstly test more different files and check if the code works. Then save the problematic document manually check if the situation is built-in behavior.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 30, 2016 7:41 AM
    Moderator