using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using System.IO.Packaging;
using System.IO;
using System.Windows.Xps.Packaging;
using System.Printing;
using System.Windows.Xps;
namespace NiXPSObjects
{
class WPF2PDF
{
static Visual CreateVisual()
{
const double Inch = 96;
DrawingVisual visual = new DrawingVisual();
DrawingContext dc = visual.RenderOpen();
Pen bluePen = new Pen(Brushes.Blue, 1);
dc.DrawRectangle(Brushes.Yellow, bluePen, new Rect(Inch / 2, Inch / 2, Inch * 1.5, Inch * 1.5));
Brush pinkBrush = new SolidColorBrush(Color.FromArgb(128, 255, 0, 255));
Pen blackPen = new Pen(Brushes.Black, 1);
dc.DrawEllipse(pinkBrush, blackPen, new Point(Inch * 2.25, Inch * 2), Inch * 1.25, Inch);
dc.Close();
return visual;
}
public static void Main(string[] args)
{
try
{
// create XPS file based on a WPF Visual, and store it in a memorystream
MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);
XpsDocument doc = new XpsDocument(package);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
writer.Write(CreateVisual());
doc.Close();
package.Close();
// now open this XPS stream in the NiXPS SDK, and export it as pdf
NOPackage lPackage = NOPackage.readPackageFromBuffer("file.xps", lMemoryStream.GetBuffer(), (uint) lMemoryStream.Length);
NOProgressReporter lReporter = new NOProgressReporter();
lPackage.getDocument(0).exportToPDF("file.pdf", lReporter);
NOPackage.destroyPackage(ref lPackage);
} catch (NOException e)
{
System.Console.Out.WriteLine( "EXCEPTION: 0x" + string.Format("{0:X}", e.getError()) );
}
}
}
}
Subscribe to
Posts [Atom]