Insert image into Word document TextBox - c#

I'm using GemBox.Document to generate an output document from a template. I want to insert an image inside a TextBox which will have the same size as that TextBox.
How can I do that?
DocumentModel document = DocumentModel.Load("mytemplate.dotx");
TextBox textBox = (TextBox)document.GetChildElements(true, ElementType.TextBox).First();
Picture picture = new Picture(document, "myimage.png");
textBox.Blocks.Add(new Paragraph(document, picture));

Try the following:
DocumentModel document = DocumentModel.Load("mytemplate.dotx");
TextBox textBox = (TextBox)document.GetChildElements(true, ElementType.TextBox).First();
// If needed you can adjust the TextBox element's inner margin to your requirement.
textBox.TextBoxFormat.InternalMargin = new Padding(0);
// If needed you can remove any existing content from TextBox element.
textBox.Blocks.Clear();
// Get TextBox element's size.
var textBoxSize = textBox.Layout.Size;
// Create and add Picture element.
textBox.Blocks.Add(
new Paragraph(document,
new Picture(document, "myimage.png", textBoxSize.Width, textBoxSize.Height)));
I hope this helps.

Related

How to show RichTextBox content in DataGridView

I want to display .Rtf file content in a DataGridView using this code:
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
rtBox.Rtf = System.IO.File.ReadAllText(RFileNm);
GridVeds.Rows[i].Cells[0].Value = rtBox.Text;
But some text showing as square i.e for:
{\rtf1\ansi\ansicpg1252\deff0\deftab1134{\fonttbl{\f0\fnil\fprq2\fcharset2
RuchaBold;}} {\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\nowidctlpar\cf1\lang1033\f0\fs36 P'b4'd4mk
DrtrRn'd6p'a4mt; nlMt'b4pvR'b7ikm'd1pJf, 'a6mh'e7'b4
s'cdM/v,m irhRp'8emW'c1ikR 'c8\par irp'b0m'b0'b4ykR
'ed'c0rmyh,qrRim poFtR'a4mSurp'b0R?m w'cd Lmpup'b0Ryp'f8mfr
jRuh'fe 'c8'c81'c8'c8\par }
How I can solve this problem ?

How to query the Base Paragraph element position? in order to add Link Annotation without saving the file

I'm creating a simple PDF file with some text and an hyperlink attached to the that text:
Document pdfDocument = new Document();
Page pdfPage = pdfDocument.Pages.Add();
TextFragment textFragment = new TextFragment("My Text");
Table table = new Table();
Row row = table.Rows.Add();
Cell cell = row.Cells.Add();
cell.Paragraphs.Add(textFragment);
pdfPage.Paragraphs.Add(table);
LinkAnnotation link = new LinkAnnotation(pdfPage, textFragment.Rectangle); //[Before Save]textFragment.Rectangle: 0,0,35.56,10
link.Action = new GoToURIAction("Link1 before save");
pdfPage.Annotations.Add(link);
pdfDocument.Save(dataDir + "SimplePDFWithLink.pdf");
The problem is that the link annotation is being assign to the before save rectangle [0,0,33.56,10] at the bottom of the screen where's the textFragment is being added to a different rectangle (I can't set here the Position property because I don't know it, it is relative to the cell's table).
In order to solve this I've tried saving the page and only then searching the textFragment using TextFragmentAbsorber
pdfDocument.Save(dataDir + "SimplePDFWithLink.pdf");
//[After Save]textFragment.Rectangle: 0,0,90,770
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
pdfPage.Accept(textFragmentAbsorber);
foreach (TextFragment absorbedTextFragment in textFragmentAbsorber.TextFragments)
{
link = new LinkAnnotation(pdfPage, absorbedTextFragment.Rectangle);
link.Action = new GoToURIAction("Link 2 after save");
pdfPage.Annotations.Add(link);
}
pdfDocument.Save(dataDir + "SimplePDFWithLink.pdf");
My Question:
Is is possible to add a simple link to a TextFragment (which is BaseParagraph not StructureElement) without saving the document first?
Here is a simple demo of the outcome, you can see that before saving the document the link is added to the left bottom of the document instead of the text rectangle:
Update:
If I specify the TextFragment's Position value with some arbitrary values, the link is then added exactly to the text, but I don't know what will be the Position value of the element because it being built dynamically using a Table.
Working with TextFragment and TextSegment does work and adds the link without pre-saving the file:
TextFragment textFragment = new TextFragment("My Text");
TextSegment textSegment = new TextSegment("Link to File");
textSegment.Hyperlink = new Aspose.Pdf.WebHyperlink("www.google.com");
textFragment.Segments.Add(textSegment);
It is worth to mention it is works well when linking to a file on the user's file-system like:
textSegment.Hyperlink = new Aspose.Pdf.WebHyperlink("Files\foo.png");

RichTextBox links appearing on same position

I have been able to get links appearing in my RichTextbox. The first entry is correct but when I try appending a new line that also contains a link, the first entry is in the same position as the new link. When clicking on the link it retains it's first entries hyperlink.
I want each line to have it's own hyperlink (where it's underlined)
Code used to append a Link
public void AppendLink(string text, string linkText)
{
LinkLabel link = new LinkLabel();
link.Text = text;
link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = linkText;
link.Links.Add(data);
link.Location = this.logTextBox.GetPositionFromCharIndex(this.logTextBox.TextLength);
this.logTextBox.Controls.Add(link);
logTextBox.SelectionFont = UNDERLINE_FONT;
this.logTextBox.AppendText(s);
}
Called using this
AppendLogLine("Sealed ");
AppendLink(itemName, GetItemLink(itemName));
AppendLog(" is an unknown item. Keeping.");
Append Log and AppendLogLine does the same as AppendLink just doesn't create a link and uses a different Font

WPF read Checkbox.Content

I have a couple of CheckBoxes with a TextBlock as content.
Now I want to read out the TextBlock.Text from each Checkbox.
If I read out the content like checkBox.Content.ToString(); I only get System.Windows.Controls.TextBlock which kinda makes sense.
I also tried to create a new TextBlock and give it the content but it didn't work.
TextBlock _tempTBL = new TextBlock();
_tempTBL = checkBox.Content;
Any help is much appreciated.
var _tempTBL = (TextBlock) checkBox.Content; //Get handle to TextBlock
var text = _tempTBL.Text; //Read TextBlock's text
Edit:
On a side note, you can directly set desired text as CheckBox's content.
checkBox.Content = "Hello World";
And when you want to access the text, no type cast is needed
string text = checkBox.Content;
You have to cast the type to a TextBlock:
// no need to 'new' it up if you're assigning an existing instance...
TextBlock _tempTBL = (TextBlock) checkBox.Content;

HyperLink text not rendered after controls are added

I have a HyperLink control with text in its Text property.
With the following code:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Text = "Test";
link.Controls.Add(img);
When I do this, the image is rendered inside a a tag, but the text is not rendred.
Is there a way to render both the image and the text inside the Text property without throwing a third control in to the mix?
When you put any controls into the WebControl.Controls collection, it will ignore what you have inside Text. So if you want to render both text and other child controls, you should add the text into Controls:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Controls.Add(new Literal{ Text = "Test"}); // this line will add the text
link.Controls.Add(img);
I feel this should work out for you.
var link = new HyperLink();
var img = new HtmlGenericControl("img");
var lbl = new Label();
img.Attributes.Add("src", "text.png");
lbl.Text = "Test";
link.Controls.Add(img);
link.Controls.Add(lbl);
this.Controls.Add(link);
According to the MSDN article "The HyperLink control can be displayed as text or an image." So the answer is no, I'm afraid.

Categories