I'm currently working on a printing application. This app has the requirement that certain pages need to come from specific trays on the printer. Here's the guts of what I've got so far:
foreach (var dto in dispensersToPrint)
{
var documents = FilterDocumentSections(DispenserDocumentsToPrint.RetrieveByDispenserId(dto.DispenserId));
var groupedDocs = documents.GroupBy(t => t.DocumentTypeId);
var queueName = Properties.Settings.Default.PrinterName;
var queue = RawPrinterHelper.GetPrintQueue(queueName);
var seq = new FixedDocumentSequence();
var xpsWriter = PrintQueue.CreateXpsDocumentWriter(queue);
foreach (var docGroup in groupedDocs)
{
var printTicket = queue.DefaultPrintTicket.Clone();
var printTray = MapPrintTray((DocumentSectionType)docGroup.Key);
if (!printTray.IsNullOrEmpty())
{
printTicket = RawPrinterHelper.ModifyPrintTicket(printTicket, "psk:JobInputBin", printTray);
}
var fixedDoc = new FixedDocument();
fixedDoc.PrintTicket = printTicket;
foreach (var doc in docGroup)
{
var pageContent = new PageContent();
var fixedPage = new FixedPage();
var localFileName = string.Empty;
var unzippedFileName = string.Empty;
//copy files locally
localFileName = CopyFileToLocalMachine(doc.FileName);
//unzip file
unzippedFileName = EmfPrintingHelper.UnzipEmfFile(localFileName);
var itemToPrint = new PrintableEmfImage
{
DataContext = new EmfImageViewModel { FileName = unzippedFileName }
};
fixedPage.Children.Add(itemToPrint);
pageContent.Child = fixedPage;
fixedDoc.Pages.Add(pageContent);
}
var docRef = new DocumentReference();
docRef.SetDocument(fixedDoc);
seq.References.Add(docRef);
}
xpsWriter.Write(seq);
}
At a real high level:
For each Dispenser (Work Order) i need to print; i first start by grouping by the DocumentType (i.e. Print type A to tray 1)
I then create a new FixedDocumentSequence
For each DocumentType; I then create a fixed document. I then modify the print ticket to look at the appropriate tray.
I then build each individual page for each document type; and add them to the FixedDocument
Once the building of the FixedDocument is complete; I append it to the DocumentSequence.
I then send the FixedDocumentSequence to the xpsWriter.
But for some reason; these settings aren't being honored. I get all the documents printing out of the same tray.
Here are some of my observations so far:
The modifying of the print ticket does work; I've verified this by sending a modified printTicket into the xpsWriter; but this applies the settings to the entire job; which is a no go for me.
When querying my print capabilities; i noticed that i only have JobInputBin. I don't quite think this means this printer doesn't support the functionality; as multi-tray printing works from a similar WindowsForms app (which uses PageSettings.PaperSource)
Any ideas on what I could try next? Has anyone been successful doing something like this before?
I'll start off by saying, I don't have access to a printer with trays, so I am unfortunately not capable of testing this solution. That said, I'll direct your attention to an MSDN forum post, here, where the original poster was in pursuit of the same tray-per-page behavior.
Based on your posted code, you may have already seen some of what's in this post, judging by your posted code having at least some implementation of ModifyPrintTicket().
In the post, there are several different users, each citing a solution for their specific version of the problem. However, the one that seems most relevant in this case is the solution regarding namespaces not being correctly accounted for in ModifyPrintTicket() (as posted by
Jo0815). I say 'most relevant' because the poster speaks of the print tray being disregarded. They (wittersworld) provide an alternate implementation to correct the issue. In the post on MSDN, the link to the complete source is broken, but can be located here.
The gist is, on ModifyPrintTicket(), they add a namespaceUri parameter, then withing changed this:
if (node != null)
{
node.Attributes["name"].Value = newValue;
}
to this:
if (node != null)
{
if (newValue.StartsWith("ns0000"))
{
// add namespace to xml doc
XmlAttribute namespaceAttribute = xmlDoc.CreateAttribute("xmlns:ns0000");
namespaceAttribute.Value = namespaceUri;
xmlDoc.DocumentElement.Attributes.Append(namespaceAttribute);
}
node.Attributes["name"].Value = newValue;
}
allowing the user to specify the printer-specific namespace used.
I hope this is helpful.
Here's a snippet of code I have to print a document in WPF. At a high level, I instantiate a UserControl, and then send it to a printer.
var printQueue = GetQueues().Where(t => t.Name == comboBox1.SelectedItem.ToString()).FirstOrDefault();
var defaultPrintTicket = printQueue.DefaultPrintTicket.Clone();
var newTicket = ModifyPrintTicket(defaultPrintTicket, "psk:JobInputBin",
((PrintTrays)listBox1.SelectedItem).ConfigValue);
var xpsWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);
var controlToPrint = new PackingSlip();
var fixedDoc = new FixedDocument();
var pageContent = new PageContent();
var fixedPage = new FixedPage();
fixedPage.Children.Add(controlToPrint);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
xpsWriter.Write(fixedDoc, newTicket);
What I'm wondering about is that over time, I'll have created hundreds of the instances of controlToPrint. Am I going to run into memory issues here, or do they get disposed of automatically in some fashion? If they aren't disposed automatically, how would I free up that memory?
They will be disposed of automatically assuming that fixedDoc, fixedPage, pagecontent and controlToPrint go out of scope at the end of the method. If application roots hold onto any reference to any of those objects then you will get a memory leak
Is there any way to render a Silverlight text to a path or graphics object at runtime? I know this can be done using the design tools, but I want to be able to do this on the fly.
I've seen an example that calls a webservice which uses WPF constructs to convert a WPF FormattedText object to a PathGeometry, but those objects aren't available in Silverlight.
I'm pretty sure this just isn't supported in Silverlight, but thought it was worth asking.
As you suspected, you'd have to do it server-side to convert the text to a PathGeometry which is supported in Silverlight.
What are you trying to achieve?
Here is the code for creating a SilverLight path Geometry dynamically.
or creating a SilverLight path from a string.
Just paste below method in a new class or in existing class and it will be ready to use.
you can find a sample code to use this functions at bottom of the method.
/// Method tested with SilverLight 2.0
// this method will generates path with the data string
public PathGeometry getPathGeometry(string data)
{
PathGeometry pg = new PathGeometry();
PathSegmentCollection psc = new PathSegmentCollection();
PathFigure pf = new PathFigure();
PathFigureCollection pfc = new PathFigureCollection();
data= data.Replace("M "," M").Replace("C "," C").Replace("L "," L");
string[] str = data.Split(' ');
for (int i = 0; i < str.Length; i++)
{
if (str[i].StartsWith("C") || str[i].StartsWith("c"))
{
string[] item = str[i].Split(',');
string[] item1 = str[i + 1].Split(',');
string[] item2 = str[i + 2].Split(',');
BezierSegment bs = new BezierSegment();
bs.Point1 = new Point(double.Parse(item[0].Substring(1)), double.Parse(item[1]));
bs.Point2 = new Point(double.Parse(item1[0]), double.Parse(item1[1]));
bs.Point3 = new Point(double.Parse(item2[0]), double.Parse(item2[1]));
i += 2;
psc.Add(bs);
}
else if (str[i].StartsWith("L") || str[i].StartsWith("l"))
{
string[] item = str[i].Split(',');
LineSegment ls = new LineSegment();
ls.Point = new Point(double.Parse(item[0].Substring(1)), double.Parse(item[1]));
psc.Add(ls);
}
else if (str[i].StartsWith("M") || str[i].StartsWith("m"))
{
string[] item = str[i].Split(',');
pf.StartPoint = new Point(double.Parse(item[0].Substring(1)), double.Parse(item[1]));
}
else if (str[i].StartsWith("z") || str[i].StartsWith("Z"))
{
pf.IsClosed = true;
}
}
pf.Segments = psc;
pfc.Add(pf);
pg.Figures = pfc;
return pg;
}
///// End of Method
Sample Code for calling method
Path path = new Path();
string str = " F1 M 933.291,430.505C 924.367,415.673 923.007,387.822 922.503,370.604C 921.343,331.31 944.994,317.76 975.999,296.994L 949.334,299.957C 938.729,302.545 930.572,309.925 920.255,313.368C 901.85,319.521 886.504,313.062 870.896,303.53C 850.12,290.842 831.457,270.65 815.107,251.462C 806.279,241.101 798.257,221.598 781.986,226.017C 767.327,229.99 760.199,246.869 743.058,244.012C 737.559,227.262 741.368,204.78 739.591,187.029C 738.108,172.136 733.986,158.933 733.996,143.736C 734.003,128.417 734.091,113.088 733.996,97.7689C 733.909,83.5475 730.302,82.6582 716.114,86.0475C 687.558,92.8796 663.68,115.232 634.418,119.337C 622.391,121.028 598.323,121.184 603.745,103.642C 603.745,103.642 547.667,116.478 522.623,101.969L 397.73,43.1915C 374.54,33.5875 352.799,21.5236 330.186,10.7568C 315.067,3.55951 298.84,3.50623 282.684,6.54358C 268.628,9.18353 252.14,8.36884 238.73,13.0222C 227.932,16.7648 225.711,27.0569 220.839,35.6369C 204.622,64.1582 184.474,89.9609 163.49,115.642C 143.3,140.356 124.747,161.949 100.268,182.977C 76.4618,203.437 58.0045,230.722 39.6698,256.062C 27.9845,272.228 10.5298,295.73 5.62447,315.546C 1.21381,333.368 7.65381,345.95 16.7778,360.225C 30.9738,382.42 52.4365,394.917 74.4578,408.658C 108.356,429.826 144.964,432.43 182.619,439.202C 194.226,441.284 201.93,444.466 212.456,450.234C 228.9,459.261 246.18,466.181 262.031,476.002C 277.378,485.518 288.175,498.328 306.771,498.502C 331.423,498.729 342.159,498.364 359.554,517.221C 368.632,527.06 372.859,537.585 380.38,548.114C 395.159,568.82 409.076,590.689 426.295,609.442C 440.326,624.728 467.967,633.601 487.652,636.902C 505.622,639.908 521.979,632.736 535.859,620.806C 545.402,612.606 552.478,602.246 557.978,591.161C 561.915,583.213 564.966,568.085 572.399,564.296C 578.046,561.41 595.117,563.91 601.338,564.312C 612.171,565.009 621.722,568.994 632.552,569.976C 651.071,571.65 654.679,567.992 668.187,558.989C 681.275,550.254 697.746,547.268 711.451,538.109C 733.726,523.208 751.861,501.273 773.035,484.254C 795.099,466.53 815.65,437.337 845.207,434.924C 871.813,432.754 933.291,430.505 933.291,430.505 Z";
path.Data = getPathGeometry(str);
I'm building a demo app in WPF, which is new to me. I'm currently displaying text in a FlowDocument, and need to print it.
The code I'm using looks like this:
PrintDialog pd = new PrintDialog();
fd.PageHeight = pd.PrintableAreaHeight;
fd.PageWidth = pd.PrintableAreaWidth;
fd.PagePadding = new Thickness(50);
fd.ColumnGap = 0;
fd.ColumnWidth = pd.PrintableAreaWidth;
IDocumentPaginatorSource dps = fd;
pd.PrintDocument(dps.DocumentPaginator, "flow doc");
fd is my FlowDocument, and for now I'm using the default printer instead of allowing the user to specify print options. It works OK, except that after the document prints, the FlowDocument displayed on screen has changed to to use the settings I specified for printing.
I can fix this by manually resetting everything after I print, but is this the best way? Should I make a copy of the FlowDocument before I print it? Or is there another approach I should consider?
yes, make a copy of the FlowDocument before printing it. This is because the pagination and margins will be different. This works for me.
private void DoThePrint(System.Windows.Documents.FlowDocument document)
{
// Clone the source document's content into a new FlowDocument.
// This is because the pagination for the printer needs to be
// done differently than the pagination for the displayed page.
// We print the copy, rather that the original FlowDocument.
System.IO.MemoryStream s = new System.IO.MemoryStream();
TextRange source = new TextRange(document.ContentStart, document.ContentEnd);
source.Save(s, DataFormats.Xaml);
FlowDocument copy = new FlowDocument();
TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd);
dest.Load(s, DataFormats.Xaml);
// Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog,
// and allowing the user to select a printer.
// get information about the dimensions of the seleted printer+media.
System.Printing.PrintDocumentImageableArea ia = null;
System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia);
if (docWriter != null && ia != null)
{
DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;
// Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.
paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight);
Thickness t = new Thickness(72); // copy.PagePadding;
copy.PagePadding = new Thickness(
Math.Max(ia.OriginWidth, t.Left),
Math.Max(ia.OriginHeight, t.Top),
Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right),
Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom));
copy.ColumnWidth = double.PositiveInfinity;
//copy.PageWidth = 528; // allow the page to be the natural with of the output device
// Send content to the printer.
docWriter.Write(paginator);
}
}
You can use the code from the URL below, it wraps the flow document in a fixed document and prints that, the big advantage is that you can use it to add margin, headers and footers.
https://web.archive.org/web/20150502085246/http://blogs.msdn.com:80/b/fyuan/archive/2007/03/10/convert-xaml-flow-document-to-xps-with-style-multiple-page-page-size-header-margin.aspx
The following works with both text and non-text visuals:
//Clone the source document
var str = XamlWriter.Save(FlowDoc);
var stringReader = new System.IO.StringReader(str);
var xmlReader = XmlReader.Create(stringReader);
var CloneDoc = XamlReader.Load(xmlReader) as FlowDocument;
//Now print using PrintDialog
var pd = new PrintDialog();
if (pd.ShowDialog().Value)
{
CloneDoc.PageHeight = pd.PrintableAreaHeight;
CloneDoc.PageWidth = pd.PrintableAreaWidth;
IDocumentPaginatorSource idocument = CloneDoc as IDocumentPaginatorSource;
pd.PrintDocument(idocument.DocumentPaginator, "Printing FlowDocument");
}
I am also generating a WPF report off a Flow document, but I am purposely using the flow document as a print preview screen. I there for want the margins to be the same. You can read about how I did this here.
In your scenario I'm thinking why not just make a copy of your settings, instead of the entire flow document. You can then re-apply the settings if you wish to return the document back to it's original state.