4

I've been looking at importing an RTF into a flowdocument, obstinately for the purposes of unattended printing; after a lot of poking, using a FlowDocument seemed to be the right approach. I've got the RTF to generally work, however footers disappear when loading the RTF into a FlowDocument.

The RTF was generated in Word, and when loaded into Wordpad, the footers are visible, so I can only assume I'm loading the document wrong, or it's an issue with the default paginator, or possibly both.

This is what I have so far, which loads the file and does the printing:

[STAThread]
public static int Main(string[] args)
{
    var documentPath = @"C:\temp\Example.rtf";

    var fileStream = File.Open(documentPath, FileMode.Open, FileAccess.Read, FileShare.Read);

    // Load the RTF into the flow document
    var flowDocument = new FlowDocument();
    TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
    textRange.Load(fileStream, DataFormats.Rtf);
    flowDocument.ColumnWidth = double.PositiveInfinity;
    var t = new Thickness(72);
    flowDocument.PagePadding = t; // Page margin

    // Get printer
    var queue = LocalPrintServer.GetDefaultPrintQueue();
    var capa = queue.GetPrintCapabilities();

    // Configure paginator
    var paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
    paginator.PageSize = new Size(capa.OrientedPageMediaWidth.Value, capa.OrientedPageMediaHeight.Value);

    // ...and print.
    var writer = System.Printing.PrintQueue.CreateXpsDocumentWriter(queue);
    writer.Write(paginator);

    return 0;
}

...beyond that, I'm at a loss. It's not clear whether the footers simply haven't loaded (and thus aren't part of the FlowDocument) - I suspect this is the case as we're loading into a TextRange that marks the start and end of the main FlowDocument content; but I'm also suspecting I might need a custom paginator as well.

Are there any pointers to the docs I'm missing - a google search for "RTF footer import flowdocument paginator" (and similar) has (so far) revealed no pertinent results. The closest I've so far found is this SO question, but this doesn't cover fetching the header/footer from an RTF file.

Community
  • 1
  • 1
Chris J
  • 30,688
  • 6
  • 69
  • 111
  • Did you try to use a Paragraph in the FlowDocument? – Jerry Mar 02 '14 at 00:02
  • I don't follow -- how does a paragraph tie in to headers/footers? If I can understand your thinking I might be able to provide a better comment. There are paragraphs within the RTF that I am importing and I can assume that they are being imported as FlowDocument paragraphs. – Chris J Mar 03 '14 at 13:18
  • Poke in the wind here, but will `flowDocument.ContentStart, flowDocument.ContentEnd` not have something to do with it? – TheGeekZn Mar 05 '14 at 10:12
  • That's the range in which to import the RTF data. I don't see any way to specify header/footer ranges; understanding what I do about a FlowDocument, this is probably accurate as headers/footers are an aspects of pagination, hence thinking this needs to be handled by a custom paginator. The issue then is getting the `{\footer` (and similar) RTF segments parsed and processed. – Chris J Mar 06 '14 at 08:49

2 Answers2

1

Take a look at this MSDN blogpost they are doing almost the same you are doing. With a custom DocumentPaginator to scale the the original pages. I think this should do the trick. I also used it a long time ago and headers and footers were no problem.

Jordy van Eijk
  • 2,718
  • 2
  • 19
  • 37
  • You should add quotes / code / comments relating to the article to better the quality of your post :) – TheGeekZn Mar 05 '14 at 10:10
  • @NewAmbition You mean i need to copy the article. I'm not fan of copying the code from the article quite long. And think this says enough – Jordy van Eijk Mar 05 '14 at 11:01
  • That's a variation on various blogs that show me how to get a header/footer with a custom paginator. The missing link is getting the RTF header/footer into this. Now, I could just parse the RTF DOM manually, translating the RTF into something suitable (possibly complicated catering for different field codes (style, alignment, codes, etc), but given FlowDocument can parse RTF natively, that would have been ideal. The other issue then is of parsing special field codes (e.g., page numbers) encoded in the RTF and manually handling these. – Chris J Mar 06 '14 at 08:38
  • But the real problem you face now is that you're not able to get your header and footer to show up in your fixedDocument (XPS) right? Isn't the header and footer some kind of `Block` inside the FlowDocument? – Jordy van Eijk Mar 06 '14 at 16:26
  • That's what I don't know: *is* the header/footer in the FlowDoc? In the case of that blog post, they're creating a manual header/footer with fixed text ("Page "); hence the blog doesn't add much - they don't appear to be extracting the header/footer from the FlowDocument it's processing. – Chris J Mar 07 '14 at 11:48
  • Hi Chris, I dig in something deeper on The FlowDocument. But the standard FlowDocument lacks the support of header and footers. The only way you can accomplish that is to create a ContentVisual and add this to the document inside your custom Paginator. There is no way to read your header / footer into the FlowDocument. I accomplished this sometime using the DevExpress library that supports reading DocX with the header and footer. I suspect that they are also using a FlowDocument. So check their source code if you have a license – Jordy van Eijk Mar 07 '14 at 14:00
0

To provide the header and footer property during printing, we usually handle the PrintPage event and provide these features in this event handler. Here is a sample for your reference. Although this sample is a printer class for DataGridView, you can take the logic of add the header and footer features.

· http://www.codeproject.com/KB/cs/DGVPrinter.aspx

If you are using the WebBrowser to print the HTML, you can custom the header and footer like mentioned in the following article.

· http://www.codeproject.com/KB/miscctrl/tips.aspx#tip3.8.2

Dharmesh Hadiyal
  • 719
  • 1
  • 7
  • 18
  • The key point though is how do I get and parses the header/footer from an RTF file though? I've now seen numerous examples now of how to get a header/footer withe custom paginator, but nothing that tells me how to fetch and grok the `{\footer` section (et al) of the RTF, which is the key question. – Chris J Mar 06 '14 at 13:05