I'm trying to figure out how to convert a docX file that was downloaded as a byte stream and written to a file into a PDF. I'd rather not pay for any packages. I tried Sautinsoft pdfmetamorphosis, but couldn't get that to work.
Here's my code:
async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
Publication p = (Publication)e.SelectedItem;
Debug.WriteLine(p);
if (p.folderID.Equals("-1"))
{
using (Stream respStream = await post(p.docNum))
{
string ext = p.appextension.ToLower();
byte[] buffer = new byte[respStream.Length];
respStream.Read(buffer, 0, buffer.Length);
string path = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/downloadedFile." + ext;
File.WriteAllBytes(path, buffer);
switch (ext)
{
case "pdf":
await Navigation.PushAsync(new PDFViewPage(path));
break;
case "docx":
//SautinSoft.PdfMetamorphosis converter = new SautinSoft.PdfMetamorphosis();
//string pdfPath = Path.ChangeExtension(path, ".pdf");
//converter.DocxToPdfConvertFile(path, pdfPath);
//await Navigation.PushAsync(new PDFViewPage(pdfPath));
//DocumentModel.Load(path).Save(Path.ChangeExtension(path, ".pdf"));
//await Navigation.PushAsync(new PDFViewPage(path));
await DisplayAlert("Error", ".docx files are not supported at this time", "Return");
break;
default:
Debug.WriteLine("wasn't pdf");
Debug.WriteLine("was a ." + ext);
break;
}
}
}
else
{
await Navigation.PushAsync(new PublicationsPage(p.folderID));
}
}
private async Task<Stream> post(string id)
{
Dictionary<string, string> dir = new Dictionary<string, string>();
dir.Add("LoginID", App.user.login_id);
dir.Add("docID", id);
var jsonReq = JsonConvert.SerializeObject(dir);
Debug.WriteLine("req: " + (String)jsonReq);
var content = new StringContent(jsonReq, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content);
var responseString = await response.Content.ReadAsStreamAsync();
return responseString;
}
}