How to get a FlowDocument from the PrintSettings?

SyntaxEditor for WPF Forum

Posted 13 years ago by Peter Luo - AIMNEXT
Version: 11.1.0544
Avatar

Hi,

It request a FlowDocument(not FixedDocument) for the Print and PrintPreview in our application, can I get a FlowDocument from the PrintSettings?

If not support that, I also can do it with the RTF, but I need insert the line number before every line, so how can I export one line to RTF(not all of the document), I don't know how to get a ITextSnapshot for a line.


Thanks you very much!

Comments (5)

Posted 13 years ago by Peter Luo - AIMNEXT
Avatar
Currently I use the following code:

string rtf = editor.Document.CurrentSnapshot.Export(new TextExporterFactory().CreateRtf());
MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(rtf));

FlowDocument flowDocument = new FlowDocument() { FontSize = editor.FontSize, FontFamily = editor.FontFamily };
TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
textRange.Load(ms, DataFormats.Rtf);

int index = 0;

Paragraph p = flowDocument.Blocks.FirstBlock as Paragraph;

while (p != null)
{
    p.Inlines.InsertBefore(p.Inlines.FirstInline,
        new Run(string.Format("{0,-5} ", ++index))
        {
            Foreground = editor.LineNumberMarginForeground,
            FontFamily = editor.LineNumberMarginFontFamily,
            FontSize = editor.FontSize - 1,
            BaselineAlignment = BaselineAlignment.Center
        });
    p = p.NextBlock as Paragraph;
}
It looks not bad, is there any better way ?
Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Peter,

We have never tried to create a FlowDocument from a FixedDocument. This article we found may or may not help you if you try that:
http://dedjo.blogspot.com/2008/04/converting-fixeddocument-xpsdocument.html

All ITextExporters have an Export method that takes a snapshot and set of TextRanges to export. You could pass the TextRange of the snapshot line you want to that.


Actipro Software Support

Posted 13 years ago by Peter Luo - AIMNEXT
Avatar
Thank you very much!
Posted 13 years ago by Peter Luo - AIMNEXT
Avatar
Hi,

Is the "editor.Document.CurrentSnapshot.Lines.Count" always one more than the document content?
Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Peter,

That is always how many lines there are in the snapshot. There is a minimum of one line in a document, even in an empty document.


Actipro Software Support

The latest build of this product (v24.1.2) was released 15 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.