public override IText Add() { OpenXmlSdk.Text openXmlText = new OpenXmlSdk.Text(); _range.InnerObject.Append(openXmlText); OpenXmlSdkText text = new OpenXmlSdkText(_range, openXmlText); if (text != null) { Add(text); } return text; }
public static Wordprocessing.TableCell AddCell(this Wordprocessing.TableRow tableRow, string value) { Wordprocessing.TableCell tableCell = new Wordprocessing.TableCell(); Wordprocessing.Paragraph par = new Wordprocessing.Paragraph(); Wordprocessing.ParagraphProperties parprop = new Wordprocessing.ParagraphProperties(); Wordprocessing.Justification justification1 = new Wordprocessing.Justification() { Val = Wordprocessing.JustificationValues.Center }; Wordprocessing.SpacingBetweenLines spacingBetweenLines2 = new Wordprocessing.SpacingBetweenLines() { After = "0"}; parprop.Append(spacingBetweenLines2); parprop.Append(justification1); par.Append(parprop); Wordprocessing.Run run = new Wordprocessing.Run() ; Wordprocessing.Text text = new Wordprocessing.Text(value); run.Append(text); par.Append(run); tableCell.Append(par); tableRow.Append(tableCell); return tableCell; }
private static void GenerateHeaderPartContent(HeaderPart part) { Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } }; header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" }; ParagraphProperties paragraphProperties1 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" }; paragraphProperties1.Append(paragraphStyleId1); Run run1 = new Run(); DocumentFormat.OpenXml.Wordprocessing.Text text1 = new DocumentFormat.OpenXml.Wordprocessing.Text(); text1.Text = "Header"; run1.Append(text1); paragraph1.Append(paragraphProperties1); paragraph1.Append(run1); header1.Append(paragraph1); part.Header = header1; }
public void FiftyPercentageEMFontSize() { string html = "<div style=\"font-size:0.50em\">test</div>"; using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser(html)); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph para = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties properties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(properties); Assert.AreEqual(1, properties.ChildElements.Count); FontSize fontSize = properties.ChildElements[0] as FontSize; Assert.IsNotNull(fontSize); Assert.AreEqual("12", fontSize.Val.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
public void CenterInsideTextAlignLeftDiv() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<div style=\"text-align:left\"><center>test</center></div>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph para = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(2, para.ChildElements.Count); ParagraphProperties paraProperties = para.ChildElements[0] as ParagraphProperties; Assert.IsNotNull(paraProperties); Assert.AreEqual(1, paraProperties.ChildElements.Count); Justification justification = paraProperties.ChildElements[0] as Justification; Assert.IsNotNull(justification); Assert.AreEqual(JustificationValues.Center, justification.Val.Value); Run run = para.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); Word.Text text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public void UnderlineStrike() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<s><u>test</u></s>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph para = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties properties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(properties); Assert.AreEqual(2, properties.ChildElements.Count); Strike underline = properties.ChildElements[0] as Strike; Assert.IsNotNull(underline); Underline strike = properties.ChildElements[1] as Underline; Assert.IsNotNull(strike); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
public void SpanSubscriptStyle() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<span style=\"vertical-align:sub\">test</span>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph para = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties properties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(properties); Assert.AreEqual(1, properties.ChildElements.Count); VerticalTextAlignment verticalTextAlignment = properties.ChildElements[0] as VerticalTextAlignment; Assert.IsNotNull(verticalTextAlignment); Assert.AreEqual(VerticalPositionValues.Subscript, verticalTextAlignment.Val.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public void ParagraphDecimalLineHeight() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<div style='line-height:1.5'>test</div>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph paragraph = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(paragraph); Assert.AreEqual(2, paragraph.ChildElements.Count); ParagraphProperties properties = paragraph.ChildElements[0] as ParagraphProperties; Assert.IsNotNull(properties); Assert.AreEqual(1, properties.ChildElements.Count); SpacingBetweenLines space = properties.ChildElements[0] as SpacingBetweenLines; Assert.IsNotNull(space); Assert.AreEqual("160", space.Line.Value); Run run = paragraph.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); Word.Text text = run.ChildElements[0] as Word.Text; Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); errors.PrintValidationErrors(); Assert.AreEqual(0, errors.Count()); }
public void BInsideNoFontWeightDiv() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<div style=\"font-weight:normal\"><b>test</b></div>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph para = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties properties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(properties); Assert.AreEqual(1, properties.ChildElements.Count); Bold bold = properties.ChildElements[0] as Bold; Assert.IsNotNull(bold); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public void DivInsideItalic() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<i><div>test</div></i>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph para = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties properties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(properties); Assert.AreEqual(1, properties.ChildElements.Count); Italic italic = properties.ChildElements[0] as Italic; Assert.IsNotNull(italic); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public void IInsideNoTextDecorationDiv() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<div style=\"text-decoration:none\"><u>test</u></div>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph para = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties properties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(properties); Assert.AreEqual(1, properties.ChildElements.Count); Underline underline = properties.ChildElements[0] as Underline; Assert.IsNotNull(underline); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public void AddParagraph() { using (var doc = WordprocessingDocument.Open("Templates\\CKEmptyTemplate.docx", true, new OpenSettings { MarkupCompatibilityProcessSettings = new MarkupCompatibilityProcessSettings(MarkupCompatibilityProcessMode.ProcessAllParts, FileFormatVersions.Office2010) })) { var text = new Text("Perec"); var run = new Run(); run.RunProperties = new RunProperties(); run.RunProperties.RunStyle = new RunStyle { Val = "TitleChar" }; run.Append(text); var paragraph = new Paragraph(run); //paragraph.ParagraphProperties = new ParagraphProperties {ParagraphStyleId = new ParagraphStyleId { Val = "Title" }}; var text2 = new Text("Alma"); var run2 = new Run(); run2.Append(text2); run2.RunProperties = new RunProperties(); run2.RunProperties.RunStyle = new RunStyle { Val = "Normal" }; paragraph.Append(run2); doc.MainDocumentPart.Document.Body.AppendChild<Paragraph>(paragraph); doc.MainDocumentPart.Document.Save(); } }
private static void GenerateFooterPartContent(FooterPart part) { var footer1 = new Footer() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } }; var paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" }; var paragraphProperties1 = new ParagraphProperties(); var paragraphStyleId1 = new ParagraphStyleId() { Val = "Footer" }; paragraphProperties1.Append(paragraphStyleId1); var run1 = new Run(); var text1 = new Text { Text = "Footer" }; run1.Append(text1); paragraph1.Append(paragraphProperties1); paragraph1.Append(run1); footer1.Append(paragraph1); part.Footer = footer1; }
public void TestAlignJustify() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<p style='text-align:justify;'>test</p>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph paragraph = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(paragraph); Assert.AreEqual(2, paragraph.ChildElements.Count); ParagraphProperties properties = paragraph.ChildElements[0] as ParagraphProperties; Assert.IsNotNull(properties); Justification align = properties.ChildElements[0] as Justification; Assert.IsNotNull(align); Assert.AreEqual(JustificationValues.Both, align.Val.Value); Run run = paragraph.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); Word.Text text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); errors.PrintValidationErrors(); Assert.AreEqual(0, errors.Count()); } }
public void DivMultipleFontFamily() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<div style='font-family:Arial, Georgia, Serif'>test</div>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); OpenXmlElement para = doc.Document.Body.ChildElements[0]; Assert.IsTrue(para is Paragraph); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); Assert.IsNotNull(run.RunProperties); Assert.AreEqual(1, run.RunProperties.ChildElements.Count); RunFonts fonts = run.RunProperties.ChildElements[0] as RunFonts; Assert.IsNotNull(fonts); Assert.AreEqual("Arial,Georgia,Serif", fonts.Ascii.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public void DivTextDecorationLine() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<div style='text-decoration-line:underline'>test</div>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); OpenXmlElement para = doc.Document.Body.ChildElements[0]; Assert.IsTrue(para is Paragraph); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); Assert.IsNotNull(run.RunProperties); Assert.AreEqual(1, run.RunProperties.ChildElements.Count); Underline underline = run.RunProperties.ChildElements[0] as Underline; Assert.IsNotNull(underline); Assert.AreEqual(UnderlineValues.Single, underline.Val.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public void H1WithoutBold() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<h1 style=\"font-size:15px;font-weight:normal\">test</h1>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph paragraph = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(paragraph); Run run = paragraph.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties properties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(properties); Assert.AreEqual(1, properties.ChildElements.Count); FontSize fontSize = properties.ChildElements[0] as FontSize; Assert.IsNotNull(fontSize); Assert.AreEqual("30", fontSize.Val.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
public void StrongTagWithSpace() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<strong>Name & SSN </string>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); OpenXmlElement para = doc.Document.Body.ChildElements[0]; Assert.IsTrue(para is Paragraph); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); Assert.IsNotNull(run.RunProperties); Assert.AreEqual(1, run.RunProperties.ChildElements.Count); Bold bold = run.RunProperties.ChildElements[0] as Bold; Assert.IsNotNull(bold); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("Name & SSN ", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public void SinglePBackGround() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<p style='background-color:#000'>test</p>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph paragraph = doc.Document.Body.ChildElements[0] as Paragraph; Assert.AreEqual(2, paragraph.ChildElements.Count); Assert.IsNotNull(paragraph.ParagraphProperties); Assert.IsNotNull(paragraph.ParagraphProperties.Shading); Assert.AreEqual("000000", paragraph.ParagraphProperties.Shading.Fill.Value); Assert.AreEqual(Word.ShadingPatternValues.Clear, paragraph.ParagraphProperties.Shading.Val.Value); Run run = paragraph.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties runProperties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(runProperties); Assert.AreEqual("000000", runProperties.Shading.Fill.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
public void DivFontBold() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<div style='font-weight:bold'>test</div>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); OpenXmlElement para = doc.Document.Body.ChildElements[0]; Assert.IsTrue(para is Paragraph); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); Assert.IsNotNull(run.RunProperties); Assert.AreEqual(1, run.RunProperties.ChildElements.Count); Bold bold = run.RunProperties.ChildElements[0] as Bold; Assert.IsNotNull(bold); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public void SpanWithLeadingSpaceAndSpan() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<span>one</span> <span> two</span>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph para = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(2, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); Word.Text text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("one", text.InnerText); run = para.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(" two", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
public void SinglePAllBorder() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<p style='border:1px solid #000'>test</p>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph paragraph = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(paragraph); Assert.AreEqual(2, paragraph.ChildElements.Count); ParagraphProperties paragraphProperties = paragraph.ChildElements[0] as ParagraphProperties; ParagraphBorders paragraphBorders = paragraphProperties.ChildElements[0] as ParagraphBorders; Assert.IsNotNull(paragraphBorders); Assert.AreEqual(4, paragraphBorders.ChildElements.Count); TopBorder topBorder = paragraphBorders.ChildElements[0] as TopBorder; Assert.IsNotNull(topBorder); Assert.AreEqual(BorderValues.Single, topBorder.Val.Value); Assert.AreEqual("000000", topBorder.Color.Value); Assert.AreEqual(1, topBorder.Size.Value); LeftBorder leftBorder = paragraphBorders.ChildElements[1] as LeftBorder; Assert.IsNotNull(leftBorder); Assert.AreEqual(BorderValues.Single, leftBorder.Val.Value); Assert.AreEqual("000000", leftBorder.Color.Value); Assert.AreEqual(1, leftBorder.Size.Value); BottomBorder bottomBorder = paragraphBorders.ChildElements[2] as BottomBorder; Assert.IsNotNull(bottomBorder); Assert.AreEqual(BorderValues.Single, bottomBorder.Val.Value); Assert.AreEqual("000000", bottomBorder.Color.Value); Assert.AreEqual(1, bottomBorder.Size.Value); RightBorder rightBorder = paragraphBorders.ChildElements[3] as RightBorder; Assert.IsNotNull(rightBorder); Assert.AreEqual(BorderValues.Single, rightBorder.Val.Value); Assert.AreEqual("000000", rightBorder.Color.Value); Assert.AreEqual(1, rightBorder.Size.Value); Run run = paragraph.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); Word.Text text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public void TestAllRunProperties() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<p><span style='font-family:arial;font-weight:bold;text-decoration:underline;font-size:12px;font-style:italic;background-color:#ccc;color:#000'>test</span></p>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph paragraph = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(paragraph); Assert.AreEqual(1, paragraph.ChildElements.Count); Run run = paragraph.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties properties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(properties); RunFonts fonts = properties.ChildElements[0] as RunFonts; Assert.IsNotNull(fonts); Assert.AreEqual("arial", fonts.Ascii.Value); Bold bold = properties.ChildElements[1] as Bold; Assert.IsNotNull(bold); Italic italic = properties.ChildElements[2] as Italic; Assert.IsNotNull(italic); Word.Color color = properties.ChildElements[3] as Word.Color; Assert.IsNotNull(color); Assert.AreEqual("000000", color.Val.Value); FontSize fontSize = properties.ChildElements[4] as FontSize; Assert.IsNotNull(fontSize); Assert.AreEqual("24", fontSize.Val.Value); Underline underline = properties.ChildElements[5] as Underline; Assert.IsNotNull(underline); Word.Shading shading = properties.ChildElements[6] as Word.Shading; Assert.IsNotNull(shading); Assert.AreEqual("cccccc", shading.Fill.Value); Assert.AreEqual(Word.ShadingPatternValues.Clear, shading.Val.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); errors.PrintValidationErrors(); Assert.AreEqual(0, errors.Count()); }
public MemoryStream ReplaceImages() { if (_rep.ImagePlaceholders.Count == 0 || _rep.ImagePlaceholders == null) { return(null); } using (WordprocessingDocument doc = WordprocessingDocument.Open(_docxMs, true)) { CleanMarkup(doc); var document = doc.MainDocumentPart.Document; foreach (var text in document.Descendants <Text>()) // <<< Here { foreach (var replace in _rep.ImagePlaceholders) { string pl = _rep.ImagePlaceholderStartTag + replace.Key + _rep.ImagePlaceholderEndTag; _imageCounter++; if (text.Text.Contains(pl)) { var run = text.Ancestors <Run>().First(); var newRunForImage = new Run(); //Break the texts into the part before and after image. Then create separate runs for them var pos = text.Text.IndexOf(pl, StringComparison.CurrentCulture); if (text.Text.Length > pl.Length) { if (pos == 0) { var newAfterRun = (Run)run.Clone(); string afterText = text.Text.Substring(pl.Length, text.Text.Length - pl.Length); Text newAfterRunText = newAfterRun.GetFirstChild <Text>(); newAfterRunText.Space = SpaceProcessingModeValues.Preserve; newAfterRunText.Text = afterText; run.Parent.InsertAfter(newAfterRun, run); } else if (text.Text.EndsWith(pl)) { var newBeforeRun = (Run)run.Clone(); string beforeText = text.Text.Substring(0, pos); Text newBeforeRunText = newBeforeRun.GetFirstChild <Text>(); newBeforeRunText.Space = SpaceProcessingModeValues.Preserve; newBeforeRunText.Text = beforeText; run.Parent.InsertBefore(newBeforeRun, run); } else { var newBeforeRun = (Run)run.Clone(); string beforeText = text.Text.Substring(0, pos); Text newBeforeRunText = newBeforeRun.GetFirstChild <Text>(); newBeforeRunText.Space = SpaceProcessingModeValues.Preserve; newBeforeRunText.Text = beforeText; run.Parent.InsertBefore(newBeforeRun, run); var newAfterRun = (Run)run.Clone(); string afterText = text.Text.Substring(pos + pl.Length, text.Text.Length - pos - pl.Length); Text newAfterRunText = newAfterRun.GetFirstChild <Text>(); newAfterRunText.Space = SpaceProcessingModeValues.Preserve; newAfterRunText.Text = afterText; run.Parent.InsertAfter(newAfterRun, run); } } run.Parent.InsertBefore(newRunForImage, run); run.Remove(); AppendImageToElement(replace, newRunForImage, doc); } } } } _docxMs.Position = 0; return(_docxMs); }
public MemoryStream ReplaceTableRows() { if (_rep.TablePlaceholders.Count == 0 || _rep.TablePlaceholders == null) { return(null); } using (WordprocessingDocument doc = WordprocessingDocument.Open(_docxMs, true)) { CleanMarkup(doc); var document = doc.MainDocumentPart.Document; foreach (var trDict in _rep.TablePlaceholders) //Take a Row (one Dictionary) at a time { var trCol0 = trDict.First(); // Find the first text element matching the search string // where the text is inside a table cell --> this is the row we are searching for. var textElement = document.Body.Descendants <Text>() .FirstOrDefault(t => t.Text == _rep.TablePlaceholderStartTag + trCol0.Key + _rep.TablePlaceholderEndTag && t.Ancestors <DocumentFormat.OpenXml.Wordprocessing.TableCell>().Any()); if (textElement != null) { var newTableRows = new List <TableRow>(); var tableRow = textElement.Ancestors <TableRow>().First(); for (var j = 0; j < trCol0.Value.Length; j++) //Lets create row by row and replace placeholders { newTableRows.Add((TableRow)tableRow.CloneNode(true)); var tableRowCopy = newTableRows[newTableRows.Count - 1]; foreach (var text in tableRow.Descendants <Text>() ) //Cycle through the cells of the row to replace from the Dictionary value ( string array) { for (var index = 0; index < trDict.Count; index++) //Now cycle through the "columns" (keys) of the Dictionary and replace item by item { var item = trDict.ElementAt(index); if (text.Text.Contains(_rep.TablePlaceholderStartTag + item.Key + _rep.TablePlaceholderEndTag)) { if (item.Value[j].Contains(_rep.NewLineTag)) //If we have line breaks present { string[] repArray = item.Value[j].Split(new string[] { _rep.NewLineTag }, StringSplitOptions.None); var lastInsertedText = text; var lastInsertedBreak = new Break(); for (var i = 0; i < repArray.Length; i++) { if (i == 0 ) //The text is only replaced with the first part of the replacement array { text.Text = text.Text.Replace( _rep.TablePlaceholderStartTag + item.Key + _rep.TablePlaceholderEndTag, repArray[i]); } else { var tmpText = new Text(repArray[i]); var tmpBreak = new Break(); text.Parent.InsertAfter(tmpBreak, lastInsertedText); lastInsertedBreak = tmpBreak; text.Parent.InsertAfter(tmpText, lastInsertedBreak); lastInsertedText = tmpText; } } } else { text.Text = text.Text.Replace( _rep.TablePlaceholderStartTag + item.Key + _rep.TablePlaceholderEndTag, item.Value[j]); } break; } } } if (j < trCol0.Value.Length - 1) { tableRow.Parent.InsertAfter(tableRowCopy, tableRow); tableRow = tableRowCopy; } } } } } _docxMs.Position = 0; return(_docxMs); }
public void AllParagraphProperties() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<p style='text-align:center;margin:5px;background-color:#ccc;border:1px solid #000'>test</p>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph paragraph = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(paragraph); Assert.AreEqual(2, paragraph.ChildElements.Count); ParagraphProperties properties = paragraph.ChildElements[0] as ParagraphProperties; Assert.IsNotNull(properties); ParagraphBorders paragraphBorders = properties.ChildElements[0] as ParagraphBorders; Assert.IsNotNull(paragraphBorders); Assert.AreEqual(4, paragraphBorders.ChildElements.Count); TopBorder topBorder = paragraphBorders.ChildElements[0] as TopBorder; Assert.IsNotNull(topBorder); Assert.AreEqual(BorderValues.Single, topBorder.Val.Value); Assert.AreEqual("000000", topBorder.Color.Value); Assert.AreEqual(1, topBorder.Size.Value); LeftBorder leftBorder = paragraphBorders.ChildElements[1] as LeftBorder; Assert.IsNotNull(leftBorder); Assert.AreEqual(BorderValues.Single, leftBorder.Val.Value); Assert.AreEqual("000000", leftBorder.Color.Value); Assert.AreEqual(1, leftBorder.Size.Value); BottomBorder bottomBorder = paragraphBorders.ChildElements[2] as BottomBorder; Assert.IsNotNull(bottomBorder); Assert.AreEqual(BorderValues.Single, bottomBorder.Val.Value); Assert.AreEqual("000000", bottomBorder.Color.Value); Assert.AreEqual(1, bottomBorder.Size.Value); RightBorder rightBorder = paragraphBorders.ChildElements[3] as RightBorder; Assert.IsNotNull(rightBorder); Assert.AreEqual(BorderValues.Single, rightBorder.Val.Value); Assert.AreEqual("000000", rightBorder.Color.Value); Assert.AreEqual(1, rightBorder.Size.Value); Assert.IsNotNull(paragraph.ParagraphProperties.Shading); Assert.AreEqual("cccccc", paragraph.ParagraphProperties.Shading.Fill.Value); Assert.AreEqual(Word.ShadingPatternValues.Clear, paragraph.ParagraphProperties.Shading.Val.Value); SpacingBetweenLines spacing = properties.ChildElements[2] as SpacingBetweenLines; Assert.IsNotNull(spacing); Assert.AreEqual("100", spacing.Before.Value); Indentation ind = properties.ChildElements[3] as Indentation; Assert.IsNotNull(ind); Assert.AreEqual("100", ind.Left.Value); Assert.IsNotNull(ind.Right); Assert.AreEqual("100", ind.Right.Value); Justification align = properties.ChildElements[4] as Justification; Assert.IsNotNull(align); Assert.AreEqual(JustificationValues.Center, align.Val.Value); Run run = paragraph.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties runProperties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(runProperties); Assert.AreEqual("cccccc", runProperties.Shading.Fill.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); errors.PrintValidationErrors(); Assert.AreEqual(0, errors.Count()); } }
/// <summary> /// The create paragraph. /// </summary> /// <param name="content"> /// The content. /// </param> /// <param name="styleID"> /// The style id. /// </param> /// <returns> /// </returns> private static DocumentFormat.OpenXml.Wordprocessing.Paragraph CreateParagraph( string content, string styleID = null) { var p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(); if (styleID != null) { var pp = new ParagraphProperties { ParagraphStyleId = new ParagraphStyleId { Val = styleID } }; p.Append(pp); } var text = new Text(content); var run = new Run(text); p.Append(run); return p; }
public void AddTextToWord(WordprocessingDocument doc, string text, bool bold, bool italic, bool underline, int heading, int slideNumber) { MainDocumentPart mainPart = doc.MainDocumentPart; WordDoc.Body body = mainPart.Document.Body; WordDoc.Paragraph para = new WordDoc.Paragraph(); foreach (WordDoc.Paragraph p in body.Elements<WordDoc.Paragraph>()) { para = p; } if (heading != 0) { para = body.AppendChild(new WordDoc.Paragraph()); para.ParagraphProperties = new WordDoc.ParagraphProperties(new WordDoc.ParagraphStyleId() { Val = "Heading" + heading.ToString() }); bold = false; italic = false; underline = false; WordDoc.BookmarkStart bs = new WordDoc.BookmarkStart(); bs.Id = slideNumber.ToString(); bs.Name = text; bs = para.AppendChild(bs); } else { if (para.ParagraphProperties != null) { if (para.ParagraphProperties.ParagraphStyleId != null) { if (para.ParagraphProperties.ParagraphStyleId.Val == "Heading1" || para.ParagraphProperties.ParagraphStyleId.Val == "Heading2" || para.ParagraphProperties.ParagraphStyleId.Val == "Heading3") { para = body.AppendChild(new WordDoc.Paragraph()); } } } } string[] t1 = text.Split('\n'); for (int j = 0; j < t1.Length; j++) { WordDoc.Run run = para.AppendChild(new WordDoc.Run()); WordDoc.RunProperties rp = run.AppendChild(new WordDoc.RunProperties()); if (bold) { WordDoc.Bold b = rp.AppendChild(new WordDoc.Bold()); } if (italic) { WordDoc.Italic i = rp.AppendChild(new WordDoc.Italic()); } if (underline) { WordDoc.Underline u = new WordDoc.Underline() { Val = WordDoc.UnderlineValues.Single }; rp.AppendChild(u); } WordDoc.Text t = new WordDoc.Text() { Text = t1[j], Space = SpaceProcessingModeValues.Preserve }; run.AppendChild(t); if (j != t1.Length - 1) run.AppendChild(new WordDoc.Break()); } WordDoc.BookmarkEnd be = new WordDoc.BookmarkEnd(); be.Id = slideNumber.ToString(); be = para.AppendChild(be); }
private static bool Render(TableElement element, WordprocessingDocument document, Body body) { if (element.Table.Rows.Count == 0) { return(true); } var table = new DocumentFormat.OpenXml.Wordprocessing.Table(); if (element.Headers.Count != 0) { var row = new DocumentFormat.OpenXml.Wordprocessing.TableRow(); foreach (var head in element.Headers) { var text = new DocumentFormat.OpenXml.Wordprocessing.Text(head.ToString()); var run = new DocumentFormat.OpenXml.Wordprocessing.Run(text); var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(run); paragraph.ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties { ParagraphStyleId = new DocumentFormat.OpenXml.Wordprocessing.ParagraphStyleId() { Val = WordStyleIdFromStyleName(document, element.HeaderStyle.Name) }, WordWrap = new DocumentFormat.OpenXml.Wordprocessing.WordWrap { Val = new OnOffValue { Value = true } }, TextAlignment = new DocumentFormat.OpenXml.Wordprocessing.TextAlignment { Val = DocumentFormat.OpenXml.Wordprocessing.VerticalTextAlignmentValues.Auto }, }; var cell = new DocumentFormat.OpenXml.Wordprocessing.TableCell(paragraph); DocumentFormat.OpenXml.Wordprocessing.TableCellProperties cellprop = WordCellProperties(element.HeaderStyle); cell.Append(cellprop); row.Append(cell); } table.Append(row); } for (int i = 0; i < element.Table.Rows.Count; i++) { var row = new DocumentFormat.OpenXml.Wordprocessing.TableRow(); for (int j = 0; j < element.Table.Columns.Count; j++) { var text = new DocumentFormat.OpenXml.Wordprocessing.Text(element.Table.Rows[i][j].ToString()); var run = new DocumentFormat.OpenXml.Wordprocessing.Run(text); var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(run); paragraph.ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties { ParagraphStyleId = new DocumentFormat.OpenXml.Wordprocessing.ParagraphStyleId() { Val = WordStyleIdFromStyleName(document, element.TableStyle.Name) }, WordWrap = new DocumentFormat.OpenXml.Wordprocessing.WordWrap { Val = new OnOffValue { Value = true } }, TextAlignment = new DocumentFormat.OpenXml.Wordprocessing.TextAlignment { Val = DocumentFormat.OpenXml.Wordprocessing.VerticalTextAlignmentValues.Auto }, }; var cell = new DocumentFormat.OpenXml.Wordprocessing.TableCell(paragraph); var cellprop = WordCellProperties(element.TableStyle); cell.Append(cellprop); row.Append(cell); } table.Append(row); } body.AppendChild <DocumentFormat.OpenXml.Wordprocessing.Table>(table); return(false); }
public void TableCellStyleInheritance() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<table><tr><td style='border:1px solid #000;background-color:red'>test</td></tr></table>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Word.Table table = doc.Document.Body.ChildElements[0] as Word.Table; Assert.IsNotNull(table); Assert.AreEqual(3, table.ChildElements.Count); TableRow row = table.ChildElements[2] as TableRow; Assert.IsNotNull(row); Assert.AreEqual(1, row.ChildElements.Count); TableCell cell = row.ChildElements[0] as TableCell; Assert.IsNotNull(cell); Assert.AreEqual(2, cell.ChildElements.Count); TableCellProperties cellProperties = cell.ChildElements[0] as TableCellProperties; Assert.IsNotNull(cellProperties); Assert.AreEqual(2, cellProperties.ChildElements.Count); TableCellBorders borders = cellProperties.ChildElements[0] as TableCellBorders; Assert.IsNotNull(borders); Assert.AreEqual(4, borders.ChildElements.Count); TopBorder topBorder = borders.ChildElements[0] as TopBorder; Assert.IsNotNull(topBorder); TestUtility.TestBorder <TopBorder>(topBorder, BorderValues.Single, "000000", 1U); LeftBorder leftBorder = borders.ChildElements[1] as LeftBorder; Assert.IsNotNull(leftBorder); TestUtility.TestBorder <LeftBorder>(leftBorder, BorderValues.Single, "000000", 1U); BottomBorder bottomBorder = borders.ChildElements[2] as BottomBorder; Assert.IsNotNull(bottomBorder); TestUtility.TestBorder <BottomBorder>(bottomBorder, BorderValues.Single, "000000", 1U); RightBorder rightBorder = borders.ChildElements[3] as RightBorder; Assert.IsNotNull(rightBorder); TestUtility.TestBorder <RightBorder>(rightBorder, BorderValues.Single, "000000", 1U); Word.Shading backgroundColor = cellProperties.ChildElements[1] as Word.Shading; Assert.IsNotNull(backgroundColor); Assert.AreEqual("FF0000", backgroundColor.Fill.Value); Paragraph para = cell.ChildElements[1] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties runProperties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(runProperties); Word.Shading shading = runProperties.ChildElements[0] as Word.Shading; Assert.AreEqual("FF0000", shading.Fill.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
public void TableCellWidth() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<table style='width:500px'><tr><td style='width:250px'>1</td><td style='width:250px'>2</td></tr></table>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Word.Table table = doc.Document.Body.ChildElements[0] as Word.Table; Assert.IsNotNull(table); Assert.AreEqual(3, table.ChildElements.Count); TableProperties tableProperties = table.ChildElements[0] as TableProperties; Assert.IsNotNull(tableProperties); Assert.AreEqual(2, tableProperties.ChildElements.Count); TableStyle tableStyle = tableProperties.ChildElements[0] as TableStyle; Assert.IsNotNull(tableStyle); Assert.AreEqual("TableGrid", tableStyle.Val.Value); TableWidth tableWidth = tableProperties.ChildElements[1] as TableWidth; Assert.IsNotNull(tableWidth); Assert.AreEqual("10000", tableWidth.Width.Value); Assert.AreEqual(TableWidthUnitValues.Dxa, tableWidth.Type.Value); TableRow row = table.ChildElements[2] as TableRow; Assert.IsNotNull(row); Assert.AreEqual(2, row.ChildElements.Count); TableCell cell = row.ChildElements[0] as TableCell; Assert.IsNotNull(cell); Assert.AreEqual(2, cell.ChildElements.Count); TableCellProperties cellProperties = cell.ChildElements[0] as TableCellProperties; Assert.IsNotNull(cellProperties); TableCellWidth cellWidth = cellProperties.ChildElements[0] as TableCellWidth; Assert.IsNotNull(cellWidth); Assert.AreEqual("5000", cellWidth.Width.Value); Assert.AreEqual(TableWidthUnitValues.Dxa, cellWidth.Type.Value); Paragraph para = cell.ChildElements[1] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); Word.Text text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("1", text.InnerText); cell = row.ChildElements[1] as TableCell; Assert.IsNotNull(cell); Assert.AreEqual(2, cell.ChildElements.Count); cellProperties = cell.ChildElements[0] as TableCellProperties; Assert.IsNotNull(cellProperties); cellWidth = cellProperties.ChildElements[0] as TableCellWidth; Assert.IsNotNull(cellWidth); Assert.AreEqual("5000", cellWidth.Width.Value); Assert.AreEqual(TableWidthUnitValues.Dxa, cellWidth.Type.Value); para = cell.ChildElements[1] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("2", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
public void TableThCellStyles() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<table><tr><th>Id</th></tr><tr><td>1</td></tr></table>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Word.Table table = doc.Document.Body.ChildElements[0] as Word.Table; Assert.IsNotNull(table); Assert.AreEqual(4, table.ChildElements.Count); TableProperties tableProperties = table.ChildElements[0] as TableProperties; Assert.IsNotNull(tableProperties); TableStyle tableStyle = tableProperties.ChildElements[0] as TableStyle; Assert.IsNotNull(tableStyle); Assert.AreEqual("TableGrid", tableStyle.Val.Value); TableGrid tableGrid = table.ChildElements[1] as TableGrid; Assert.IsNotNull(tableGrid); Assert.AreEqual(1, tableGrid.ChildElements.Count); TableRow row = table.ChildElements[2] as TableRow; Assert.IsNotNull(row); Assert.AreEqual(1, row.ChildElements.Count); TableCell cell = row.ChildElements[0] as TableCell; Assert.IsNotNull(cell); Assert.AreEqual(1, cell.ChildElements.Count); Paragraph para = cell.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); Assert.IsNotNull(run.RunProperties); Bold bold = run.RunProperties.ChildElements[0] as Bold; Assert.IsNotNull(bold); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("Id", text.InnerText); row = table.ChildElements[3] as TableRow; Assert.IsNotNull(row); Assert.AreEqual(1, row.ChildElements.Count); cell = row.ChildElements[0] as TableCell; cell.TestTableCell(1, "1"); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
private Wordprocessing.Paragraph retText(int r, int c) { string value=""; if (r == 0 && c == 0) value = "Подпись преподавателя"; if (r == 1 && c == 1) value = "1.Факультет составляет ведомость в 1 экз."; if (r == 2 && c == 1) value = "2.Экзаменатор лично получает ведомость в деканате."; if (r == 3 && c == 1) value = "3.По окончании экзамена экзаменатор лично сдает"; if (r == 4 && c == 1) value = "ведомость в деканат."; if (r == 1 && c == 2) value = "ИТОГО"; if (r == 1 && c == 3) value = "отлично"; if (r == 2 && c == 3) value = "хорошо"; if (r == 3 && c == 3) value = "удовлетворительно"; if (r == 4 && c == 3) value = "плохо"; if (r == 5 && c == 3) value = "не явился"; Wordprocessing.Run run = new Wordprocessing.Run(); Wordprocessing.Text text = new Wordprocessing.Text(value); run.Append(text); Wordprocessing.Paragraph par = new Wordprocessing.Paragraph(); Wordprocessing.ParagraphProperties parprop = new Wordprocessing.ParagraphProperties(); Wordprocessing.SpacingBetweenLines spacingBetweenLines = new Wordprocessing.SpacingBetweenLines() { After = "0" }; parprop.Append(spacingBetweenLines); par.Append(parprop); par.Append(run); return par; }
public void TestInnerThreeBetweenDiv() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<ul><li>One</li><li><ul><li>Two</li></ul></li><li><div>Three</div></li></ul>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(3, doc.Document.Body.ChildElements.Count); Paragraph para = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(2, para.ChildElements.Count); ParagraphProperties properties = para.ChildElements[0] as ParagraphProperties; Assert.IsNotNull(properties); Assert.AreEqual(2, properties.ChildElements.Count); ParagraphStyleId paragraphStyleId = properties.ChildElements[0] as ParagraphStyleId; Assert.IsNotNull(paragraphStyleId); Assert.AreEqual("ListParagraph", paragraphStyleId.Val.Value); NumberingProperties numberingProperties = properties.ChildElements[1] as NumberingProperties; Assert.IsNotNull(numberingProperties); Assert.AreEqual(2, numberingProperties.ChildElements.Count); NumberingLevelReference numberingLevelReference = numberingProperties.ChildElements[0] as NumberingLevelReference; Assert.IsNotNull(numberingLevelReference); Assert.AreEqual(0, numberingLevelReference.Val.Value); NumberingId numberingId = numberingProperties.ChildElements[1] as NumberingId; Assert.IsNotNull(numberingId); Assert.AreEqual(2, numberingId.Val.Value); Run run = para.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); Word.Text text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("One", text.InnerText); para = doc.Document.Body.ChildElements[1] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(2, para.ChildElements.Count); properties = para.ChildElements[0] as ParagraphProperties; Assert.IsNotNull(properties); Assert.AreEqual(2, properties.ChildElements.Count); paragraphStyleId = properties.ChildElements[0] as ParagraphStyleId; Assert.IsNotNull(paragraphStyleId); Assert.AreEqual("ListParagraph", paragraphStyleId.Val.Value); numberingProperties = properties.ChildElements[1] as NumberingProperties; Assert.IsNotNull(numberingProperties); Assert.AreEqual(2, numberingProperties.ChildElements.Count); numberingLevelReference = numberingProperties.ChildElements[0] as NumberingLevelReference; Assert.IsNotNull(numberingLevelReference); Assert.AreEqual(1, numberingLevelReference.Val.Value); numberingId = numberingProperties.ChildElements[1] as NumberingId; Assert.IsNotNull(numberingId); Assert.AreEqual(3, numberingId.Val.Value); run = para.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("Two", text.InnerText); para = doc.Document.Body.ChildElements[2] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(2, para.ChildElements.Count); properties = para.ChildElements[0] as ParagraphProperties; Assert.IsNotNull(properties); Assert.AreEqual(2, properties.ChildElements.Count); paragraphStyleId = properties.ChildElements[0] as ParagraphStyleId; Assert.IsNotNull(paragraphStyleId); Assert.AreEqual("ListParagraph", paragraphStyleId.Val.Value); numberingProperties = properties.ChildElements[1] as NumberingProperties; Assert.IsNotNull(numberingProperties); Assert.AreEqual(2, numberingProperties.ChildElements.Count); numberingLevelReference = numberingProperties.ChildElements[0] as NumberingLevelReference; Assert.IsNotNull(numberingLevelReference); Assert.AreEqual(0, numberingLevelReference.Val.Value); numberingId = numberingProperties.ChildElements[1] as NumberingId; Assert.IsNotNull(numberingId); Assert.AreEqual(2, numberingId.Val.Value); run = para.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("Three", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); }
public Wordprocessing.Body insertinform() { Wordprocessing.Body body = new Wordprocessing.Body(); //Параграф1 Wordprocessing.Paragraph paragraph1 = new Wordprocessing.Paragraph(); Wordprocessing.ParagraphProperties paragraphProperties1 = new Wordprocessing.ParagraphProperties(); Wordprocessing.SpacingBetweenLines spacingBetweenLines1 = new Wordprocessing.SpacingBetweenLines() { After = "0" }; Wordprocessing.Justification justification1 = new Wordprocessing.Justification() { Val = Wordprocessing.JustificationValues.Center }; paragraphProperties1.Append(spacingBetweenLines1); paragraphProperties1.Append(justification1); Wordprocessing.Run run1 = new Wordprocessing.Run(); Wordprocessing.RunProperties runProperties1 = new Wordprocessing.RunProperties(); Wordprocessing.RunFonts runFonts2 = new Wordprocessing.RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" }; Wordprocessing.Bold bold2 = new Wordprocessing.Bold(); Wordprocessing.FontSize fontSize2 = new Wordprocessing.FontSize() { Val = "24" }; runProperties1.Append(runFonts2); runProperties1.Append(bold2); runProperties1.Append(fontSize2); Wordprocessing.Text text1 = new Wordprocessing.Text(); text1.Text = "ФГБОУВПО \"ПЕРМСКИЙ ГОСУДАРСТВЕННЫЙ НАЦИОНАЛЬНЫЙ ИССЛЕДОВАТЕЛЬСКИЙ УНИВЕРСИТЕТ\""; run1.Append(runProperties1); run1.Append(text1); paragraph1.Append(paragraphProperties1); paragraph1.Append(run1); //Параграф2 Wordprocessing.Paragraph paragraph2 = new Wordprocessing.Paragraph(); Wordprocessing.ParagraphProperties paragraphProperties2 = new Wordprocessing.ParagraphProperties(); Wordprocessing.SpacingBetweenLines spacingBetweenLines2 = new Wordprocessing.SpacingBetweenLines() { After = "0" }; Wordprocessing.Justification justification2 = new Wordprocessing.Justification() { Val = Wordprocessing.JustificationValues.Center }; paragraphProperties2.Append(spacingBetweenLines2); paragraphProperties2.Append(justification2); Wordprocessing.Run run2 = new Wordprocessing.Run(); Wordprocessing.RunProperties runProperties2 = new Wordprocessing.RunProperties(); Wordprocessing.RunFonts runFonts4 = new Wordprocessing.RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" }; Wordprocessing.FontSize fontSize4 = new Wordprocessing.FontSize() { Val = "24" }; runProperties2.Append(runFonts4); runProperties2.Append(fontSize4); Wordprocessing.Text text2 = new Wordprocessing.Text(); text2.Text = "Механико-математический факультет "; run2.Append(runProperties2); run2.Append(text2); Wordprocessing.Run run3 = new Wordprocessing.Run(); Wordprocessing.RunProperties runProperties3 = new Wordprocessing.RunProperties(); Wordprocessing.RunFonts runFonts5 = new Wordprocessing.RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" }; Wordprocessing.FontSize fontSize5 = new Wordprocessing.FontSize() { Val = "24" }; runProperties3.Append(runFonts5); runProperties3.Append(fontSize5); Wordprocessing.Break break1 = new Wordprocessing.Break(); Wordprocessing.Text text3 = new Wordprocessing.Text(); text3.Text = "очная форма обучения"; run3.Append(runProperties3); run3.Append(break1); run3.Append(text3); paragraph2.Append(paragraphProperties2); paragraph2.Append(run2); paragraph2.Append(run3); //Параграф2 Wordprocessing.Paragraph paragraph3 = new Wordprocessing.Paragraph() { RsidParagraphAddition = "004D49E1", RsidParagraphProperties = "004D49E1", RsidRunAdditionDefault = "004D49E1" }; Wordprocessing.ParagraphProperties paragraphProperties3 = new Wordprocessing.ParagraphProperties(); Wordprocessing.SpacingBetweenLines spacingBetweenLines3 = new Wordprocessing.SpacingBetweenLines() { After = "0" }; Wordprocessing.Justification justification3 = new Wordprocessing.Justification() { Val = Wordprocessing.JustificationValues.Center }; paragraphProperties3.Append(spacingBetweenLines3); paragraphProperties3.Append(justification3); Wordprocessing.Run run4 = new Wordprocessing.Run(); Wordprocessing.RunProperties runProperties4 = new Wordprocessing.RunProperties(); Wordprocessing.RunFonts runFonts7 = new Wordprocessing.RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" }; Wordprocessing.Bold bold4 = new Wordprocessing.Bold(); runProperties4.Append(runFonts7); runProperties4.Append(bold4); Wordprocessing.Text text4 = new Wordprocessing.Text(); text4.Text = "ЭКЗАМЕНАЦИОННАЯ ВЕДОМОСТЬ"; run4.Append(runProperties4); run4.Append(text4); Wordprocessing.Run run5 = new Wordprocessing.Run(); Wordprocessing.Break break2 = new Wordprocessing.Break(); run5.Append(break2); paragraph3.Append(paragraphProperties3); paragraph3.Append(run4); paragraph3.Append(run5); body.Append(paragraph1); body.Append(paragraph2); body.Append(paragraph3); return body; }
private Wordprocessing.Table doptable(Wordprocessing.Table table) { //Строка подпись декана Wordprocessing.TableRow tableRow2 = new Wordprocessing.TableRow() { RsidTableRowAddition = "00FF67BF", RsidTableRowProperties = "008E2483" }; Wordprocessing.TableCell tableCell = new Wordprocessing.TableCell(); Wordprocessing.TableCellProperties tableCellProperties = new Wordprocessing.TableCellProperties(); Wordprocessing.TableCellWidth tableCellWidth = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto }; Wordprocessing.GridSpan gridSpan = new Wordprocessing.GridSpan() { Val = 6 }; Wordprocessing.TableCellVerticalAlignment tableCellVerticalAlignment = new Wordprocessing.TableCellVerticalAlignment() { Val = Wordprocessing.TableVerticalAlignmentValues.Center }; tableCellProperties.Append(tableCellWidth); tableCellProperties.Append(gridSpan); tableCellProperties.Append(tableCellVerticalAlignment); Wordprocessing.Paragraph paragraph = new Wordprocessing.Paragraph(); Wordprocessing.ParagraphProperties parprop = new Wordprocessing.ParagraphProperties(); Wordprocessing.SpacingBetweenLines spacingBetweenLines2 = new Wordprocessing.SpacingBetweenLines() { After = "0" }; parprop.Append(spacingBetweenLines2); paragraph.Append(parprop); Wordprocessing.Run run = new Wordprocessing.Run(); Wordprocessing.Text text = new Wordprocessing.Text(); text.Text = "Подпись декана"; run.Append(text); paragraph.Append(run); tableCell.Append(tableCellProperties); tableCell.Append(paragraph); tableRow2.Append(tableCell); table.Append(tableRow2); return table; }
private void AddText(OXmlTextElement element) { //if (_paragraph == null) // AddParagraph(); AddRun(); OW.Text text = new OW.Text(element.Text); if (element.PreserveSpace) text.Space = SpaceProcessingModeValues.Preserve; _run.AppendChild(text); }
private void RemoveFields(string fname) { using (WordprocessingDocument pkgDoc = WordprocessingDocument.Open(fname, true)) { string fieldList = string.Empty; Document doc = pkgDoc.MainDocumentPart.Document; //Get all field code elements in the document IEnumerable <FieldChar> fldChars = doc.Descendants <FieldChar>(); if (fldChars == null) { return; //No field codes in the document } // bool fldStart = false; FieldChar fldCharStart = null; FieldChar fldCharEnd = null; FieldChar fldCharSep = null; FieldCode fldCode = null; string fldContent = String.Empty; foreach (FieldChar fldChar in fldChars) { string fldCharPart = fldChar.FieldCharType.ToString(); switch (fldCharPart) { case "begin": //start of the field fldCharStart = fldChar; //get the field code, which will be an instrText element // either as sibling or as a child of the parent sibling fldCode = fldCharStart.Parent.Descendants <FieldCode>().FirstOrDefault(); if (fldCode == null) //complex field { fldCode = fldCharStart.Parent.NextSibling <Run>().Descendants <FieldCode>().FirstOrDefault(); } if (fldCode != null && fldCode.InnerText.Contains("MERGEFIELD")) { fldContent = fldCode.InnerText; fieldList += fldContent + "\n"; } break; case "end": // end of the field fldCharEnd = fldChar; break; case "separate": //complex field with text result //we want to put the database content in this text run //yet still remove the field code //If there's no "separate" field char for the current field, //we need to insert it somewhere else fldCharSep = fldChar; break; default: break; } if ((fldCharStart != null) && (fldCharEnd != null)) //start and end field codes have been found { if (fldCharSep != null) { DocumentFormat.OpenXml.Wordprocessing.Text elemText = (DocumentFormat.OpenXml.Wordprocessing.Text)fldCharSep.Parent.NextSibling().Descendants <DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault(); elemText.Text = fldContent; //Delete all the field chars with their runs DeleteFieldChar(fldCharStart); DeleteFieldChar(fldCharEnd); DeleteFieldChar(fldCharSep); fldCode.Remove(); } else { DocumentFormat.OpenXml.Wordprocessing.Text elemText = new DocumentFormat.OpenXml.Wordprocessing.Text(fldContent); fldCode.Parent.Append(elemText); fldCode.Remove(); //Delete all the field chars with their runs DeleteFieldChar(fldCharStart); DeleteFieldChar(fldCharEnd); DeleteFieldChar(fldCharSep); } fldCharStart = null; fldCharEnd = null; fldCharSep = null; fldCode = null; fldContent = string.Empty; } } } }
public void ReplaceString(OpenXmlElement element, string val) { var text = element.Descendants <Word.Text>().FirstOrDefault(); var run = text == null?element.Descendants <Word.Run>().FirstOrDefault() : FindParent <Word.Run>(text); var runp = run.Parent; var paragraph = FindParent <Word.Paragraph>(runp); run.RsidRunProperties = null; run.RemoveAllChildren <Word.Text>(); run.RemoveAllChildren <Word.RunProperties>(); runp.RemoveAllChildren <Word.Run>(); runp.RemoveAllChildren <Word.Break>(); if (paragraph.ParagraphProperties?.ParagraphMarkRunProperties != null) { run.RunProperties = new Word.RunProperties(); foreach (var item in paragraph.ParagraphProperties.ParagraphMarkRunProperties) { run.RunProperties.AppendChild(item.CloneNode(true)); } } if (text == null) { text = new Word.Text(); } else if (text.Parent != null) { text.Remove(); } string[] pagesplit = val.TrimEnd("\r\n".ToCharArray()).Split("\f".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int p = 0; p < pagesplit.Length; p++) { var lineSpit = pagesplit[p].Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); if (lineSpit.Length > 0) { for (int i = 0; i < lineSpit.Length; i++) { if (p == 0 && i == 0) { text.Text = lineSpit[0]; text.Space = SpaceProcessingModeValues.Preserve; run.Append(text); runp.Append(run); continue; } Word.Run r = run.Clone() as Word.Run; r.RemoveAllChildren <Word.Text>(); r.Append(new Word.Text(lineSpit[i]) { Space = SpaceProcessingModeValues.Preserve }); Word.Paragraph pr = (Word.Paragraph)paragraph.Clone(); pr.RemoveAllChildren <Word.Run>(); pr.RemoveAllChildren <Word.Break>(); pr.RemoveAllChildren <Word.SdtBlock>(); pr.RemoveAllChildren <Word.SdtRun>(); pr.Append(r); paragraph.Parent.InsertAfter <Word.Paragraph>(pr, paragraph); paragraph = pr; } } if (p < pagesplit.Length - 1) { var bp = new Word.Break() { Type = Word.BreakValues.Page }; paragraph.AppendChild(bp); } } }
Word.TableRow CreateRow(ArrayList cellText) { Word.TableRow tr = new Word.TableRow(); // Create cells with simple text. foreach (string s in cellText) { Word.TableCell tc = new Word.TableCell(); Word.Paragraph p = new Word.Paragraph(); Word.Run r = new Word.Run(); Word.Text t = new Word.Text(s); r.AppendChild(t); p.AppendChild(r); tc.AppendChild(p); tr.AppendChild(tc); } return tr; }
public void TableBorder() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<table border='1'><tr><td>test</td></tr></table>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Word.Table table = doc.Document.Body.ChildElements[0] as Word.Table; Assert.IsNotNull(table); Assert.AreEqual(3, table.ChildElements.Count); TableProperties tableProperties = table.ChildElements[0] as TableProperties; Assert.IsNotNull(tableProperties); TableStyle tableStyle = tableProperties.ChildElements[0] as TableStyle; Assert.IsNotNull(tableStyle); Assert.AreEqual("TableGrid", tableStyle.Val.Value); TableBorders tableBorders = tableProperties.ChildElements[1] as TableBorders; Assert.IsNotNull(tableBorders); Assert.AreEqual(4, tableBorders.ChildElements.Count); TopBorder topBorder = tableBorders.ChildElements[0] as TopBorder; Assert.IsNotNull(topBorder); TestUtility.TestBorder <TopBorder>(topBorder, BorderValues.Single, "auto", 4U); LeftBorder leftBorder = tableBorders.ChildElements[1] as LeftBorder; Assert.IsNotNull(leftBorder); TestUtility.TestBorder <LeftBorder>(leftBorder, BorderValues.Single, "auto", 4U); BottomBorder bottomBorder = tableBorders.ChildElements[2] as BottomBorder; Assert.IsNotNull(bottomBorder); TestUtility.TestBorder <BottomBorder>(bottomBorder, BorderValues.Single, "auto", 4U); RightBorder rightBorder = tableBorders.ChildElements[3] as RightBorder; Assert.IsNotNull(rightBorder); TestUtility.TestBorder <RightBorder>(rightBorder, BorderValues.Single, "auto", 4U); TableRow row = table.ChildElements[2] as TableRow; Assert.IsNotNull(row); Assert.AreEqual(1, row.ChildElements.Count); TableCell cell = row.ChildElements[0] as TableCell; Assert.IsNotNull(cell); Assert.AreEqual(2, cell.ChildElements.Count); TableCellProperties cellProperties = cell.ChildElements[0] as TableCellProperties; Assert.IsNotNull(cellProperties); Assert.AreEqual(1, cellProperties.ChildElements.Count); TableCellBorders cellBorders = cellProperties.ChildElements[0] as TableCellBorders; Assert.IsNotNull(cellBorders); Assert.AreEqual(4, cellBorders.ChildElements.Count); topBorder = cellBorders.ChildElements[0] as TopBorder; Assert.IsNotNull(topBorder); TestUtility.TestBorder <TopBorder>(topBorder, BorderValues.Single, "auto", 4U); leftBorder = cellBorders.ChildElements[1] as LeftBorder; Assert.IsNotNull(leftBorder); TestUtility.TestBorder <LeftBorder>(leftBorder, BorderValues.Single, "auto", 4U); bottomBorder = cellBorders.ChildElements[2] as BottomBorder; Assert.IsNotNull(bottomBorder); TestUtility.TestBorder <BottomBorder>(bottomBorder, BorderValues.Single, "auto", 4U); rightBorder = cellBorders.ChildElements[3] as RightBorder; Assert.IsNotNull(rightBorder); TestUtility.TestBorder <RightBorder>(rightBorder, BorderValues.Single, "auto", 4U); Paragraph para = cell.ChildElements[1] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); Word.Text text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
public void TableCellPadding() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<table cellpadding='5'><tr><td>test</td></tr></table>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Word.Table table = doc.Document.Body.ChildElements[0] as Word.Table; Assert.IsNotNull(table); Assert.AreEqual(3, table.ChildElements.Count); TableProperties tableProperties = table.ChildElements[0] as TableProperties; Assert.IsNotNull(tableProperties); Assert.IsNotNull(tableProperties.TableCellMarginDefault); Assert.AreEqual(100, tableProperties.TableCellMarginDefault.TableCellLeftMargin.Width.Value); Assert.AreEqual(TableWidthValues.Dxa, tableProperties.TableCellMarginDefault.TableCellLeftMargin.Type.Value); Assert.AreEqual("100", tableProperties.TableCellMarginDefault.TopMargin.Width.Value); Assert.AreEqual(TableWidthUnitValues.Dxa, tableProperties.TableCellMarginDefault.TopMargin.Type.Value); Assert.AreEqual(100, tableProperties.TableCellMarginDefault.TableCellRightMargin.Width.Value); Assert.AreEqual(TableWidthValues.Dxa, tableProperties.TableCellMarginDefault.TableCellRightMargin.Type.Value); Assert.AreEqual("100", tableProperties.TableCellMarginDefault.BottomMargin.Width.Value); Assert.AreEqual(TableWidthUnitValues.Dxa, tableProperties.TableCellMarginDefault.BottomMargin.Type.Value); TableRow row = table.ChildElements[2] as TableRow; Assert.IsNotNull(row); Assert.AreEqual(1, row.ChildElements.Count); TableCell cell = row.ChildElements[0] as TableCell; Assert.IsNotNull(cell); Assert.AreEqual(1, cell.ChildElements.Count); Paragraph para = cell.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); Word.Text text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
/// <summary> /// Fügt Text in Word ein /// Diese Methode wurde erstellt, damit nicht immer der ganze Pfad angegeben werden muss /// da sich Text aus ReportCompiler.Text und aus DocumentFormat.OpenXml.Wordprocessing.Text /// überschneiden /// </summary> /// <param name="value"></param> /// <returns></returns> private DocumentFormat.OpenXml.Wordprocessing.Text InsertText(string value) { DocumentFormat.OpenXml.Wordprocessing.Text text = new DocumentFormat.OpenXml.Wordprocessing.Text(); text.Text = value; return text; }
public static DocumentFormat.OpenXml.OpenXmlElement[] GetFormattedChapter(Chapter chapter) { if(!stylesAdded) StyleCreator.AddStylePart(); List<OpenXmlElement> result = new List<OpenXmlElement>(); //Setting up Heading style Word.ParagraphProperties paraProps = new Word.ParagraphProperties(); Word.ParagraphStyleId style = new Word.ParagraphStyleId() { Val = "Heading1" }; paraProps.Append(style); //Adding chapter title Word.Paragraph para = new Word.Paragraph(); Word.Run run = new Word.Run(); Word.Text text = new Word.Text() { Text = chapter.Title }; run.Append(text); para.Append(paraProps); para.Append(run); result.Add(para); //Add all child elements foreach (Element element in chapter.SubElements) { if (element.GetElementType() == ElementType.MultiColumnSection) { result.AddRange(MultiColumnSectionFormatter.GetFormattedSection((MultiColumnSection)element)); } else if (element.GetElementType() == ElementType.Section) result.AddRange(SectionFormatter.GetFormattedSection((Section)element)); else throw new InvalidSubFeatureException( chapter.GetElementType().ToString() , element.GetElementType().ToString()); } return result.ToArray(); }