使用 pdfsharp、c# 将 wpf 布局保存为 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20372379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving a wpf layout to pdf using pdfsharp, c#
提问by AndyB
I'm new to c#, wpf and the pdfsharp library. This is my XAML Code:
我是 c#、wpf 和 pdfsharp 库的新手。这是我的 XAML 代码:
<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>
I want now save my "graphLayout" to a pdf-file using Pdfsharp. I created a button and used basically the "hello world" sample code in the pdfsharp wiki to start.
我现在想使用 Pdfsharp 将我的“graphLayout”保存到 pdf 文件。我创建了一个按钮,并基本上使用了 pdfsharp wiki 中的“hello world”示例代码来启动。
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);
I get a pdf , but there is just the text in it. Can somebody tell me please, how i can save the whole layout into a pdf? thanks
我得到一个 pdf ,但里面只有文本。有人可以告诉我,我如何将整个布局保存为 pdf?谢谢
回答by Erti-Chris Eelmaa
Read documentations: http://www.pdfsharp.net/wiki/Graphics.ashx?AspxAutoDetectCookieSupport=1
阅读文档:http: //www.pdfsharp.net/wiki/Graphics.ashx?AspxAutoDetectCookieSupport=1
I am not aware that you can convert directly from WPF to PDF, however it's pretty simple
我不知道您可以直接从 WPF 转换为 PDF,但这很简单
with WPF<-->XPS<-->PDF.
使用 WPF<-->XPS<-->PDF。
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);
where dp is your Visual/layout
其中 dp 是您的视觉/布局
sources:
来源:
http://www.nathanpjones.com/wp/2013/03/output-to-pdf-in-wpf-for-free/
http://www.nathanpjones.com/wp/2013/03/output-to-pdf-in-wpf-for-free/

