How build link to local file in richtextbox - c#

Hello i builded application with log that sow me what i am doing(where i saving files).
Sow my goal here is build links to all my new files that i created and open them by click
This my code
StreamWriter sw = new StreamWriter(Path.Combine(filepath), false, Encoding.GetEncoding("Windows-1255"));
sw.Write(Encoding.GetEncoding("Windows-1255").GetString(byteArray1255));
sw.Close();
rtx_Log.Text+= filepath;
here i created some file and i just want to show the pass in richtextbox and open it by click.

If RichTextBox.DetectUrls is set to true, the control will automatically detect links from the protocol and create a link.
"My File: file://c:/MyFile.txt" will display the file:// part as a link.
The RichTextBox.LinkClicked event is fired when the user clicks a link - and you can act on it as needed.
private void RichTextBox1OnLinkClicked(object sender, LinkClickedEventArgs e)
{
var filePath = new Uri(e.LinkText).AbsolutePath;
}

Related

Outlook VSTO code crashes when click on the button for image

I am working on this Outlook VSTO Add-on, to add an image in the body of the email, but no luck at all for two weeks!!! It works with a normal path like c:\folder
but it doesn't work if I want to use the Resources folder inside of the app.
When I run it and click on the button, it crashes and goes to the:
document.Application.Selection.InlineShapes.AddPicture(ImagePath);
There is a Ribbon with a button. When user clicks, it should add the email signature in the body.
PLEASE HELP!!!!!!!!!!!!!!!!
private void button_Click(object sender, RibbonControlEventArgs e)
{
Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
Word.Document document = inspector.WordEditor;
string ImagePath = #"\Resources\Picture.jpg";
if (document.Application.Selection == null)
{
MessageBox.Show("Please select the email body");
}
else
{
document.Application.Selection.InlineShapes.AddPicture(ImagePath);
}
}
Word Object Model knows nothing about resources inside your assembly. It does not know and does not care about any external assemblies. It only understands files - you will need to extract the resource into a temp file and specify that file's fully qualified file name.

PDF file isn't centered in webBrowser control of Windows Form

I'm trying to make a PDF utility app that will allow users to merge and split PDF files. The only problem I have right now is that on load my app shows the pdf file off center. Example
In order to make the PDF file center I have to manually mouse-click the dark grey area that is shown when the PDF file is off center. After that the pdf file will be center like so
So is there anyway possible to make the PDF file centered automatically like the 2nd image?
Code below is how I call webBrowser1 that is rendering the pdf file.
public Form1()
{
InitializeComponent();
string filename = "pdf_example.pdf";
string path = Path.Combine(Environment.CurrentDirectory, filename); // grab pdf file from root program file
webBrowser1.Url = new Uri(path); // <-- input pdf location, WebBrowser Code section, REDACTED
//"button1" == "Load PDF Files", EventHandler
button1.Click += new EventHandler(button1_click);
//"button2" == "Save PDF Files", EventHandler
button2.Click += new EventHandler(button2_click);
//"button3" == "Merge PDF Files", EventHandler
button3.Click += new EventHandler(button3_click);
//"button4" == "Split PDF Files", EventHandler
button4.Click += new EventHandler(button4_click);
}
I was thinking it had to do with the pdf being loaded before the Form1 window was completely loaded. So I tried making a method and set Form1 Shown properties to this method.
void Form1_Shown(object sender, EventArgs e)
{
string filename = "pdf_example.pdf";
string path = Path.Combine(Environment.CurrentDirectory, filename); // grab pdf file from root program file
webBrowser1.Url = new Uri(path); // <-- input pdf location, WebBrowser Code section, REDACTED
}
It didn't work.
Any help would be much appreciated.
Also I completely redid the app to use axAcroPDF control to render PDF instead of webBrowser control to render PDF and I still got the same problem where the PDF file was off-center.
For future reference. The problem was that my monitor resolution was set to 4k. This caused the pdf file to be open off center in IE every-time for some reason. If the program was open in a 1920x1080 resolution monitor then the pdf file is perfectly centered every-time the program starts.

cannot open my RTF file with my custom fonts

I made my own custom .ttf fonts for my UWP program and put them in Assets folder (vs 2017/2019). They are working well when the Document is processed in RichEditBox. However, when I Save the RTF file and then Open it, my custom fonts are ignored. If I install my custom fonts beforehand into Windows\Fonts folder , then Open file load the document with my custom fonts. Looks like without installing my custom fonts the program does not link them to the document.
Again - I wrote a program with RichEditBox and my custom fonts in that program. When processed - fonts changed, styles changed, etc - everything goes as designed. When I save RTF file with that program, and open that RTF with that (the same) program - color table is OK but my fonts are not shown though the fonts were compiled with that program (BuildAction - Content; CopyToOutputDirectory - Copy always). To simplify - I made button with the information the file contains. Though fonts are compiled (located in Assets folder), the program does not link them to the document.
Actually with that button I tried to reproduce what is described here: RichEditBox (UWP) ignores font and foreground when setting Rtf text However in my case, the RichEditBox shows only fonts installed in Windows\Fonts directory. How to overcome that and either use links to local fonts compiled with my program or make installer install the fonts to Windows\Fonts directory?
How could I use my custom fonts (link them to the document) without installing them or what I need to do my UWP program installs my custom fonts to user's device while installing itself?
This is code for button I used to display text:
private void Page_Click(object sender, RoutedEventArgs e)
{
string myRtfString = #"{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil MyFont;}{\f1\fnil MyFont1;}{\f2\fnil MyFont2;}} {\colortbl ;\red0\green0\blue0;\red255\green255\blue255;\red255\green100\blue0;} {\*\generator Riched20 10.0.18362}\viewkind4\uc1 \pard\sl480\slmult1\qj\cf1\highlight2\f0\fs36 tt\highlight3\f1 g\f0 acgt\f2 c\highlight2\f0 tt\highlight0\par}";
editor.Document.SetText(TextSetOptions.FormatRtf, myRtfString);
}
This is XAML for RichEditBox:
<RichEditBox
x:Name="editor"
Height="200"
FontFamily="Assets/Fonts/MyFont.ttf#MyFont"
FontSize="24" RelativePanel.Below="openFileButton"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True" />
Gosha, this way you can apply at least one of the fonts to that .rtf file - see below. For others, I think, you need to use either map information from that .rtf or make an additional map of your own. That will be some "trabajo", but what can you do?
private void applyMyFonts()
{
string TextOut;
MyRichEditBox.Document.GetText(TextGetOptions.None, out TextOut);
MyRichEditBox.Document.Selection.SetRange(0, TextOut.Length);
MyRichEditBox.Document.Selection.CharacterFormat.Name = "Assets/Fonts/MyFont.ttf#MyFont";
}
private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
Windows.Storage.Pickers.FileOpenPicker open =
new Windows.Storage.Pickers.FileOpenPicker();
open.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
open.FileTypeFilter.Add(".rtf");
Windows.Storage.StorageFile file = await open.PickSingleFileAsync();
if (file != null)
{
try
{
Windows.Storage.Streams.IRandomAccessStream randAccStream =
await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
// Load the file into the Document property of the RichEditBox.
MyRichEditBox.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, randAccStream);
}
catch (Exception)
{
ContentDialog errorDialog = new ContentDialog()
{
Title = "File open error",
Content = "Sorry, I couldn't open the file.",
PrimaryButtonText = "Ok"
};
await errorDialog.ShowAsync();
}
}
applyMyfonts();
}

Why doesn't a pdf file view in a seperate tab in internet explorer?

I have a function which display a pdf file. Im using Internet Explorer and it's upto date. I tried to do it in two computers. In one browser it asks to open through a pdf reader while other one opens a tab and display an empty page. I've tried many codes found in the internet even in stackoverflow. But nothing works as i want. Here i have added my code. Please take a look at it.
Linkbutton click event
protected void pdfViewLOP_Click(object sender, EventArgs e)
{
Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "viewPDF.aspx"));
}
code in new page which pdf should be displayed
protected void Page_Load(object sender, EventArgs e)
{
try
{
string name = Session["name"].ToString();
string FilePath = Server.MapPath("~/filesPDF/" + name);
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(FilePath);
if (buffer != null)
{
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}
catch (Exception ex)
{
WebMsgBox.Show(ex.Message);
}
}
It is likely due to settings on the client machine, specifically Adobe preferences.
To change the default PDF open behavior when using a web browser:
Choose Edit—>Preferences
Select the Internet category from the list on the left
To display the PDF in the browser, check "Display in browser"
To open PDFs from the web directly in Acrobat, uncheck "Display in browser:
See this article and this article.
Also note: To display a PDF in your browser, your cache control headers must allow the browser to create a temporary copy of the PDF. If you are setting cache hints to prevent caching (e.g. if you application contains sensitive pages) you might be better off letting the user download the PDF and view it offline.

HtmlAnchor control not opening in new window

I have the following code to dynamically create an HtmlAnchor and assign its HRef property. When I right click the link and do "save target as" it is downloading the pdf, but when clicking the link is not opening in a new tab.
HtmlAnchor htmlanc = new HtmlAnchor();
htmlanc.HRef = "file:\\arts\Shared\Let";
htmlanc.Title = "Letter Link";
htmlanc.InnerText = "file:\\arts\Shared\Let";
htmlanc.Target = "_blank";
pnlLet.Controls.Add(htmlanc);
when i click on the link it is generation an error which in the below image.
And the path is in a network folder which is shared. Is the error generating because my application cannot access the path. I checked with the networking guys but they say that the application have full access to the network shared folder, but i doubt it.
I wanted this to be a comment but it seems you are struggling to understand our help so I wanted to expand it here.
I have copied your code sample and put on Page_Load of a blank WebForms solution.
protected void Page_Load(object sender, EventArgs e)
{
var htmlanc = new HtmlAnchor();
htmlanc.HRef = "http://stackoverflow.com/questions/29281667/htmlanchor-control-not-opening-in-new-window";
htmlanc.Title = "Open Question";
htmlanc.InnerText = "Open Question";
htmlanc.Target = "_blank";
Controls.Add(htmlanc);
}
When running it you can right-click on the link:
Then you can see all of the attributes and see the target="_blank":
When I click this link it correctly shows in a new tab. If you follow the above steps can you check your link has the correct target set and expand on what happens when you click it? Depending on your browser it may be set to download based on mime-type rather than opening a new tab etc.

Categories