コード例 #1
0
        public async Task <HttpResponseMessage> SideCover()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, "The request doesn't contain valid content!"));
            }

            try
            {
                var provider = new MultipartMemoryStreamProvider();
                await Request.Content.ReadAsMultipartAsync(provider);

                foreach (var file in provider.Contents)
                {
                    var dataStream = await file.ReadAsByteArrayAsync();

                    // use the data stream to persist the data to the server (file system etc)
                    Stream stream = new MemoryStream(dataStream);

                    var dict = new Dictionary <string, int>();
                    dict.Add("tentruong", 0);
                    dict.Add("hoten", 1);
                    dict.Add("khoa", 2);
                    dict.Add("he", 3);
                    dict.Add("tieude", 4);
                    dict.Add("nganh", 5);
                    dict.Add("maso", 6);
                    dict.Add("detai", 7);
                    dict.Add("gvhd", 8);
                    dict.Add("nam", 9);

                    MemoryStream ms   = new MemoryStream();
                    DocxView     view = GenResult(stream, dict);



                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DocxView));
                    ser.WriteObject(ms, view);
                    string json     = Encoding.Default.GetString(ms.ToArray());
                    var    response = Request.CreateResponse(HttpStatusCode.OK);
                    response.Content = new StringContent(json, Encoding.UTF8, "text/plain");
                    response.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(@"text/html");
                    return(response);
                }
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message));
            }
            return(null);
        }
コード例 #2
0
        public DocxView GenResult(Stream stream, Dictionary <string, int> col)
        {
            var view = new DocxView();
            var docx = DocX.Load(stream);
            WordprocessingDocument doc = WordprocessingDocument.Open(stream, false);
            var convert        = new Extends.Library.Convert();
            var ml             = convert.PaperClipsToCentimeters(docx.MarginLeft);
            var paragraphInfos = new List <ParagraphInfo>();

            view.Page.Layout.Margin.Top    = docx.MarginTop;
            view.Page.Layout.Margin.Bottom = docx.MarginBottom;
            view.Page.Layout.Margin.Left   = docx.MarginLeft;
            view.Page.Layout.Margin.Right  = docx.MarginRight;
            view.Page.Layout.Size.Width    = docx.PageWidth;
            view.Page.Layout.Size.Height   = docx.PageHeight;
            var pps = new Models.PaperSize();

            view.Page.Layout.Size.PaperType = pps.PaperType(view.Page.Layout.Size);

            var paragraphs = doc.MainDocumentPart.Document.Descendants <DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
            int pageIdx    = 1;

            foreach (var paragraph in paragraphs)
            {
                var run  = paragraph.GetFirstChild <DocumentFormat.OpenXml.Wordprocessing.Run>();
                var info = new ParagraphInfo
                {
                    Paragraph  = paragraph,
                    PageNumber = pageIdx
                };

                if (!paragraph.InnerText.Trim().Equals(""))
                {
                    paragraphInfos.Add(info);
                }


                if (run != null)
                {
                    var lastRenderedPageBreak = run.GetFirstChild <LastRenderedPageBreak>();
                    var pageBreak             = run.GetFirstChild <Break>();


                    if (lastRenderedPageBreak != null || pageBreak != null)
                    {
                        pageIdx++;
                    }
                }
            }

            foreach (var c in col)
            {
                view.Cover.Paragraphs[c.Key] = GetParagraph(paragraphInfos, docx, c.Value);
            }

            return(view);
            //view.Cover.Paragraphs["tentruong"] = GetParagraph(paragraphInfos, docx, 0);
            //view.Cover.Paragraphs["hoten"] = GetParagraph(paragraphInfos, docx, 1);
            //view.Cover.Paragraphs["khoa"] = GetParagraph(paragraphInfos, docx, 2);
            //view.Cover.Paragraphs["he"] = GetParagraph(paragraphInfos, docx, 3);
            //view.Cover.Paragraphs["tieude"] = GetParagraph(paragraphInfos, docx, 4);
            //view.Cover.Paragraphs["nganh"] = GetParagraph(paragraphInfos, docx, 5);
            //view.Cover.Paragraphs["detai"] = GetParagraph(paragraphInfos, docx, 6);
            //view.Cover.Paragraphs["nam"] = GetParagraph(paragraphInfos, docx, 7);
        }