4

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

AndyB
  • 391
  • 2
  • 4
  • 17
  • May i know the control that you are using to support zoom Is that some external tools? – Sebastian Mar 09 '16 at 06:12
  • I can't really remember, since it's too long ago, but I think this was the library I used: http://wpfextensions.codeplex.com/ – AndyB Mar 09 '16 at 13:05

1 Answers1

13

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

Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
  • 3
    thanks a lot for the link, this works, but you have to add the PdfSharp.Xps.dll form the PdfSharp Version 1.31, the convert to xps feature is not available in the latest version 1.32. – AndyB Dec 04 '13 at 16:25
  • @erti--chris i have lost images while converting xps into pdf ? – Meer Apr 30 '16 at 06:11
  • This one is not working in windows 8.1 app. Is there any windows 8.1 app compatible version available? – Saadi May 16 '18 at 03:53