Get path geometry from FlowDocument object - c#

Can someone tell me how to get path geometry from a WPF FlowDocument object? Please note that I do not want to use FormattedText. Thanks.

A FlowDocument can be viewed in any number of ways, but a Path is a fixed shape. I think maybe you really want some simplified, visual-only form of a FlowDocument's contents.
In that case you might try converting the FlowDocument to an XPS FixedDocument - the FixedPages have Canvases containing a bunch of Paths and Glyphs.

Get the Text property of a TextRange object initialized over the entire FlowDocument:
FlowDocument myFlowDocument = new FlowDocument(); //get your FlowDocument
//put in some (or it already has) text
string inText = "Hello, WPF World!";
TextRange tr = new TextRange(FlowDocument.ContentStart, FlowDocument.ContentEnd);
tr.Text = inText;
//get the current text out of the FlowDocument
TextRange trPrime = new TextRange(FlowDocument.ContentStart, FlowDocument.ContentEnd);
string outText = trPrime.Text;
//now outText == "Hello, WPF World!";
//to get formatting, looks like you would use myFlowDocument.TextEffects

Can you use
ChildVisual = VisualTreeHelper.GetChild(Visual yourVisual)
Dunno if you can take a Visual and turn it into a path geometry..

Related

Formatting Sections of a TextRange or TextRange2 in PowerPoint.Interop C#

I would like to add rich text to a TextBox on a PowerPoint Slide.
I am using .Net 4.7.2 with Microsoft.Office.Interop.PowerPoint
Overview
// Initialization of the Application
public PresentationGenerator () {
pptApplication = new Application ();
}
// Creating a new document based on a teplate.
Presentation pptPresentation = generator.pptApplication.Presentations.Open (templatePath, MsoTriState.msoFalse, MsoTriState.msoTrue, showWindow);
// Setting a slide and editing the text works perfectly fine with this
Slide currentSlide;
currentSlide = pptPresentation.Slides[1];
currentSlide.Shapes.Title.TextFrame.TextRange.Text = "Wonderful Title";
currentSlide.Shapes[3].TextFrame.TextRange.Text = "Great TextBox";
This is an mwe of my setup to set text on slides.
I would however like to add text to one of my shapes using a loop and setting the layout depending on a property.
Imagine the following Array
customParagraphs = [
{
text:"example heading",
type:"title"
},{
text:"example normal text",
type:"text"
}]
I can loop over this list and add the text to the end of the TextRange2 using .insertAfter(text) and try setting the font-size for the text portion that i added.
TextRange2 textrange = currentSlide.Shapes[3].TextFrame2.TextRange
foreach(var paragraph in customParagraphs){
TextRange2 paragraphRange = textrange.Paragraphs.insertAfter(paragraph.text)
if(paragraph.type == "title"){
paragraphRange.Font.Size = 24.0F;
}
}
This will successfully add the text and change the font-size, if type is title. However it will change the font-size for the whole text-range!
The reference returned by .insertAfter() seems to refer to the instance of TextRange2 and not my newly added paragraph.
My Questions
Is there a way to change the font-size and other attributes of a line, paragraph or word inside a TextRange or TextRange2 element?
Is there a better way to add text to a TextRange or TextRange2 element than .insertAfter that preferably returns a reference to only the text i added?

extract lines from a flowdocument in a richtextbox starting with specific words WPF

I'm very new to wpf and I would like to make a text analysing tool.
I already know how to import text into rich textbox and format it properly, but now I want to run a method that extracts all lines in the flowdocument that start with INT or EXT and place them in a listbox. It seems to be quite easier to do this in winforms than in WPF.
Is there someone who can guide me with this?
I wish I could already provide some code but the flowdocument is new to me as is wpf.
I have written a code snippet to collect the lines that begin with INT or EXT.
I am sure the code is not optimal, because i am not practised with RichTextBox, but i think it is very easy to understand.
private List<string> CollectLines()
{
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
TestRichTextBox.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
TestRichTextBox.Document.ContentEnd);
// The Text property on a TextRange object returns a string
// representing the plain text content of the TextRange.
var text = textRange.Text;
List<string> resultList = new List<string>();
// Collect all line that begin with INT or EXT
// Or use .Contains if the line could begin with a tab (\t), spacing or whatever
using (StringReader sr = new StringReader(text))
{
var line = sr.ReadLine();
while (line != null)
{
if (line.StartsWith("INT") || line.StartsWith("EXT"))
{
resultList.Add(line);
}
line = sr.ReadLine();
}
}
return resultList;
}
Maybe you can find out how you can put the list into a listbox yourself :)

richtext to byte[] and byte[] to richtext in WPF [duplicate]

How do I get the text in RTF of a RichTextBox? I'm trying to get like this, but the property does not exist.
RichTextBox rtb = new RichTextBox();
string s = rtb.Rtf;
To get the actual XAML created by the user inside of the RichTextBox:
TextRange tr = new TextRange(myRichTextBox.Document.ContentStart,
myRichTextBox.Document.ContentEnd);
MemoryStream ms = new MemoryStream();
tr.Save(ms, DataFormats.Xaml);
string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());
EDIT: I don't have code in front of me to test, but an instance of the TextRange type has a Save (to stream) method that takes a DataFormats parameter, which can be DataFormats.Rtf
There are 2 RichTextBox classes, one from the winforms framework and one from the WPF framework:
System.Windows.Controls.RichTextBox wpfBox;
System.Windows.Forms.RichTextBox winformsBox;
Only the Winforms RichTextBox has an Rtf property, the other has a Document property which contains a FlowDocument.

how to transfer data from richtextbox to another richtextbox WPF C#

hi i have a problem in displaying or transfering the data in my richtextbox to other richtextbox...
richtextbox1.Document = richtextbox2.Document; //This will be the idea..
actually what i'm planning to do is, i want to transfer my data from my database to my listview which will display as what it is
SQLDataEHRemarks = myData["remarks"].ToString();// Here is my field from my database which is set as Memo
RichTextBox NewRichtextBox = new RichTextBox();// Now i created a new Richtextbox for me to take the data from SQLDataEHRemarks...
NewRichtextBox.Document.Blocks.Clear();// Clearing
TextRange tr2 = new TextRange(NewRichtextBox.Document.ContentStart, NewRichtextBox.Document.ContentEnd);// I found this code from other forum and helps me a lot by loading data from the database....
MemoryStream ms2 = GetMemoryStreamFromString(SQLDataEHRemarks);//This will Convert to String
tr2.Load(ms2, DataFormats.Rtf);//then Load the Data to my NewRichtextbox
Now what i want to do is, im going to load this data to my ListView.. or other control like textblock or textbox...
_EmpHistoryDataCollection.Add(new EmployeeHistoryObject{
EHTrackNum = tr2.ToString() // The problem here is it will display only the first line of the paragraph.. not the whole paragraph
});
Use the Text property of the TextRange instead of .ToString()
Method to get the content of a RichTextBox as string:
public static string GetStringFromRichTextBox(RichTextBox richTextBox)
{
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
return textRange.Text;
}
Method to get the content of a RichTextBox as rich text:
public static string GetRtfStringFromRichTextBox(RichTextBox richTextBox)
{
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
MemoryStream ms = new MemoryStream();
textRange.Save(ms, DataFormats.Rtf);
return Encoding.Default.GetString(ms.ToArray());
}
Edit: You can put the rich text returned from GetRtfStringFromRichTextBox() in another RichText control by doing the following:
FlowDocument fd = new FlowDocument();
MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(richTextString));
TextRange textRange = new TextRange(fd.ContentStart, fd.ContentEnd);
textRange.Load(ms, DataFormats.Rtf);
richTextBox.Document = fd;
Wouldn't getting the contents of a RichTextBox as string simply be something like
the following (in VB.Net)
Dim strText as string = MyRTB.text

Problem with Printing Content Of RichTextBox with Adorner Layers

I am trying to print the content of RichTextBox including the Adorner Layers inside.
I am using this code to print
double w = Editor.ExtentWidth; // Editor is the RichTextBox
double h = Editor.ExtentHeight;
LocalPrintServer ps = new LocalPrintServer();
PrintQueue pq = ps.DefaultPrintQueue;
XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(pq);
PrintTicket pt = pq.UserPrintTicket;
if (xpsdw != null)
{
pt.PageOrientation = PageOrientation.Portrait;
PageMediaSize pageMediaSize = new PageMediaSize(w, h);
pt.PageMediaSize = pageMediaSize;
xpsdw.Write(Editor);
}
The problem I'm facing is that this code only prints the content that is visible on the screen, not the whole content of the Editor.
EDIT
The pictures are adorner layers, If I print using the method above, it only prints the visible part on the screen not the whole document.
Edit
I'm trying to print each page separately but I cant force Editor.InvalidateVisual(); after doing a Editor.PageDown(); Is there a way I can do that in my method ?
When controls draw on the adorner layer, they search up the tree until they find an adorner layer. Often times this is a the window level. In some cases, you'll want an adorner layer closer to the control, or directly around the control. In this case, wrap the control with an <AdornerDecorator><RichTextBox /></AdornerDecorator>
In your case, you'd probably want to pass a parent element of adorner decorator, or the decorator itself to the print logic. This way the print logic would include the adorner layer as part of the visual. Maybe something like this:
<Grid Name="EditorWrapper">
<AdornerDecorator>
<RichTextBox />
</AdornerDecorator>
</Grid>
Then, pass "EditorWrapper" to the print logic.
EDIT
If you just want to print the contents of the RichTextBox, then you might be best to use the built-in pagination capabilities of the FlowDocument. FlowDocument implements IDocumentPaginatorSource, which will return a paginator that can print the document. Pass that paginator to the XpsDocumentWriter and it should dump the content properly.
var doc = Editor.Document;
var src = doc as IDocumentPaginatorSource;
var pag = src.DocumentPaginator;
xpsdw.Write(pag);
I found this code here:
// Serialize RichTextBox content into a stream in Xaml or XamlPackage format. (Note: XamlPackage format isn't supported in partial trust.)
TextRange sourceDocument = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
MemoryStream stream = new MemoryStream();
sourceDocument.Save(stream, DataFormats.Xaml);
// Clone the source document's content into a new FlowDocument.
FlowDocument flowDocumentCopy = new FlowDocument();
TextRange copyDocumentRange = new TextRange(flowDocumentCopy.ContentStart, flowDocumentCopy.ContentEnd);
copyDocumentRange.Load(stream, DataFormats.Xaml);
// Create a XpsDocumentWriter object, open a Windows common print dialog.
// This methods returns a ref parameter that represents information about the dimensions of the printer media.
PrintDocumentImageableArea ia = null;
XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia);
if (docWriter != null && ia != null)
{
DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocumentCopy).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 pagePadding = flowDocumentCopy.PagePadding;
flowDocumentCopy.PagePadding = new Thickness(
Math.Max(ia.OriginWidth, pagePadding.Left),
Math.Max(ia.OriginHeight, pagePadding.Top),
Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), pagePadding.Right),
Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), pagePadding.Bottom));
flowDocumentCopy.ColumnWidth = double.PositiveInfinity;
// Send DocumentPaginator to the printer.
docWriter.Write(paginator);
}
Adorner layers are are drawing oriented. So one option left is to convert the entire RichTextBox into a drawing and print that as an Image in XPS.
Although this poses multiple issues...
It will print the external and internal contents that occupy or occupied by the richtextbox i.e. editor toolbar (if it is part of the control template of the rich text box), internal scroll bars etc.
If there are scrollbars then the content out of the scrollbars are not going to be printed as the image will be the exact "snapshot" of the textbox (with remaining text clipped by srollbars).
Will you be happy with that?
I didn't find any way that works 100% for this problem, so I'm trying to transform all my adorner layers to actual images. I'll update the question once I get a 100% working solution.

Categories

Resources