首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >保存一个wpf布局到pdf使用pdfsharp,c#

保存一个wpf布局到pdf使用pdfsharp,c#
EN

Stack Overflow用户
提问于 2013-12-04 18:08:43
回答 1查看 20.2K关注 0票数 4

我刚接触c#、wpf和pdfsharp库。这是我的XAML代码:

代码语言:javascript
运行
复制
<Grid>
    <zoom:ZoomControl>
    <graphsharp:GraphLayout x:Name="graphLayout"
                            Graph="{Binding ElementName=root, Path=GraphToVisualize}" 
                            LayoutAlgorithmType="FR"
                            OverlapRemovalAlgorithmType="FSA"
                            HighlightAlgorithmType="Simple"></graphsharp:GraphLayout>
    </zoom:ZoomControl>
    <Button Content="Button" Height="23" Name="button1" Width="75" Margin="12,294,658,412" Click="button1_Click" />
</Grid>

现在我想用Pdfsharp将我的"graphLayout“保存到一个pdf文件中。我创建了一个按钮,并基本上使用了pdfsharp wiki中的"hello world“示例代码作为开始。

代码语言:javascript
运行
复制
PdfDocument document = new PdfDocument();
        document.Info.Title = "Created with PDFsharp";
        PdfPage page = document.AddPage(); 
        XGraphics gfx = XGraphics.FromPdfPage(page);
        XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
        gfx.DrawString("My Graph", font, XBrushes.Black,
            new XRect(0, 0, page.Width, page.Height),
            XStringFormats.TopCenter);
        const string filename = "MyGraph.pdf";
        document.Save(graphLayout+filename);
        Process.Start(filename);

我得到了一个pdf,但里面只有文本。谁能告诉我,我怎样才能将整个布局保存到pdf中?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-04 18:23:21

阅读文档:http://www.pdfsharp.net/wiki/Graphics.ashx?AspxAutoDetectCookieSupport=1

我不知道你可以直接从WPF转换成PDF,但它很简单

使用WPF<-->XPS<-->PDF。

代码语言:javascript
运行
复制
MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);
XpsDocument doc = new XpsDocument(package);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
writer.Write(dp);
doc.Close();
package.Close();

var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream);
PdfSharp.Xps.XpsConverter.Convert(pdfXpsDoc, d.FileName, 0);

其中dp是您的视觉/布局

资料来源:

http://www.nathanpjones.com/wp/2013/03/output-to-pdf-in-wpf-for-free/

http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20372379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档