【问题标题】:Saving a wpf layout to pdf using pdfsharp, c#使用 pdfsharp,c# 将 wpf 布局保存为 pdf
【发布时间】:2013-12-04 10:08:42
【问题描述】:

我是 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>

我现在想使用 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);

我得到一个 pdf 文件,但里面只有文字。有人可以告诉我,我如何将整个布局保存为pdf? 谢谢

【问题讨论】:

  • 我可以知道你用来支持缩放的控件 是一些外部工具吗?
  • 我不太记得了,因为太久以前了,但我认为这是我使用的库:wpfextensions.codeplex.com

标签: c# wpf graph pdfsharp quickgraph


【解决方案1】:

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

我不知道您可以直接从 WPF 转换为 PDF,但是它非常简单

使用 WPFXPSPDF。

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

【讨论】:

  • 非常感谢您提供的链接,这可行,但您必须从 PdfSharp 版本 1.31 添加 PdfSharp.Xps.dll,转换为 xps 功能在最新版本 1.32 中不可用。
  • @erti--chris 我在将 xps 转换为 pdf 时丢失了图像?
  • 这个不能在 Windows 8.1 应用程序中运行。是否有任何 Windows 8.1 应用兼容版本可用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-20
  • 1970-01-01
  • 2018-04-30
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 2013-07-23
相关资源
最近更新 更多