Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using (var client = new WebClient())
- {
- Console.WriteLine("Please choose a Word document to convert to PDF. All conversion will be done at http://www.convertapi.com server!");
- var openFileDialog = new OpenFileDialog { Filter = "Word document(*.doc;*.docx)|*.doc;*.docx"};
- if (openFileDialog.ShowDialog()!=DialogResult.OK) return;
- var fileToConvert = openFileDialog.FileName;
- Console.WriteLine("Converting the file {0} Please wait.", fileToConvert);
- var data = new NameValueCollection();
- data.Add("OutputFormat","jpg");
- data.Add("OutputFileName", "MyFile"); //Optional
- //!!!MANDATORY!!!
- data.Add("ApiKey", "YourApiKey"); //API Key must be set if you purchased membership with credits. Please login to your control panel to find out your API Key http://www.convertapi.com/prices
- try
- {
- client.QueryString.Add(data);
- var response = client.UploadFile("http://do.convertapi.com/word2pdf", fileToConvert);
- var responseHeaders = client.ResponseHeaders;
- var path = Path.Combine(@"C:\", responseHeaders["OutputFileName"]);
- File.WriteAllBytes(path, response);
- Console.WriteLine("The conversion was successful! The word file {0} converted to PDF and saved at {1}", fileToConvert, path);
- }
- catch (WebException e)
- {
- Console.WriteLine("Exception Message :" + e.Message);
- if (e.Status == WebExceptionStatus.ProtocolError)
- {
- Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
- Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement