Here is my scenario:
I have a document, currently a FlowDocument, that I would like to print on two different page sizes. The first is standard letter 8.5 by 11; the other is a portable printer and is 5 by as-long-as-the-spool-of-paper. It prints fine on the 5x? but it prints out on two pages of 8.5×11. I want the 8.5×11 to have two columns so the first column flows into the second column not onto a second page. I do NOT want to scale the whole thing so that it prints in one skinny column on a single sheet of 8.5×11; that is unreadable. Some scaling to fit the page in two columns is OK though. It prints fine on both sizes of paper from Xamlpad, but not from my program. So, it must be possible, however, for the life of me, I cannot figure it out.
My printer class:
public void Print(MyViewModel Data) {
IDocumentPaginator flowDoc = RenderFlowDocumentTemplate(pathToFlowDocument, data);
PrintDocumentImageableArea area = null;
XpsDocumentWriter xpsDocWriter = PrintQueue.CreateXpsDocumentWriter(ref area);
if (xpsDocWriter != null) {
PrintFlowDocument(xpsDocWriter, flowDoc.DocumentPaginator, area);
}
}
private IDocumentPaginator RenderFlowDocumentTemplate(string path, MyViewModel Data) {
string rawXamlText = "";
using (StreamReader streamReader = File.OpenText(templatePath)) {
rawXamlText = streamReader.ReadToEnd();
}
FlowDocument doc = XamlReader.Load(new XmlTextReader(new StringReader(rawXamlText)) as FlowDocument;
if (data != null) {
doc.DataContext = data;
}
return doc;
}
private void PrintFlowDocument(XpsDocumentWriter writer, DocumentPaginator document, PrintDocumentImageableArea area) {
document.PageSize = new Size(area.ExtentWidth, area.ExtentHeight);
PrintDocumentPaginator(writer, document);
}
private void PrintDocumentPaginator(XpsDocumentWriter xpsDocWriter, DocumentPaginator document) {
try {
xpsDocWriter.Write(document);
}
catch (PrintSystemException) {}
}
And here is a very trimmed down version of my FlowDocument:
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Background="White"
ColumnWidth="500">
<FlowDocument.Resources>...some resources...</FlowDocument.Resources>
<Paragraph />
<BlockUIContainer>
<Grid />
</BlockUIContainer>
.
.
.
<Section />
</FlowDocument>
I have tried messing with column width, the document page size, scaling, adding wrap panels, adding stack panels, etc… and I cannot figure this out. It is driving me nuts!! Can anyone help?
P.S. any typos in the code are induced by me since I had to copy it by hand from my other PC.
So, after a lot more digging and playing around with values and adding the FlowDocument to various viewers, I noticed that a majority of the people asking questions about FlowDocument printing have the opposite problem I have. And their solution was to set the ColumnWidth.
I simply removed
from the FlowDocument and I now have the behavior I want. Prints in two columns on A4 and Prints in a single column the length it needs on the smaller spooled paper. And for clarity, I am also not setting the ColumnWidth anywhere in code.