Access Violation runtime error when getting WebBrowser .Url property - c#

Getting an "Access Violation" runtime error when trying to get a WebBrowser's .Url property in an Outlook Add-In I'm writing. Need some help on how to get past this please. Research indicates I may need to set "FullTrust" permissionset, have tried that with no success.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security;
using System.Security.Permissions;
namespace MyAddin1
{
public partial class authForm : Form
{
public string currentUrl;
public authForm()
{
InitializeComponent();
}
private void formLoaded(object sender, EventArgs e)
{
WebBrowser browser = new WebBrowser();
browser.Location = new System.Drawing.Point(0, 0);
browser.Width = 870;
browser.Height = 510;
browser.Navigate("https://www.yammer.com/dialog/oauth?client_id=<redacted>&response_type=token");
browser.Show();
//runtime error occurs on following line
currentUrl = browser.Url.ToString();
browser.Url = new Uri("http://www.yahoo.com");
browser.Navigated += new WebBrowserNavigatedEventHandler(checkForToken);
this.Controls.Add(browser);
}
private void checkForToken(object sender, EventArgs e)
{
MessageBox.Show(currentUrl);
}
}
}

browser.Url is initially null, Change your code as below.
if (browser.Url != null)
{
currentUrl = browser.Url.ToString();
}

Related

I currently have this code to print labels, but now I want to add a loop or function to control how many copies the label prints

For this I have a Text Box, that would be the amount of copies that are going to be printed, that is to say if the user puts 2, 2 would be printed and so on.
But I can't figure out how to take the number typed by the user in the textbox and use it in the for condition, I tried to put For(int i =1;i>1;i++) before the If but it generates an infinite loop of the print box and still only prints one copy.
I use this code, and the class RawPrinterHelper
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Código_zpl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
string x = "^XA^LH30,30\n^FO20,10^ADN,90,50^AD^FDHello World^FS\n^XZ";
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName,x);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
I do not have much reputation to comment on the conversation between you and Trix. But I would have suggested you created a method that accepts a parameter. that when calling, you can pass the text.
void printcopies(int numberofcopies){
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
pd.PrinterSettings.Copies = numberofcopies;
....
}
}
....

the name 'imagePdfDocument' does not exist in current context

Why I am getting the error "the name 'imagePdfDocument' does not exist in current context."
Can anyone tell me how to fix it and why is this error message showing..
Thanks in advance.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using C1.Win.C1Tile;
using System.Diagnostics;
private void _exportImage_Click(object sender, EventArgs e)
{
List<Image> images = new List<Image>();
foreach (Tile tile in _imageTileControl.Groups[0].Tiles)
{
if (tile.Checked)
{
images.Add(tile.Image);
}
}
ConvertToPdf(images);
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.DefaultExt = "pdf";
saveFile.Filter = "PDF files (*.pdf)|*.pdf*";
if (saveFile.ShowDialog() == DialogResult.OK)
{
imagePdfDocument.Save(saveFile.FileName);
}
}
private void ConvertToPdf(List<Image> images)
{
RectangleF rect = imagePdfDocument.PageRectangle;
bool firstPage = true;
foreach (var selectedimg in images)
{
if (!firstPage)
{
imagePdfDocument.NewPage();
}
firstPage = false;
rect.Inflate(-72, -72);`enter code here`
imagePdfDocument.DrawImage(selectedimg, rect);
}
}
Look what you have to do..... go to reference option of your project and uninstall the one which are identical, there may be same reference with different version.

Are there any flags i need to provide to CefSharp to work with document elements?

I converted a window of mine to use CefSharp in order to render some PDF file. One of the features the old component supported (WebBrowser of WPF, which unfortunately depends on IE, which does not work in this case) is accessing javascript elements through code in order to be aware of when the PDF has been scrolled to the bottom. Unfortunately this does not appear to work with CefSharp with the following code. Perhaps i am missing some initializer flag for this to work?
Currently in my Logs All i get is ScrollProgress|0|0|734
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CefSharp;
using NLog;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;
using WebBrowser = System.Windows.Forms.WebBrowser;
namespace MyNamespace
{
public partial class PdfConfirmWindow : Window
{
private static readonly ILogger Log = LogManager.GetLogger(nameof(PdfConfirmWindow));
public PdfConfirmWindow()
{
InitializeComponent();
}
private void WindowLoaded(object sender, RoutedEventArgs e)
{
ChromiumWebBrowser.LoadingStateChanged += ChromiumWebBrowserOnLoadingStateChanged;
UpdateBrowserContent();
}
private void ChromiumWebBrowserOnLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (!e.IsLoading)
{
Log.Debug("Loading completed");
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
// (window.innerHeight + window.scrollY) >= document.body.offsetHeight
// e.scrollHeight - e.scrollTop === e.clientHeight
// CefSharp.PostMessage('EndOfDocumentReached');
const string script = #"
(function(){
var intervalHandler;
function onScrollCallback() {
var e = document.body;
var values = ['ScrollProgress'];
values.push(document.body.clientHeight);
values.push(window.scrollY);
values.push(window.innerHeight);
CefSharp.PostMessage(values.join('|'));
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight){
//clearInterval(intervalHandler);
}
}
setTimeout(function(){
var intervalHandler = setInterval(function(){
onScrollCallback();
}, 100);
}, 500);
})();
";
ChromiumWebBrowser.Focus();
ChromiumWebBrowser.WebBrowser.JavascriptMessageReceived += WebBrowserOnJavascriptMessageReceived;
ChromiumWebBrowser.WebBrowser.ExecuteScriptAsync(script);
}));
}
}
private async void WebBrowserOnJavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e)
{
await UIHelper.Thread;
// ChromiumWebBrowser.WebBrowser.JavascriptMessageReceived -= WebBrowserOnJavascriptMessageReceived;
if (e.Message?.ToString().Equals("EndOfDocumentReached", StringComparison.OrdinalIgnoreCase) == true)
{
SetReachedStatus(true);
}
if (e.Message?.ToString().StartsWith("ScrollProgress", StringComparison.OrdinalIgnoreCase) == true)
{
Log.Debug(e.Message?.ToString());
}
}
}

error in text direction when i try to extract text from arabic pdf file using itextsharp c#

i have problem in my code
i am trying to read text from arabic rtl file using itextsharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
namespace Masmo3
{
public partial class FrmMain : Form
{
string book = null;
public FrmMain()
{
InitializeComponent();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
BtnOpen.Enabled = false;
book = null;
using(OpenFileDialog Ofd = new OpenFileDialog() {Filter= "PDF files (*.pdf)|*.pdf|txt files (*.txt)|*.txt",ValidateNames= true, Multiselect= false})
{
if (Ofd.ShowDialog() == DialogResult.OK)
{
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(Ofd.FileName);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
sb.Append(PdfTextExtractor.GetTextFromPage(reader, i));
book = sb.ToString();
}
reader.Close();
}
}
BtnOpen.Enabled = true;
}
}
}
this code show my text like "ﻢﯿﺣﺮﻟا ﻦﻤﺣﺮﻟا ﷲ ﻢﺴﺑ" its wrong because its rtl language the correct way "بسم الله الرحمن الرحيم"
what i can do to get correct text direction ?
"بسم الله الرحمن الرحيم"
please use ColumnText object to set the DIrection to be RTL
CT.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
Good luck bro

C# Using Youtube API - How do I check the last time a particular user uploaded a video to Youtube?

I know how to upload a video to Youtube using Youtube API within C#
But I'd like to use the Youtube API to return a date when a particular user last uploaded a video.
My code for uploading a video using C# is below, but I really don't know how to do the above???
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.GData.Client;
using Google.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.GData.Extensions.Location;
namespace UploadLimit
{
public partial class Form1 : Form
{
string key = "somegarbage";
string appName = "SlowUpload";
string username = "blarg";
string password = "blarg";
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
txtFile.Text = dialog.FileName;
}
private void btnUpload_Click(object sender, EventArgs e)
{
YouTubeRequestSettings settings = new YouTubeRequestSettings(appName, key, username, password);
YouTubeRequest request = new YouTubeRequest(settings);
Video newVideo = new Video();
newVideo.Title = "My Test Movie";
newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "cars, funny";
newVideo.Description = "My description";
newVideo.YouTubeEntry.Private = false;
newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag",
YouTubeNameTable.DeveloperTagSchema));
newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
// alternatively, you could just specify a descriptive string
// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");
newVideo.YouTubeEntry.MediaSource = new MediaFileSource(txtFile.Text, "video/quicktime");
Video createdVideo = request.Upload(newVideo);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UploadLimit
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
You can get all the user's videos and get the latest date:
var videos = new YouTubeRequest().GetVideoFeed(userId).Entries
DateTime lastUploadDate = videos.Max(video => video.YouTubeEntry.Published)
To get the actual video's title:
var lastVideo = videos.Where(video => video.YouTubeEntry.Published == lastUploadDate).First();
var name = lastVideo.Title

Categories