I have created a RichTextBlock like this
RichTextBlock RTB = new RichTextBlock();
But I can not find a way to add text and URL's to the RichTextBlock.
I am thinking, if I need a specific using or something similiar
try this :
// creat your RichTextBlock
RichTextBlock MyRTB = new RichTextBlock();
// Create a Run of plain text and some bold text.
Run myRun1 = new Run();
myRun1.Text = "Some text here";
myRun1.FontStyle = FontStyle.Oblique;
myRun1.FontSize = 72;
Run myRun2 = new Run();
myRun2.Text = "Other text";
myRun2.FontSize = 140;
myRun2.Foreground = new SolidColorBrush(Colors.Red);
// Create a paragraph and add the Run and Bold to it.
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun1);
myParagraph.Inlines.Add(myRun2);
// add paragraphe to your RichTextBlock blocks
MyRTB.Blocks.Add(myParagraph);
// now, add RichTextBlock to the grid (or any other controler in your page)
ParentGrind.Children.Add(MyRTB);
// update layoute
ParentGrind.UpdateLayout();
this work well for me to add and format text dynamically
Related
Consider a 1-page PDF document named in.pdf that looks like this:
We execute the following code in C#:
using (var reader = new PdfReader(#"C:\<path>\in.pdf"))
using (var writer = new PdfWriter(#"C:\<path>\out.pdf"))
using (var pdfDoc = new PdfDocument(reader, writer))
{
var page = pdfDoc.GetFirstPage();
var pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
using (var canvas = new Canvas(pdfCanvas, new Rectangle(400, 300)))
{
canvas.Add(new Paragraph("new text\r\nnew text")
.SetFontColor(new DeviceRgb(255, 0, 0))
.SetFontSize(50));
}
pdfCanvas.Release();
}
We get new text appearing under the original text, as expected:
Now we execute the same code with one change - we create PdfCanvas as follows:
var pdfCanvas = new PdfCanvas(page);
Result is still as expected - new text is above the original text:
Finally, we execute the same code with PdfCanvas created as follows:
var pdfCanvas = new PdfCanvas(page.NewContentStreamAfter(), page.GetResources(), pdfDoc);
New text is above the original text as expected, but it's flipped!
Is this a bug? Getting the same flipped output creating PdfCanvas like this:
var pdfCanvas = new PdfCanvas(page.GetFirstContentStream(), page.GetResources(), pdfDoc);
//OR
var pdfCanvas = new PdfCanvas(page.GetLastContentStream(), page.GetResources(), pdfDoc);
Using nuget package itext7 v7.1.15.
Is this a bug?
No. It's matching your code and a feature of the PdfCanvas(PdfPage page) constructor.
Each PDF content stream contains a sequence of instructions that change the current graphics state and/or draw something. If the original creator of the respective content stream did not take care to clean up the graphics state, instructions you add to it or to a following content stream of the same page are subject to those graphics state changes.
In your case the original page content stream apparently at the end has a graphics state with the current transformation matrix set to a reflection. Depending on how you add your new content, your additions also are subject to that reflection transformation:
var pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
Here you add a new content stream before all existing content streams. Thus, your additions to pdfCanvas are not subject to any graphics state changes and you get upright text.
var pdfCanvas = new PdfCanvas(page);
The constructor you use here, PdfCanvas(PdfPage page), calls another constructor, PdfCanvas(PdfPage page, bool wrapOldContent), which for wrapOldContent == true wraps the existing content in an envelope of a save-graphics-state / restore-graphics-state instruction pair, to restore the original graphics state after the existing content. PdfCanvas(PdfPage page) uses a true value for wrapOldContent under certain conditions, in particular if you are manipulating an existing PDF and the page in question already has a non-empty content stream.
In your case, therefore, the original content is wrapped in such an envelope and the graphics state is restored thereafter. So your additions are not subject to any graphics state changes and you get upright text.
var pdfCanvas = new PdfCanvas(page.NewContentStreamAfter(), page.GetResources(), pdfDoc);
var pdfCanvas = new PdfCanvas(page.GetFirstContentStream(), page.GetResources(), pdfDoc);
var pdfCanvas = new PdfCanvas(page.GetLastContentStream(), page.GetResources(), pdfDoc);
The PdfCanvas constructors you use here do not add any such envelope. Thus, your additions are subject to graphics state changes in the original content and you get mirrored text.
I add a button to CKEditor. I want to wrap the selected text in the tag h2. If no text, the button is not working properly. I use the following code.
var selected_text = editor.getSelection().getSelectedText();
var newElement = new CKEDITOR.dom.element('h2');
newElement.setAttributes({style: ''});
newElement.setText(selected_text);
editor.insertElement(newElement);
But the tag is added h2even if text is not selected. How can I fix this?
Is there a reason you can not just check the length before making any changes?
var selected_text = editor.getSelection().getSelectedText();
if(selected_text.length > 0)
{
var newElement = new CKEDITOR.dom.element('h2');
newElement.setAttributes({style: ''});
newElement.setText(selected_text);
editor.insertElement(newElement);
}
I am creating a hyperlink button like this:
HyperlinkButton hlbMail = new HyperlinkButton();
hlbMail.Height = 89;
hlbMail.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
hlbMail.VerticalAlignment = System.Windows.VerticalAlignment.Top;
hlbMail.Margin = new Thickness(60, -70, 0, 0);
hlbMail.Width = 290;
hlbMail.FontSize = 22;
TextBlock btnContent = new TextBlock();
btnContent.TextWrapping = TextWrapping.Wrap;
btnContent.Text = message;
btnContent.Visibility = System.Windows.Visibility.Visible;
hlbMail.Content = btnContent;
hlbMail.Visibility = System.Windows.Visibility.Visible;
mailStackPanel.Children.Add(hlbMail);
mailscrollViewer.Content = mailStackPanel;
PIMail.Content = mailscrollViewer;
But i have a problem in which the content of the button is not being displayed. As can be seen, the content is supposed to be the text of the TextBlock (the message var is a string which is not empty). What can be the reason that the content is not being shown?
You need to use this:
hlbMail.Content= btnContent.Text;
I think you forgot to add the .Text.
You cannot have a hyperlinkbutton having textblock as its content. But you can have the text of a textblock as the content of the hyperlink
As you also mentioned you need the text of textblock than you need to specify that you need the text by adding the btnContent.Text;
function sidebar(editor)
{
var selection = editor.getSelection();
if(selection.getSelectedText()!="")
{
var range = selection.getRanges();
var customNode = editor.document.createElement( 'cdl:sidebar' );
var extractedContent = range[0].extractContents();
customNode.append(extractedContent);
var sidebarHolder = editor.document.createElement("sidebarHolder");
sidebarHolder.append(customNode);
var nodeHtml = sidebarHolder.getHtml();
editor.insertHtml(nodeHtml+" ");
}
else {
showErrorMessage("Selection is not proper");
}
}
This is my code.Whenever i select a single word like "Please" in "Please post comments or corrections" statement ,after adding tags Please.The space between "Please Post" get's trimmed.But when i select "Please "(word with space),the code works correctly.And , i want that tag should not visible in editor , it should be visible in source panel.
Try appending html to ckeditor instance rather adding text.
I was trying to create two different kinds of buttons: buttons with background and buttons without background.
And unfortunately I can't figure out how I can set some buttons to use the first style and some buttons to use the second.
Here's a code sample (although i doubt it'll help)
Is there some way to change the Style of the button in the .cs file, or any workaround?
//init hyperlink
Button hyperlink = new Button();
Label hyperlink_label = new Label();
hyperlink_label.Text = hyperlink_text;
hyperlink_label.ModifyFg(StateType.Normal, new Gdk.Color(0,0,0));
hyperlink_label.ModifyFg(StateType.Prelight, new Gdk.Color(255,255,255));
hyperlink_label.ModifyFg(StateType.Selected, new Gdk.Color(255,255,255));
Pango.FontDescription hyperlinkFontDesc = new Pango.FontDescription();
hyperlinkFontDesc.Family = "Adobe Garamond Pro";
hyperlinkFontDesc.AbsoluteSize = hyperlink_fontSize * Pango.Scale.PangoScale;
hyperlink_label.ModifyFont(hyperlinkFontDesc);
hyperlink.Add(hyperlink_label);
mainWindowFixed.Put(hyperlink, hyperlink_pos[0], hyperlink_pos[1]);
//init startbutton
Button startButton = new Button();
Label startLabel = new Label();
startLabel.Text = startButton_text;
startButton.Settings.ThemeName = "Ludwig_AutoUpdater";
startLabel.ModifyFg(StateType.Normal, new Gdk.Color(255,255,255));
startLabel.ModifyFg(StateType.Prelight, new Gdk.Color(255,255,255));
startLabel.ModifyFg(StateType.Selected, new Gdk.Color(255,255,255));
Pango.FontDescription startLabelFontDesc = new Pango.FontDescription();
startLabelFontDesc.Family = "Klavika bd";
startLabelFontDesc.AbsoluteSize = startButton_fontSize * Pango.Scale.PangoScale;
startButton.Add(startLabel);
startButton.Child.ModifyFont(startLabelFontDesc);
startButton.SetSizeRequest(startButton_size[0], startButton_size[1]);
mainWindowFixed.Put(startButton, startButton_pos[0],startButton_pos[1]);
Don't change the style of Gtk.Button. Create a custom widget derived from Gtk.Button and change that widget's style. For example:
public class BGButton : Gtk.Button {}
and
public class NoBGButton : Gtk.Button {}
and now change the style of BGButton and NoBGButton.
Then if you want a Button with a Background use BGButton , else use NoBGButton.