Open PDF file in RichTextBox in WPF - c#

Can I open a PDF file in RichTextBox?

Short answer: No.
Longer answer: No. A RichTextBox is for displaying rich text. PDFs can contain anything including text, but that's not the document model underlying the RichTextBox. Besides, WPF does not handle PDF natively. There are third-party controls, however.
This question also has some pointers which may be of use to you, albeit not using a RichTextBox.

You need to use the Acrobat Control for ActiveX or at least the Adobe Reader 9 equivalent and use as
using PdfLib;
namespace WindowsFormsApplication1{
public partial class ViewerForm : Form{
public ViewerForm()
{
InitializeComponent();
PdfLib.AxAcroPDF axAcroPDF1;
axAcroPDF1.LoadFile(#"C:\Documents and Settings\jcrowe\Desktop\Medical Gas\_0708170240_001.pdf");
axAcroPDF1.Show(); }
private void richTextBox1_TextChanged(object sender, EventArgs e)
{ } } }

You can write a simple app in a few seconds containing a WebBrowser control, and just call the navigate method and give it a URL pointing to the document you want.
XAML:
<Grid>
<WebBrowser x:Name="Browser"/>
</Grid>
C#:
private void Window1_Loaded(object sender, WindowLoadedArgs args)
{
Browser.Navigate(new URL("path to document.pdf");
}
Note: I am writing from memory so consider this pseudocode rather than something that will work as-is.

Related

WPF - Copying the content of a RichTextBox into a Paragraph

I'm currently trying to create a simple program in WPF to make cards for a custom card game.
Some of the descriptions feature rich text (like bold and italics) to highlight important things. The effect and the flavor text are copied into the same RichTextBox for aesthetic and convenience, which is why I need to be able to copy rich text from an input into a line/paragraph that is added to the display.
Here is the code:
private void EffectInput_TextChanged(object sender, TextChangedEventArgs e)
{
TextRange temp = new TextRange(EffectInput.Document.ContentStart, EffectInput.Document.ContentEnd);
Console.WriteLine(temp.Text);
effectText.Inlines.Clear();
effectText.Inlines.Add(new Run(temp.Text));
updateDescription();
}
private void updateDescription()
{
Description.Document.Blocks.Clear();
Description.Document.Blocks.Add(effectText);
Description.Document.Blocks.Add(flavorText);
}
I managed to make it work with the Text inside the box, but I can't find a way to also copy the format of the text. Can anyone help me?

C# Winforms WebBrowser open links in default browser

I know this has been discussed several times around here but the default behaviour for opening links
clicked in a WebBrowser control does not work for my application.
So while this works as in it opens a link clicked in IE:
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
I am using a dropdown list to update the html file that the webBrowser is displaying like so:
private void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Url = myURI;
}
Now the problem I'm having is that with the _Navigating method above, the webBrowser does not load any subsequent changes to the URL (thanks to the e.cancel I guess) so it only displays the first html file it loads.
If I remove the _Navigating method it updates fine but then the links open up in the same webBrowser control which is what I do not want.
How can I get it to work both ways?
I hope this can help you.
If you want to open a link in a browser, you can add this simple code:
Process.Start("http://google.com");
Remember, there is a lot of information about it. Here in stack Overflow you can take a look in this post: How to open in default browser in C#
If you want to open your link in another browser, you can use this code:
System.Diagnostics.Process.Start("firefox.exe", "http://www.google.com");
Don't forget to visit this post called: How do I open alternative webbrowser (Mozilla or Firefox) and show the specific url?
And Finally, I could recommend you this stack overtflow post called: .NET C#: WebBrowser control Navigate() does not load targeted URL
I hope this information can help you a little bit.
This is an old post but I believe I may understand what the original poster wanted to do. They wanted a page to load in the webbrowser control if the user selected it from the dropdown list but any links in the loaded page should open in the user's web browser. If this is indeed the case, the original poster need a flag on the form to determine the behavior.
The original poster simply needed a flag such as linksOpenInSystemBrowser shown below.
using System;
using System.Windows.Forms;
namespace Browser_Test
{
public partial class myForm : Form
{
private bool linksOpenInSystemBrowser = false;
public myForm()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
linksOpenInSystemBrowser = false;
webBrowser1.Navigate(comboBox1.SelectedItem.ToString());
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if(!linksOpenInSystemBrowser)
{
linksOpenInSystemBrowser = true;
return;
}
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
}
}

Can a FixedDocument be converted to a Drawing.Printing.PrintDocument?

I have written a Label making program for work. It uses an opendialog to pull in the data. Splits it up and put it in tables. No issues there. Setting up the the FixedDocument to print the user control labels as well as the class that stores the Fixed Document Data all work great.
My only issue is I can't stand the restrictions on a WPF document when it comes to text searching and print control. I have gone with a printpreviewdialog but this does not use FixedDocument.
I am needing to know if it can be converted with a simple bit of code or if i Have to rewrite my entire class and go back to the drawing that printpreviewdialog uses.
The code for the call is below.
private void button3_Click(object sender, EventArgs e)
{
var avery = new Avery5160();
DataTable data = (DataTable)dataGridView1.DataSource;
var A5160 = avery.CreateDocument(data);
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = A5160;
ppd.ShowDialog();
}
The error is :
Cannot implicitly convert type 'System.Windows.Documents.FixedDocument' to
'System.Drawing.Printing.PrintDocument'
Thanks for any assistance.
I am not so sure there is a simple conversion. A simple (if not a little time-consuming) method would be to create a preview dialog with a document viewer to simulate what you would be seeing. Then printing using the FixedDocument's DocumentPaginator and a regular PrintDialog.
The answer to this question shows how to do it by creating an XpsDocument that is then used with a custom PrintPreview class.

HTML to Image: The Confusion Begins

Currently I load an HTML string into a webBrowser control's contents and tell the user to print-screen it.
I'd like to somehow either grab the webBrowser contents as an image, or somehow render this HTML to an image file that can be saved. Are there any free libraries to do this?
I have been curious as to how this is accomplished myself. I have not tried to do this, but here is a link to a CodeProject article that seems to describe the process quite well:
HTML to Image in C#
I use a program called Webshot and I absolutely love it. It has a freeware version with upgrade available. It even has a callable command line interface which can be used in an automated fashion.
Link here.
Well, there's bound to be a better solution than this, but I'm pretty sure it's better than your solution (I haven't checked out the other answers), so...
I have a working C# program that saves a bitmap image of a control. Currently, the control is a textbox, but you could easily make it a web browser control. The entire program is below. The form has a button, button1, and a textbox, textBox1.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(textBox1.Width,textBox1.Height);
textBox1.DrawToBitmap(bm,new Rectangle(0,0,bm.Width,bm.Height));
bm.Save("image.bmp");
}
}
}

In Silverlight, what's the difference between UserControl_Loaded and Page_Loaded?

I'm trying to write a silverlight application that takes in InitParams and then uses those InitParams to make a change to the Source of the MediaElement on the page. I'm trying to figure out the proper place to put my code.
I watched Tim Heuer's excellent video on InitParams, but in the video (which was for Silverlight 2), it shows the following on the Page.xaml.cs:
void Page_Loaded(object sender, RoutedEventArgs e)
{
}
I don't see Page_Loaded when I open MainPage.xaml.cs, and I'm wondering if that was automatically created in the Silverlight 2 SDK and left out of the Silverlight 3 SDK. Or perhaps Tim added that in his video manually.
I find that I can go into the opening UserControl tag of MainPage.xaml and add Loaded="<New_Event_Handler>" which creates the following in MainPage.xaml.cs:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
By default, there's also the following in MainPage.xaml.cs, which is run during the Application_Startup event in App.xaml.cs:
public MainPage()
{
InitializeComponent();
}
I need to figure out where is the best place to insert my code to change the Source on my MediaElement in my xaml. Should I put it in MainPage? Should I add the Loaded event handler and put it into UserControl_Loaded? If it's supposed to be Page_Loaded, where do I find that in Silverlight 3?
Any help would be much appreciated.
"UserControl_Loaded" and "Page_Loaded" are just method names and the names don't matter (you could name the method "Foo" if you wanted). What makes these methods do anything is the fact that they are attached to the Loaded event on the UserControl (which is what you did when you edited the MainPage.xaml file).

Categories