Esempio n. 1
0
        public void Update(DocumentFormat.OpenXml.Wordprocessing.Tag tag, string value, MainDocumentPart mainPart)
        {
            string mainhtml = "<html><head><style type='text/css'>.catalogGeneralTable{border-collapse: collapse;text-align: left;} .catalogGeneralTable td, th{ padding: 5px; border: 1px solid #999999; } </style></head> <body style='font-family:Trebuchet MS;font-size:.9em;'>" + value + "</body></html>";


            int    blockLevelCounter = 1;
            string altChunkId        = String.Format("AltChunkId{0}", altChunkIdCounter++);

            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId);

            using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter stringWriter = new StreamWriter(chunkStream, Encoding.UTF8)) //Encoding.UTF8 is important to remove special characters
                {
                    stringWriter.Write(mainhtml);
                }
            }

            AltChunk altChunk = new AltChunk();

            altChunk.Id = altChunkId;

            SdtBlock sdtBlock = tag.Ancestors <SdtBlock>().Single();

            sdtBlock.InsertAt(altChunk, blockLevelCounter++);
        }
Esempio n. 2
0
        public static void Update(this DocumentFormat.OpenXml.Wordprocessing.Tag tag, object value, IDictionary <string, object> settings, MainDocumentPart mainPart)
        {
            ABlock block = GetBlock(tag, value);

            try
            {
                block.Update(tag, value, settings, mainPart);
            }
            catch (Exception exception)
            {
                throw new DuradosException("Could not update the block: " + tag.Val, exception);
            }
        }
Esempio n. 3
0
 public static BlockType GetBlockType(DocumentFormat.OpenXml.Wordprocessing.Tag tag, object value)
 {
     if (value is DataView || (tag.Ancestors <SdtBlock>().Count() > 0 && tag.Ancestors <SdtBlock>().Single().Descendants <Table>().Count() > 0))
     {
         return(BlockType.Table);
     }
     else if (tag.Parent.Parent is DocumentFormat.OpenXml.Wordprocessing.SdtBlock)
     {
         return(BlockType.Rich);
     }
     else
     {
         return(BlockType.Text);
     }
 }
Esempio n. 4
0
        public static ABlock GetBlock(this DocumentFormat.OpenXml.Wordprocessing.Tag tag, object value)
        {
            BlockType blockType = ABlock.GetBlockType(tag, value);

            switch (blockType)
            {
            case BlockType.Table:
                return(new TableBlock());

            case BlockType.Rich:
                return(new RichTextBlock());

            default:
                return(new TextBlock());
            }
        }
Esempio n. 5
0
        public override void Update(DocumentFormat.OpenXml.Wordprocessing.Tag tag, object value, IDictionary <string, object> settings, MainDocumentPart mainPart)
        {
            bool?completePage = false;

            if (settings != null && settings.ContainsKey("completePage"))
            {
                completePage = (bool)settings["completePage"];
            }

            int?completeHeight = 28;

            if (settings != null && settings.ContainsKey("linesInPage"))
            {
                completeHeight = (int)settings["linesInPage"];
            }

            Update(tag, (DataView)value, completePage, completeHeight, mainPart);
        }
Esempio n. 6
0
        public override void Update(DocumentFormat.OpenXml.Wordprocessing.Tag tag, object value, IDictionary <string, object> settings, MainDocumentPart mainPart)
        {
            string s = (value == null) ? string.Empty : value.ToString();

            Update(tag, s, mainPart);
        }
Esempio n. 7
0
        public void Update(DocumentFormat.OpenXml.Wordprocessing.Tag tag, DataView dataView, bool?completePage, int?linesInPage, MainDocumentPart mainPart)
        {
            // This should return only one table.
            SdtBlock sdtBlock = tag.Ancestors <SdtBlock>().Single();

            Table theTable = sdtBlock.Descendants <Table>().Single();

            var tableRows = theTable.Elements <TableRow>().ToList();


            TableRow theRow    = tableRows.Last();
            TableRow headerRow = tableRows.First();

            Orientation orientation = theRow.InnerText.ToLower().StartsWith("v") ? Orientation.Vertical : Orientation.Horizontal;

            for (int i = tableRows.Count() - 1; i >= 1; i--)
            {
                TableRow row = tableRows[i];

                theTable.RemoveChild(row);
            }


            var rowCells = theRow.Descendants <TableCell>().ToList();

            for (int j = 0; j < rowCells.Count; j++)
            {
                //TableCell cell = rowCells[j];
                theRow.Descendants <TableCell>().ElementAt(j).RemoveAllChildren <Paragraph>();
            }

            var headerCells = headerRow.Descendants <TableCell>().ToList();


            int totaLines = 0;

            int rowCount = 0;

            if (orientation == Orientation.Horizontal)
            {
                foreach (DataRowView row in dataView)
                {
                    TableRow rowCopy = (TableRow)theRow.CloneNode(true);

                    for (int j = 0; j < headerCells.Count; j++)
                    {
                        //TableCell cell = headerCells[j];
                        //string value = string.Empty;
                        //value = row.Row[j].ToString();
                        //rowCopy.Descendants<TableCell>().ElementAt(j).Append(new Paragraph
                        //(new Run(new Text(value))));
                        rowCopy.Descendants <TableCell>().ElementAt(j).Append(GetElement(dataView, row.Row, j, mainPart));
                    }

                    theTable.AppendChild(rowCopy);
                    rowCount++;

                    if (completePage.HasValue && completePage.Value && linesInPage.HasValue)
                    {
                        totaLines += GetRowLines(theTable, rowCopy);
                    }
                }
            }
            else
            {
                foreach (DataRowView row in dataView)
                {
                    for (int j = 0; j < headerCells.Count; j++)
                    {
                        TableRow rowCopy = (TableRow)theRow.CloneNode(true);

                        rowCopy.Descendants <TableCell>().ElementAt(0).Append(GetElement(dataView, row.Row, j, mainPart));
                        rowCopy.Descendants <TableCell>().ElementAt(1).Append(new Paragraph(new Run(new Text(""))));

                        theTable.AppendChild(rowCopy);
                        rowCount++;

                        if (completePage.HasValue && completePage.Value && linesInPage.HasValue)
                        {
                            totaLines += GetRowLines(theTable, rowCopy);
                        }
                    }
                }
            }


            //if (orientation == Orientation.Horizontal)
            //{
            if (completePage.HasValue && completePage.Value && linesInPage.HasValue)
            {
                for (int i = totaLines % linesInPage.Value; i < linesInPage.Value; i++)
                {
                    TableRow rowCopy = (TableRow)theRow.CloneNode(true);

                    for (int j = 0; j < headerCells.Count; j++)
                    {
                        string    value = string.Empty;
                        TableCell cell  = headerCells[j];
                        rowCopy.Descendants <TableCell>().ElementAt(j).Append(new Paragraph
                                                                                  (new Run(new Text(value))));
                    }

                    theTable.AppendChild(rowCopy);
                }
            }
            //}
        }
Esempio n. 8
0
        //public virtual string GetTagName(SdtBlock sdtBlock)
        //{
        //    return sdtBlock.SdtProperties.GetFirstChild<DocumentFormat.OpenXml.Wordprocessing.Tag>().Val;
        //}

        public abstract void Update(DocumentFormat.OpenXml.Wordprocessing.Tag tag, object value, IDictionary <string, object> settings, MainDocumentPart mainPart);
Esempio n. 9
0
 public void Update(DocumentFormat.OpenXml.Wordprocessing.Tag tag, string value, MainDocumentPart mainPart)
 {
     tag.Parent.Parent.Descendants <Text>().Single().Text = value;
 }