none
请教XAML转XPS异常 RRS feed

  • 问题

  •         void ConvertXamlToXps(string srcXamlFile, string destXpsFile)
            {
                using (FileStream fs = File.Open(srcXamlFile, FileMode.Open))
                {
                    FlowDocument flowDoc = (FlowDocument)XamlReader.Load(fs);
                    if (flowDoc == null)
                    {
                        Debug.Assert(false, "flowDoc is null");
                        return;
                    }

                    flowDoc.PageWidth = 790;
                    flowDoc.PageHeight = 1110;
                    flowDoc.ColumnWidth = 790;
                    XpsDocument xpsDoc = new XpsDocument(destXpsFile, FileAccess.Write);
                    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                    writer.Write(((IDocumentPaginatorSource)flowDoc).DocumentPaginator);
                    xpsDoc.Close();
                }
            }

    XAML转XPS的函数如上,XAML为图文混合的表格,其中PageWidth和PageHeight的数值是通过实验得出的,目的是将XPS打印到A4纸上。

    该函数执行过程中,有时正确转化,有时会抛出如下异常:

    [2012/8/6 16:04:45.923] [ERROR] [ConvertXamlToXps] MS.Internal.PtsHost.UnsafeNativeMethods.PTS+SecondaryException: 算术运算中发生溢出或下溢。 ---> System.ArithmeticException: 算术运算中发生溢出或下溢。 ---> System.ArithmeticException: 算术运算中发生溢出或下溢。
       在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       在 MS.Internal.Text.TextInterface.Native.Util.ConvertHresultToException(Int32 hr)
       在 MS.Internal.Text.TextInterface.TextAnalyzer.GetGlyphs(UInt16* textString, UInt32 textLength, Font font, UInt16 blankGlyphIndex, Boolean isSideways, Boolean isRightToLeft, CultureInfo cultureInfo, DWriteFontFeature[][] features, UInt32[] featureRangeLengths, UInt32 maxGlyphCount, TextFormattingMode textFormattingMode, ItemProps itemProps, UInt16* clusterMap, UInt16* textProps, UInt16* glyphIndices, UInt32* glyphProps, Int32* pfCanGlyphAlone, UInt32& actualGlyphCount)
       在 MS.Internal.TextFormatting.LineServicesCallbacks.GetGlyphsRedefined(IntPtr pols, IntPtr* plsplsruns, Int32* pcchPlsrun, Int32 plsrunCount, Char* pwchText, Int32 cchText, LsTFlow textFlow, UInt16* puGlyphsBuffer, UInt32* piGlyphPropsBuffer, Int32 cgiGlyphBuffers, Int32& fIsGlyphBuffersUsed, UInt16* puClusterMap, UInt16* puCharProperties, Int32* pfCanGlyphAlone, Int32& glyphCount)
       --- 内部异常堆栈跟踪的结尾 ---
       在 MS.Internal.TextFormatting.TextMetrics.FullTextLine.FormatLine(FullTextState fullText, Int32 cpFirst, Int32 lineLength, Int32 formatWidth, Int32 finiteFormatWidth, Int32 paragraphWidth, LineFlags lineFlags, FormattedTextSymbols collapsingSymbol)
       在 MS.Internal.TextFormatting.TextMetrics.FullTextLine..ctor(FormatSettings settings, Int32 cpFirst, Int32 lineLength, Int32 paragraphWidth, LineFlags lineFlags)
       在 MS.Internal.TextFormatting.TextFormatterImp.FormatLineInternal(TextSource textSource, Int32 firstCharIndex, Int32 lineLength, Double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak, TextRunCache textRunCache)
       在 MS.Internal.TextFormatting.TextFormatterImp.FormatLine(TextSource textSource, Int32 firstCharIndex, Double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak, TextRunCache textRunCache)
       在 MS.Internal.PtsHost.Line.Format(FormattingContext ctx, Int32 dcp, Int32 width, Int32 trackWidth, TextParagraphProperties lineProps, TextLineBreak textLineBreak)
       在 MS.Internal.PtsHost.TextParagraph.FormatLineCore(Line line, IntPtr pbrLineIn, FormattingContext ctx, Int32 dcp, Int32 width, Int32 trackWidth, Boolean firstLine, Int32 dcpLine)
       在 MS.Internal.PtsHost.TextParagraph.FormatLine(TextParaClient paraClient, Int32 iArea, Int32 dcp, IntPtr pbrlineIn, UInt32 fswdir, Int32 urStartLine, Int32 durLine, Int32 urStartTrack, Int32 durTrack, Int32 urPageLeftMargin, Boolean fAllowHyphenation, Boolean fClearOnLeft, Boolean fClearOnRight, Boolean fTreatAsFirstInPara, Boolean fTreatAsLastInPara, Boolean fSuppressTopSpace, IntPtr& lineHandle, Int32& dcpLine, IntPtr& ppbrlineOut, Int32& fForcedBroken, FSFLRES& fsflres, Int32& dvrAscent, Int32& dvrDescent, Int32& urBBox, Int32& durBBox, Int32& dcpDepend, Int32& fReformatNeighborsAsLastLine)
       在 MS.Internal.PtsHost.PtsHost.FormatLine(IntPtr pfsclient, IntPtr pfsparaclient, IntPtr nmp, Int32 iArea, Int32 dcp, IntPtr pbrlineIn, UInt32 fswdir, Int32 urStartLine, Int32 durLine, Int32 urStartTrack, Int32 durTrack, Int32 urPageLeftMargin, Int32 fAllowHyphenation, Int32 fClearOnLeft, Int32 fClearOnRight, Int32 fTreatAsFirstInPara, Int32 fTreatAsLastInPara, Int32 fSuppressTopSpace, IntPtr& pfsline, Int32& dcpLine, IntPtr& ppbrlineOut, Int32& fForcedBroken, FSFLRES& fsflres, Int32& dvrAscent, Int32& dvrDescent, Int32& urBBox, Int32& durBBox, Int32& dcpDepend, Int32& fReformatNeighborsAsLastLine)
       --- 内部异常堆栈跟踪的结尾 ---
       在 MS.Internal.PtsHost.UnsafeNativeMethods.PTS.ErrorTrace(Int32 fserr, PtsContext ptsContext)
       在 MS.Internal.PtsHost.PtsPage.CreateFinitePage(PageBreakRecord breakRecord)
       在 MS.Internal.PtsHost.FlowDocumentPage.FormatFinite(Size pageSize, Thickness pageMargin, PageBreakRecord breakRecord)
       在 MS.Internal.Documents.FlowDocumentPaginator.FormatPage(Int32 pageNumber)
       在 MS.Internal.Documents.FlowDocumentPaginator.GetPage(Int32 pageNumber)
       在 System.Windows.Xps.Serialization.Toolbox.GetPage(DocumentPaginator paginator, Int32 index)
       在 System.Windows.Xps.Serialization.DocumentPaginatorSerializer.PersistObjectData(SerializableObjectContext serializableObjectContext)
       在 System.Windows.Xps.Serialization.XpsSerializationManager.SaveAsXaml(Object serializedObject)
       在 System.Windows.Xps.XpsDocumentWriter.SaveAsXaml(Object serializedObject, Boolean isSync)

    请高手帮助,谢谢。

    2012年8月9日 0:57

答案