如何使用XamlToPDF库C#将FlowDocument保存为PDF

时间:2016-11-04 06:10:44

标签: c# wpf pdf-generation flowdocument

我正在使用 XamlToPDF 第三方库并使用以下代码

byte[] data = null;

string stringXAMLFlowDoc = System.Windows.Markup.XamlWriter.Save(flowDocument);

NeuroSpeech.XamlToPDF.Xaml.XamlPdfWriter writer = new NeuroSpeech.XamlToPDF.Xaml.XamlPdfWriter();

using (MemoryStream stream = new MemoryStream())
{
     writer.Write(stringXAMLFlowDoc, stream); //Following exception raised at this line
     data = stream.ToArray();
}

获得以下例外:

-       InnerException  {"I/O error occurred."} System.Exception {System.IO.IOException}
+       Data    {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
        HResult -2146232800 int
        HelpLink    null    string
+       InnerException  null    System.Exception
        Message "I/O error occurred."   string
        Source  "mscorlib"  string
        StackTrace  "   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)\r\n   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode, IntPtr errorInfo)\r\n   at MS.Internal.Text.TextInterface.Native.Util.ConvertHresultToException(Int32 hr)\r\n   at MS.Internal.Text.TextInterface.Factory.CreateFontFace(Uri filePathUri, UInt32 faceIndex, FontSimulations fontSimulationFlags)\r\n   at System.Windows.Media.GlyphTypeface.Initialize(Uri typefaceSource, StyleSimulations styleSimulations)\r\n   at System.Windows.Documents.Glyphs.ParseGlyphRunProperties()\r\n   at System.Windows.Documents.Glyphs.ComputeMeasurementGlyphRunAndOrigin()\r\n   at System.Windows.Documents.Glyphs.MeasureOverride(Size constraint)\r\n   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)\r\n   at System.Windows.UIElement.Measure(Size availableSize)\r\n   at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)\r\n   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)\r\n   at System.Windows.UIElement.Measure(Size availableSize)\r\n   at System.Windows.Documents.FixedPage.MeasureOverride(Size constraint)\r\n   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)\r\n   at System.Windows.UIElement.Measure(Size availableSize)\r\n   at MS.Internal.Documents.UIElementIsland.DoLayout(Size availableSize, Boolean horizontalAutoSize, Boolean verticalAutoSize)\r\n   at MS.Internal.PtsHost.UIElementParagraph.FormatUIElement(Int32 durAvailable, FSBBOX& fsbbox)\r\n   at MS.Internal.PtsHost.UIElementParagraph.FormatFloaterContentFinite(FloaterBaseParaClient paraClient, IntPtr pbrkrecIn, Int32 fBRFromPreviousPage, IntPtr footnoteRejector, Int32 fEmptyOk, Int32 fSuppressTopSpace, UInt32 fswdir, Int32 fAtMaxWidth, Int32 durAvailable, Int32 dvrAvailable, FSKSUPPRESSHARDBREAKBEFOREFIRSTPARA fsksuppresshardbreakbeforefirstparaIn, FSFMTR& fsfmtr, IntPtr& pfsFloatContent, IntPtr& pbrkrecOut, Int32& durFloaterWidth, Int32& dvrFloaterHeight, FSBBOX& fsbbox, Int32& cPolygons, Int32& cVertices)\r\n   at MS.Internal.PtsHost.PtsHost.FormatFloaterContentFinite(IntPtr pfsclient, IntPtr pfsparaclient, IntPtr pfsbrkFloaterContentIn, Int32 fBreakRecordFromPreviousPage, IntPtr nmFloater, IntPtr pftnrej, Int32 fEmptyOk, Int32 fSuppressTopSpace, UInt32 fswdirTrack, Int32 fAtMaxWidth, Int32 durAvailable, Int32 dvrAvailable, FSKSUPPRESSHARDBREAKBEFOREFIRSTPARA fsksuppresshardbreakbeforefirstparaIn, FSFMTR& fsfmtr, IntPtr& pfsFloatContent, IntPtr& pbrkrecpara, Int32& durFloaterWidth, Int32& dvrFloaterHeight, FSBBOX& fsbbox, Int32& cPolygons, Int32& cVertices)"    string

任何人都可以帮助将FlowDocument保存为PDF格式。

基本上我正在尝试从包含用户控件的Canvas创建一个PDF文档作为WPF中的子项。

0 个答案:

没有答案