using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Printing;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;
using System.Xml.Linq;
namespace DraggableLabelTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void layoutButton_Click(object sender, RoutedEventArgs e)
{
var win = new LayoutWindow();
win.ShowDialog();
}
private void printButton_Click(object sender, RoutedEventArgs e)
{
var nameLocation = new Point(12, 12);
var nameWidth = double.NaN;
var nameHeight = double.NaN;
var addressLocation = new Point(12, 48);
var addressWidth = double.NaN;
var addressHeight = double.NaN;
var emailLocation = new Point(12, 120);
var emailWidth = double.NaN;
var emailHeight = double.NaN;
var countryLocation = new Point(12, 156);
var countryWidth = double.NaN;
var countryHeight = double.NaN;
var ageLocation = new Point(12, 192);
var ageWidth = double.NaN;
var ageHeight = double.NaN;
var genderLocation = new Point(12, 228);
var genderWidth = double.NaN;
var genderHeight = double.NaN;
var dateLocation = new Point(12, 264);
var dateWidth = double.NaN;
var dateHeight = double.NaN;
if (File.Exists("layout.xml"))
{
XElement layout = XElement.Load("layout.xml");
var elements = layout.Elements().Select(
el => new { Name = el.Name, Top = (double) el.Attribute("Top"), Left = (double) el.Attribute("Left"), Width = (double) el.Attribute("Width"), Height = (double) el.Attribute("Height") });
var element = elements.Where(el => el.Name == "NameLabel").Single();
nameLocation = new Point(element.Left, element.Top);
nameWidth = element.Width;
nameHeight = element.Height;
element = elements.Where(el => el.Name == "AddressLabel").Single();
addressLocation = new Point(element.Left, element.Top);
addressWidth = element.Width;
addressHeight = element.Height;
element = elements.Where(el => el.Name == "EmailLabel").Single();
emailLocation = new Point(element.Left, element.Top);
emailWidth = element.Width;
emailHeight = element.Height;
element = elements.Where(el => el.Name == "CountryLabel").Single();
countryLocation = new Point(element.Left, element.Top);
countryWidth = element.Width;
countryHeight = element.Height;
element = elements.Where(el => el.Name == "AgeLabel").Single();
ageLocation = new Point(element.Left, element.Top);
ageWidth = element.Width;
ageHeight = element.Height;
element = elements.Where(el => el.Name == "GenderLabel").Single();
genderLocation = new Point(element.Left, element.Top);
genderWidth = element.Width;
genderHeight = element.Height;
element = elements.Where(el => el.Name == "DateLabel").Single();
dateLocation = new Point(element.Left, element.Top);
dateWidth = element.Width;
dateHeight = element.Height;
}
using (var xpsStream = new MemoryStream())
{
using (Package package = Package.Open(xpsStream, FileMode.Create, FileAccess.ReadWrite))
{
string packageUriString = "memorystream://data.xps";
var packageUri = new Uri(packageUriString);
PackageStore.AddPackage(packageUri, package);
var xpsDocument = new XpsDocument(package, CompressionOption.Maximum, packageUriString);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
var visual = new DrawingVisual();
using (DrawingContext ctx = visual.RenderOpen())
{
var text = new FormattedText(nameTextBox.Text, System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
text.MaxTextWidth = nameWidth;
text.MaxTextHeight = nameHeight;
ctx.DrawText(text, nameLocation);
text = new FormattedText(addressTextBox.Text, System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
text.MaxTextWidth = addressWidth;
text.MaxTextHeight = addressHeight;
ctx.DrawText(text, addressLocation);
text = new FormattedText(emailTextBox.Text, System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
text.MaxTextWidth = emailWidth;
text.MaxTextHeight = emailHeight;
ctx.DrawText(text, emailLocation);
text = new FormattedText(countryTextBox.Text, System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
text.MaxTextWidth = countryWidth;
text.MaxTextHeight = countryHeight;
ctx.DrawText(text, countryLocation);
text = new FormattedText(ageTextBox.Text, System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
text.MaxTextWidth = ageWidth;
text.MaxTextHeight = ageHeight;
ctx.DrawText(text, ageLocation);
text = new FormattedText(genderComboBox.Text, System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
text.MaxTextWidth = genderWidth;
text.MaxTextHeight = genderHeight;
ctx.DrawText(text, genderLocation);
text = new FormattedText(dateDatePicker.Text, System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black);
text.MaxTextWidth = dateWidth;
text.MaxTextHeight = dateHeight;
ctx.DrawText(text, dateLocation);
}
var printTicket = new PrintTicket();
printTicket.PageMediaSize = new PageMediaSize(816, 1248);
writer.Write(visual, printTicket);
FixedDocumentSequence document = xpsDocument.GetFixedDocumentSequence();
xpsDocument.Close();
var printPreview = new PrintPreview();
printPreview.Owner = this;
printPreview.Document = document;
printPreview.ShowDialog();
PackageStore.RemovePackage(packageUri);
}
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
nameTextBox.Text = "John Smith";
addressTextBox.Text = "2000 West Park Drive\nWestborough\nMA 01581";
emailTextBox.Text = "johnsmith@gmail.com";
countryTextBox.Text = "United States";
ageTextBox.Text = "45";
genderComboBox.SelectedIndex = 0;
dateDatePicker.Text = DateTime.Now.Date.ToShortDateString();
}
}
}