RichtTextBlock is not displaying multiple spaces present in the text - wpf

I'm trying to display some table data in RichTextBlock.
The data contains several lines of text, and the text contains multiple spaces.
I'm adding each line as Run objects to the Paragraph.
string[] lines = tableData;
Paragraph para = this.TextContent.Blocks.First() as Paragraph;
para.Inlines.Add(new LineBreak());
para.Inlines.Add(new LineBreak());
para.Inlines.Add(new LineBreak());
para.Inlines.Add(new LineBreak());
for (int l = 0; l < lines.Length; l++)
{
Run r = new Run()
{
Text = lines[l],
FontSize = 9,
};
para.Inlines.Add(r);
para.Inlines.Add(new LineBreak());
}
RichTextBlock is replacing multiple spaces and keeping one, as shown here
Input text looks like this in Notepad++(enabled special characters to highlight spaces)
I couldn't find any property in the Paragraph or Run classes to avoid this scenario.
What do I miss to get the RichTextBlock to display the exact text displayed in Notedpadd++ like this?
Thank you

As per the comment above, this problem isn't caused by missing spaces. Notepad++ displays the table in a monospace font, where all characters have the same width, but the RichTextBlock displays the text using a proportional font, where characters can have different widths. Spaces seem to be narrower than other characters, hence the rows of the table don't line up.

Related

Keep Table together in flowdocument

I am using FlowDocument to print my output.
I have table that show output in four rows.when it comes to end of page table breaks into two pages,like this i want to show table in single page
i have googled and found this solution ,but i don't know how to add table to paragraph programatically.I have tried by following way,but doesnt work.
Section s = new Section();
s.BreakPageBefore = true;
Paragraph para = new Paragraph();
Bold Heading = new Bold();
Heading.Inlines.Add("Some sort of heading");
para.Inlines.Add(Heading);
LineBreak lb = new LineBreak();
para.Inlines.Add(lb);
s.Blocks.Add(para);
s.Blocks.Add(table);
UPDATE:
Figure fg = new Figure(table);
Paragraph paraa = new Paragraph();
paraa.KeepTogether = true;
paraa.Inlines.Add(fg);
doc.Blocks.Add(paraa);
now table is shifted to next page, but it is aligned to right side.How i can make it left aligned?

How can I add text to a Checkbox I create using iTextSharp?

Derived from Jeff S's methodology found here, I can add a "Checkbox" to a PDF page like so:
PdfPTable tblFirstRow = new PdfPTable(5);
tblFirstRow.SpacingBefore = 4f;
tblFirstRow.HorizontalAlignment = Element.ALIGN_LEFT;
. . . // code where textboxes are added has been elided for brevity
PdfPCell cell204Submitted = new PdfPCell()
{
CellEvent = new DynamicCheckbox("checkbox204Submitted", "204 Submitted or on file")
};
tblFirstRow.AddCell(cell204Submitted);
doc.Add(tblFirstRow);
The DynamicCheckbox class, based on Jeff S's CustomCellLayout class, is:
public class DynamicCheckbox : IPdfPCellEvent
{
private string fieldname;
private string cap;
public DynamicCheckbox(string name, String caption)
{
fieldname = name;
cap = caption;
}
public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
{
PdfWriter writer = canvases[0].PdfWriter;
RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes");
ckbx.CheckType = RadioCheckField.TYPE_CHECK;
ckbx.Text = cap;
PdfFormField field = ckbx.CheckField;
writer.AddAnnotation(field);
}
}
My problem is that the checkbox's text (the string assigned to ckbx.Text) is not displaying. The checkbox (outsized) occupies the last cell in the table row, but there is no (visible) accompanying text.
What's missing from my code?
Note: I tried to reduce the size of the checkbox by doing this:
Rectangle tangle = new Rectangle(20, 20);
//RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes");
RadioCheckField ckbx = new RadioCheckField(writer, tangle, fieldname, "Yes");
...but that attempt failed - with that code, I can't even "find" the checkbox in the generated PDF file - clicking willy-nilly in column 5 conjures up no checkbox...
Others have answered the label part. The Rectangle that you have called "tangle" needs to be calculated off of the rectangle that comes into the event handler, similar to
Rectangle tangle = new Rectangle(
rectangle.Left,
rectangle.Top - PDFStyle.boxsize - 4.5f,
rectangle.Left + PDFStyle.boxsize,
rectangle.Top - 4.5f
);
Where PDFStyle.boxsize is the width/height of the checkbox and 4.5f is the padding the edge of the cell. Basically the rectangle isn't relative to the cell, but absolute to the page.
As described in ISO-32000-1, a check box is a field of type Button. If you define text for a button, you want to define the text that is displayed on the button. However: in the case of a check box, there is no such text! Instead, you have two appearances, one for the Off value and one for the Yes value.
An educated guess made by an attentive reader would be that you don't want to add text (to the button), but that you want to add a label (for a checkbox). Again you should consult ISO-32000-1 and you'll discover that the spec doesn't say anything about labels for check boxes. The concept just doesn't exist at the level of an AcroForm.
This doesn't mean the concept doesn't exist in general. Many PDF tools allow you to define check boxes that are preceded by a label. When you look inside the PDF, you'll discover that this label is just part of the content, whereas the check box is represented by a widget orientation.
Let's take a look at the official documentation instead of frustrating ourselves searching on every place of the web except on the official web site. More specifically: let's take a look at the Buttons example from Chapter 7 of my book. You'll see that one can set text for a real button:
PushbuttonField button = new PushbuttonField(writer, rect, "Buttons");
button.setText("Push me");
This isn't possible with check boxes (for the obvious reason that the appearance of a check box is completely different). If we want to add a label, we can add it for instance like this:
checkbox = new RadioCheckField(writer, rect, LANGUAGES[i], "Yes");
field = checkbox.getCheckField();
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", onOff[1]);
writer.addAnnotation(field);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
new Phrase(LANGUAGES[i], font), 210, 790 - i * 40, 0);
You can find the C# version of these examples here: http://tinyurl.com/itextsharpIIA2C07
Creating a checkbox, and then accompanying text to its right, can be done like this:
PdfPCell cell204Submitted = new PdfPCell()
{
CellEvent = new DynamicCheckbox("checkbox204Submitted")
};
tblFirstRow.AddCell(cell204Submitted);
// . . . Chunks and an anchor created; that code has been elided for brevity
Paragraph parCkbxText = new Paragraph();
parCkbxText.Add(Chunk204SubmittedPreamble);
parCkbxText.Add(ChunkBoldNote);
parCkbxText.Add(Chunk204Midsection);
parCkbxText.Add(anchorPayeeSetup204);
PdfPCell cellCkbxText = new PdfPCell(parCkbxText);
cellCkbxText.BorderWidth = PdfPCell.NO_BORDER;
tblFirstRow.AddCell(cellCkbxText);
public class DynamicCheckbox : IPdfPCellEvent
{
private string fieldname;
public DynamicCheckbox(string name)
{
fieldname = name;
}
public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
{
PdfWriter writer = canvases[0].PdfWriter;
RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes");
ckbx.CheckType = RadioCheckField.TYPE_CHECK;
ckbx.BackgroundColor = BaseColor.ORANGE;
ckbx.FontSize = 6;
ckbx.TextColor = BaseColor.WHITE;
PdfFormField field = ckbx.CheckField;
writer.AddAnnotation(field);
}
}

enter key behaviour of syncfusion's Data bound grid doesn't work if any column is hidden

I am using Synfusion's Data Bound Grid.
I have set two properties as
this.mygrid.TableModel.Options.EnterKeyBehavior = GridDirectionType.Right;
this.mygrid.TableModel.Options.WrapCellBehavior = GridWrapCellBehavior.WrapGrid;
If no column is hidden this works fine. If i hide any column then it doesn't works.
Please help me to achieve enter key behaviour if columns are hidden.
Thanks in Advance
I have handled the following code for hiding columns :
GridColHidden[] hiddenCols = new GridColHidden[ 3];
for (int i = 0; i < 3; i++)
{
hiddenCols[i] = new GridColHidden(i + 1);
}
this.gridDataBoundGrid1.Model.ColHiddenEntries.AddRange(hiddenCols);
I have now added the following code.
this.mygrid.TableModel.Options.EnterKeyBehavior = GridDirectionType.Right;
this.mygrid.TableModel.Options.WrapCellBehavior = GridWrapCellBehavior.WrapGrid;
I was able to see the first cell is set as current cell , when Enter key is pressed with the current cell's position placed in last cell in Grid.

How to format specific text in WPF?

I have this code that adds dotted lines under text in text box:
// Create an underline text decoration. Default is underline.
TextDecoration myUnderline = new TextDecoration();
// Create a linear gradient pen for the text decoration.
Pen myPen = new Pen();
myPen.Brush = new LinearGradientBrush(Colors.White, Colors.White, new Point(0, 0.5), new Point(1, 0.5));
myPen.Brush.Opacity = 0.5;
myPen.Thickness = 1.0;
myPen.DashStyle = DashStyles.Dash;
myUnderline.Pen = myPen;
myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;
// Set the underline decoration to a TextDecorationCollection and add it to the text block.
TextDecorationCollection myCollection = new TextDecorationCollection();
myCollection.Add(myUnderline);
PasswordSendMessage.TextDecorations = myCollection;
My problem is I need only the last 6 characters in the text to be formatted!
Any idea how can I achieve that?
Instead of setting the property on the entire TextBlock, create a TextRange for the last six characters and apply the formatting to that:
var end = PasswordSendMessage.ContentEnd;
var start = end.GetPositionAtOffset(-6) ?? PasswordSendMessage.ContentStart;
var range = new TextRange(start, end);
range.ApplyPropertyValue(Inline.TextDecorationsProperty, myCollection);
If PasswordSendMessage is a TextBox rather than a TextBlock, then you cannot use rich text like this. You can use a RichTextBox, in which case this technique will work but you will need to use PasswordSendMessage.Document.ContentEnd and PasswordSendMessage.Document.ContentStart instead of PasswordSendMessage.ContentEnd and PasswordSendMessage.ContentStart.
You could databind your text to the Inlines property of TextBox and make a converter to build the run collection with a seperate Run for the last 6 characters applying your decorations

How to use the Insert Key (like in traditional windows) for a WPF 4 App?

Ok, I know that the new versions of windows do not use the insert key by default and you have to program for it. I want to be able to type in my text box and override the content that is in it just like in old windows when you could activate the insert key. This is just for my WPF 4, VB.net Application.
Updated Information:
That what I meant: I need to mimic old terminals. I need to activate the overwrite mode programmatically for all the controls. The same affect as the 'Insert' key on the keyboard. Only that key does not work in a WPF environment.
Example I am entering the word world over a text box that says 'Hello!':
Textbox Started as: [Hello!]
The Textbox is now [World!]
You will note that the one character exclamation mark stayed because world is not enough characters to replace the '!'.
Try this out (use this control instead of vanilla TextBoxes):
public class InsertModeTextBox : TextBox
{
public InsertModeTextBox()
{
InitializeComponent();
}
protected override void OnTextInput(TextCompositionEventArgs e)
{
var txt = e.Text;
var len = txt.Length;
var pos = CaretIndex;
var builder = new StringBuilder();
if (pos > 0) builder.Append(Text.Substring(0, pos)); // text before caret
builder.Append(txt); // new text
if (Text.Length > pos + len) builder.Append(Text.Substring(pos + len)); // text after overwrite
Text = builder.ToString();
CaretIndex = pos + len;
e.Handled = true;
}
}
In WPF 4 pressing Insert key when textbox has focus activates overwrite mode. Do you mean changing between Insert and Overwrite modes for all textboxes on a window at once?

Resources