public static DocumentJSON.Document CompareDocumentImages(DocumentJSON.Document oldDocument, DocumentJSON.Document newDocument) { List <DocumentJSON.Image> oldImages = oldDocument.images.ToList <DocumentJSON.Image>(); List <DocumentJSON.Image> newImages = newDocument.images.ToList <DocumentJSON.Image>(); newDocument.lastImageId = oldDocument.lastImageId; string now = DateTime.Now.ToString("yyyymmdd"); foreach (DocumentJSON.Image img in newImages) { if (oldImages.Where(x => x.hash.Equals(img.hash)).Count() > 0) { DocumentJSON.Image oldImg = oldImages.Where(x => x.hash.Equals(img.hash)).First(); if (img.numberOfRepetition == oldImg.numberOfRepetition) { if (oldImages.Where(x => x.hash.Equals(img.hash)).First().status.Equals("d")) { img.status = "d"; } else { img.status = "o"; } img.date = oldImg.date; img.id = oldImg.id; img.version = oldImg.version; } else if (img.numberOfRepetition != oldImg.numberOfRepetition) { img.date = now; img.id = oldImg.id; img.status = "m"; img.version = newDocument.version; } oldImages.Remove(oldImg); } else { img.date = now; img.id = ++newDocument.lastImageId; img.status = "n"; img.version = newDocument.version; } } foreach (DocumentJSON.Image img in oldImages) { img.date = now; img.status = "d"; img.version = newDocument.version; newImages.Add(img); } newDocument.images = newImages.ToArray(); return(newDocument); }
public static DocumentJSON.Document CreateJSON(DocumentCore docCore, int version) { DocumentJSON.Document docJSON = new DocumentJSON.Document(); string now = DateTime.Now.ToString("yyyymmdd"); docJSON.date = now; docJSON.version = version; //Create a list of paragraphs List <DocumentJSON.Paragraph> parJSONList = new List <DocumentJSON.Paragraph>(); List <DocumentJSON.Image> imageJSONList = new List <DocumentJSON.Image>(); List <DocumentJSON.Cell> cellJSONList = new List <DocumentJSON.Cell>(); //loops through all sections int parId = 0; int imgId = 0; bool imgLoop = false; bool parLoop = false; int tableId = 0; foreach (Section sec in docCore.GetChildElements(false, ElementType.Section)) { //loop through tables foreach (Table t in sec.GetChildElements(false, ElementType.Table)) { foreach (TableRow r in t.GetChildElements(false, ElementType.TableRow)) { foreach (TableCell c in r.GetChildElements(false, ElementType.TableCell)) { DocumentJSON.Cell cell = new DocumentJSON.Cell(); cell.hash = CalculateHash(Encoding.UTF8.GetBytes(c.Content.ToString())); cell.content = c.Content.ToString(); cell.version = 1; cell.date = now; cell.status = "n"; cellJSONList.Add(cell); } } docJSON.lastTableId++; } docJSON.cells = cellJSONList.ToArray(); //gets all text on each section foreach (Paragraph par in sec.GetChildElements(false, ElementType.Paragraph)) { //gets images foreach (Picture p in par.GetChildElements(false, ElementType.Picture)) { imgLoop = true; MemoryStream stream = new MemoryStream(); stream = p.ImageData.GetStream(); BinaryReader binaryReader = new BinaryReader(stream); Byte[] data = binaryReader.ReadBytes((int)stream.Length); string hash = Document.CalculateHash(data); if (imageJSONList.Where(x => x.hash.Equals(hash)).Count() == 0) { DocumentJSON.Image image = new DocumentJSON.Image(); image.hash = hash; image.date = now; image.numberOfRepetition = 0; image.id = imgId++; image.status = "n"; image.version = version; imageJSONList.Add(image); } else { DocumentJSON.Image image = imageJSONList.Where(x => x.hash.Equals(hash)).First(); image.numberOfRepetition++; } } //guarantees that only paragraphs with actual text are saved if (!par.Content.ToString().Equals("\r\n") && !par.Content.ToString().Equals("")) { parLoop = true; DocumentJSON.Paragraph parJSON = new DocumentJSON.Paragraph(); parJSON.content = par.Content.ToString().Replace("trial", ""); if (parJSON.content.Contains("Created by the version of Document .Net 3.3.3.27!")) { parJSON.content = parJSON.content.Replace("Created by the version of Document .Net 3.3.3.27!\r\nThe version sometimes inserts \"\" into random places.\r\nGet the full version of Document .Net.\r\n", ""); } parJSON.hash = CalculateHash(Encoding.UTF8.GetBytes(parJSON.content)); parJSON.id = parId++; parJSON.version = version; parJSON.date = now; parJSON.status = "n"; parJSON.sentence = null; if (!parJSON.content.Equals("")) { parJSONList.Add(parJSON); } } } } docJSON.images = imageJSONList.ToArray(); if (parLoop) { docJSON.lastParId = --parId; } if (imgLoop) { docJSON.lastImageId = --imgId; } docJSON.paragraphs = parJSONList.ToArray(); return(docJSON); }