使用pdfsharp保存wpf布局为pdf

使用pdfsharp保存wpf布局为pdf

本文介绍了使用pdfsharp保存wpf布局为pdf,c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c#,wpf和pdfsharp库的新手。
这是我的XAML代码:

 < Grid> 
< zoom:ZoomControl>
< graphsharp:GraphLayout x:Name =graphLayout
Graph ={Binding ElementName = root,Path = GraphToVisualize}
LayoutAlgorithmType =FR
OverlapRemovalAlgorithmType = FSA
HighlightAlgorithmType =简单>< / graphsharp:GraphLayout>
< / zoom:ZoomControl>
< Button Content =ButtonHeight =23Name =button1Width =75Margin =12,294,658,412Click =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?
thanks

解决方案

阅读文档:



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



带有WPF< - > XPS< - > PDF。 b

  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是您的Visual / layout

来源:




I'm new to c#, wpf and the pdfsharp library.This is my XAML Code:

<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.

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

解决方案

Read documentations: 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

with 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

sources:

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

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

这篇关于使用pdfsharp保存wpf布局为pdf,c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:41