private static wikidata GetDataFromWiki(string sitename) { wikidata data = new wikidata(); string WikiaddressSuffix = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles="; try { WebClient client = new WebClient(); string address = WikiaddressSuffix + sitename; var response = client.DownloadString(address); var responseJson = JsonConvert.DeserializeObject <WikipediaClass.RootObject>(response); data.key = Convert.ToInt32(responseJson.query.pages.First().Key); data.value = responseJson.query.pages[data.key.ToString()].extract.ToString(); return(data); } catch (Exception e) { log.Error(e); return(data); } }
public string GenerateReportPdf(Marks marks, Coordinates coor, ReportExtraInfo reportextrainfo) { try { string serial = Guid.NewGuid().ToString(); string FinalAddressAndReportName = string.Empty; string WordReport = docxReportPath + Path.DirectorySeparatorChar + serial + wordExnt; File.Copy(templateDoc, WordReport, true); Application wordApp = new Application { Visible = true }; Document aDoc = wordApp.Documents.Open(WordReport, ReadOnly: false, Visible: true); aDoc.Activate(); FindAndReplaceMainDoc(wordApp, "[Date]", DateTime.Now.ToString("dd/MM/yyyy")); FindAndReplaceMainDoc(wordApp, "[Serial_Number]", serial); FindAndReplaceMainDoc(wordApp, "[Project Name]", reportextrainfo.ProjName); FindAndReplaceMainDocHeaderAndFooter(aDoc, "[Project Name]", reportextrainfo.ProjName); FindAndReplaceMainDoc(wordApp, "[Company Name]", reportextrainfo.CustomerName); FindAndReplaceMainDoc(wordApp, "[Title]", "Indexes Value of - Coordinates " + Math.Round(coor.lon, 4) + "/" + Math.Round(coor.lat, 4)); FindAndReplaceMainDoc(wordApp, "[Coordinates]", "Coordinates " + Math.Round(coor.lon, 4) + "/" + Math.Round(coor.lat, 4)); // let's get the full address of the location according to the coordinates using OSM reverse geocoding service GeoCodingServices service = new GeoCodingServices(); AddressResult address = service.ReverseGeoCoding(coor.lon, coor.lat); string country = string.IsNullOrEmpty(address.Country) ? "" : Convert.ToString(address.Country); string city = string.IsNullOrEmpty(address.City) ? "" : ", " + Convert.ToString(address.City); string road = string.IsNullOrEmpty(address.Road) ? "" : ", " + Convert.ToString(address.Road); string housenumber = string.IsNullOrEmpty(address.HouseNumber) ? "" : ", " + Convert.ToString(address.HouseNumber); string postalcode = address.PostCode == "no" ? "" : ", " + Convert.ToString(address.PostCode); string Fulladdress = country + city + road + housenumber + postalcode; if (!string.IsNullOrEmpty(Fulladdress)) { FinalAddressAndReportName += StringSeparator + "Address:" + Fulladdress; FindAndReplaceMainDoc(wordApp, "[Address]", Fulladdress); FindAndReplaceMainDoc(wordApp, "[Site Name]", Fulladdress); // if the city name exist, then let's check if the city name have value in wikipedia or not if (!string.IsNullOrEmpty(address.City)) { wikidata data = GetDataFromWiki(address.City); if (data.key != -1) { object oBookMark = "SiteInfo"; aDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = data.value; } else { FindAndReplaceMainDoc(wordApp, "[Site Brief Info]", " "); FindAndReplaceMainDoc(wordApp, "Source: https://en.wikipedia.org/", " "); } } } else { FinalAddressAndReportName += StringSeparator + "Address:" + " "; FindAndReplaceMainDoc(wordApp, "[Address]", " "); FindAndReplaceMainDoc(wordApp, "[Site Name]", " "); FindAndReplaceMainDoc(wordApp, "[Site Brief Info]", " "); FindAndReplaceMainDoc(wordApp, "Source: https://en.wikipedia.org/", " "); } // let's get the site FindAndReplaceMainDoc(wordApp, "{EG}", Math.Round(marks.RadiationEG, 2).ToString()); FindAnndReplaceShapes(aDoc, "{EG}", Math.Round(marks.RadiationEG, 2).ToString()); FindAndReplaceMainDoc(wordApp, "[EG Comments]", CheckinterferenceValue(Types.Radiation, Math.Round(marks.RadiationEG, 2))); FindAndReplaceMainDoc(wordApp, "{SC}", Math.Round(marks.SoilPollution, 2).ToString()); FindAnndReplaceShapes(aDoc, "{SC}", Math.Round(marks.SoilPollution, 2).ToString()); FindAndReplaceMainDoc(wordApp, "[SC Comments]", CheckinterferenceValue(Types.Soil, Math.Round(marks.SoilPollution, 2))); FindAndReplaceMainDoc(wordApp, "{EC}", Math.Round(marks.Ecology, 2).ToString()); FindAnndReplaceShapes(aDoc, "{EC}", Math.Round(marks.Ecology, 2).ToString()); FindAndReplaceMainDoc(wordApp, "[EC Comments]", CheckinterferenceValue(Types.Ecology, Math.Round(marks.Ecology, 2))); FindAndReplaceMainDoc(wordApp, "{NO}", Math.Round(marks.NoiseInterference, 2).ToString()); FindAnndReplaceShapes(aDoc, "{NO}", Math.Round(marks.NoiseInterference, 2).ToString()); FindAndReplaceMainDoc(wordApp, "[NO Comments]", CheckinterferenceValue(Types.Noise, Math.Round(marks.NoiseInterference, 2))); FindAndReplaceMainDoc(wordApp, "{AQ}", Math.Round(marks.AirQuality, 2).ToString()); FindAnndReplaceShapes(aDoc, "{AQ}", Math.Round(marks.AirQuality, 2).ToString()); FindAndReplaceMainDoc(wordApp, "[AO Comments]", CheckinterferenceValue(Types.AirQuality, Math.Round(marks.AirQuality, 2))); // will be change in the future to Solid Waste Management Mark and comments FindAndReplaceMainDoc(wordApp, "{SW}", Math.Round(marks.SoilPollution, 2).ToString()); FindAnndReplaceShapes(aDoc, "{SW}", Math.Round(marks.SoilPollution, 2).ToString()); FindAndReplaceMainDoc(wordApp, "[SW Comments]", " "); // overall index value FindAndReplaceMainDoc(wordApp, "{M}", Math.Round(marks.FinalMark, 2).ToString()); FindAnndReplaceShapes(aDoc, "{M}", Math.Round(marks.FinalMark, 2).ToString()); AddMap(aDoc, coor); //string WordReport = docxReportPath + Path.DirectorySeparatorChar + serial + wordExnt; //wordApp.Documents[1].SaveAs2(WordReport, WdSaveFormat.wdFormatDocumentDefault); wordApp.Documents[1].Save(); string pdfReport = pdfReportPath + Path.DirectorySeparatorChar + serial + PdfExnt; wordApp.Documents[1].SaveAs2(pdfReport, WdSaveFormat.wdFormatPDF); wordApp.Documents[1].Close(); wordApp.Quit(); int res = System.Runtime.InteropServices.Marshal.ReleaseComObject(aDoc); int res1 = System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp); GC.Collect(); // let's copy the file to authenticate folder for user downloads. FinalAddressAndReportName += StringSeparator + "Report:" + serial + PdfExnt; string downloadFile = pdfReportDownlaodPath + Path.DirectorySeparatorChar + serial + PdfExnt; File.Copy(pdfReport, downloadFile, true); return(FinalAddressAndReportName); } catch (Exception e) { log.Error(e); return(String.Empty); } }