I am creating a WPF application that will be used to print tags. I want to create a shortcut template as a WPF window. In another class, I will create an instance of this “window template”, fill in the properties at runtime, and print the label. I cannot show the shortcut on the screen before printing, so I cannot call .ShowDialog () on this instance of the window. It comes into play later.
I have been doing this research last week, and I found two ways that almost do what I want separately, and if I could combine them, it would work, but I’m missing a piece.
I could get this to work, but after the steps here,
Printing in WPF Part 2
This provides the basics of printing a document. However, I could not use my template, and I would have to position everything in the code. This is a viable option, but I would like to learn more about the idea of the template.
PrintDialog pd = new PrintDialog();
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = new System.Windows.Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
FixedPage page1 = new FixedPage();
page1.Width = document.DocumentPaginator.PageSize.Width;
page1.Height = document.DocumentPaginator.PageSize.Height;
Label _lblBarcode = new Label();
_lblBarcode.Content = BarcodeConverter128.StringToBarcode(palletID);
_lblBarcode.FontFamily = new System.Windows.Media.FontFamily("Code 128");
_lblBarcode.FontSize = 40;
_lblBarcode.Margin = new Thickness(96);
page1.Children.Add(_lblBarcode);
PageContent page1Content = new PageContent();
((IAddChild)page1Content).AddChild(page1);
document.Pages.Add(page1Content);
pd.PrintQueue = new System.Printing.PrintQueue(new System.Printing.PrintServer(), "CutePDF Writer");
pd.PrintDocument(document.DocumentPaginator, "PalletID");
I will just show how I type the barcode here. In a real program, I would add everything to the label and place it.
In this class, I create an instance of my shortcut, populate its properties and try to add it to the FixedPage children, but it returns this error.
Specified element is already the logical child of another element. Disconnect it first.
To get around this, I can remove each user interface element from the template and then add it to my fixed document. This does not seem like the cleanest solution, but it is possible.
UIElement , . , - , .
,
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
, , ! , . , , UIelements , . , Windows_Loaded . Windows_Loaded WindowInstance.ShowDialog();, , , , .
, , .
. , , , . :
, ,
, Lena View.xaml, View.xaml.cs, ViewModel.cs, View.xaml
private void StackOverFlow()
{
string palletID = "00801004018000020631";
PrintDialog pd = new PrintDialog();
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
fixedDoc.DocumentPaginator.PageSize = new System.Windows.Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
fixedPage.Width = fixedDoc.DocumentPaginator.PageSize.Width;
fixedPage.Height = fixedDoc.DocumentPaginator.PageSize.Height;
fixedPage.Width = 4.0 * 96;
fixedPage.Height = 3.0 * 96;
var pageSize = new System.Windows.Size(4.0 * 96.0, 3.0 * 96.0);
View v = new View();
ViewModel vm = new ViewModel();
vm.Text1 = "MyText1";
vm.Text2 = "MyText2";
v.DataContext = vm;
v.Height = pageSize.Height;
v.Width = pageSize.Width;
v.UpdateLayout();
fixedPage.Children.Add(v);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
XpsDocumentWriter dw1 = PrintQueue.CreateXpsDocumentWriter(new System.Printing.PrintQueue(new System.Printing.PrintServer(), "CutePDF Writer"));
dw1.Write(fixedDoc);
}