private ParagraphProperties GetListProperties(int id) { var paragraphProperties = new ParagraphProperties(); var paragraphStyleId = new ParagraphStyleId() { Val = "ListParagraph" }; var numbering = new NumberingProperties( new NumberingLevelReference() { Val = 0 }, new NumberingId() { Val = id }); var spacing = new SpacingBetweenLines() { After = "0", Before = "0" }; paragraphProperties.AppendChild(numbering); paragraphProperties.AppendChild(spacing); paragraphProperties.AppendChild(paragraphStyleId); return(paragraphProperties); }
CreateParagraphProperties(WordParagraphProperties paragraphProperties) { if (paragraphProperties != null) { ParagraphProperties properties = new ParagraphProperties(); properties.AppendChild(new Justification() { Val = paragraphProperties.JustificationValues }); properties.AppendChild(new SpacingBetweenLines { LineRule = LineSpacingRuleValues.Auto }); properties.AppendChild(new Indentation()); ParagraphMarkRunProperties paragraphMarkRunProperties = new ParagraphMarkRunProperties(); if (!string.IsNullOrEmpty(paragraphProperties.Size)) { paragraphMarkRunProperties.AppendChild(new FontSize { Val = paragraphProperties.Size }); } if (paragraphProperties.Bold) { paragraphMarkRunProperties.AppendChild(new Bold()); } properties.AppendChild(paragraphMarkRunProperties); return(properties); } return(null); }
private static TableCell SetupCell(HetOwner owner, double widthInCm, double start) { try { var tableCell = new TableCell(); var tableCellProperties = new TableCellProperties(); tableCellProperties.AppendChild(new TableCellWidth { Width = CentimeterToDxa(widthInCm).ToString(), Type = TableWidthUnitValues.Dxa }); tableCellProperties.AppendChild(new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }); tableCell.AppendChild(tableCellProperties); var paragraphProperties = new ParagraphProperties(); var paragraphMarkRunProperties = new ParagraphMarkRunProperties(); paragraphMarkRunProperties.AppendChild(new Color { Val = "000000" }); paragraphMarkRunProperties.AppendChild(new RunFonts { Ascii = "Arial" }); paragraphMarkRunProperties.AppendChild(new FontSize() { Val = "13pt" }); paragraphProperties.AppendChild(paragraphMarkRunProperties); paragraphProperties.AppendChild(new Justification { Val = JustificationValues.Left }); paragraphProperties.AppendChild(new Indentation { Start = CentimeterToDxa(start).ToString() }); var paragraph = new Paragraph(); paragraph.AppendChild(paragraphProperties); if (owner != null) { PopulateParagraph(owner, paragraph); } tableCell.AppendChild(paragraph); return(tableCell); } catch (Exception e) { Console.WriteLine(e); throw; } }
public void Transform(string firstName, string lastName, string city) { using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(LastConvertedWordFileName ?? LastWordFileName, true)) { Body body = wordprocessingDocument.MainDocumentPart.Document.Body; var firstParagraph = body.ChildElements.FirstOrDefault(c => c is Paragraph); if (firstParagraph != null) { AddPersonalRow(body, firstParagraph, firstName, lastName, city); } ApplyColorsFormat(body); Paragraph breakParagraph = new Paragraph(); ParagraphProperties breakProperties = new ParagraphProperties(); SectionProperties sectionProperties = new SectionProperties(); SectionType sectionType = new SectionType() { Val = SectionMarkValues.NextPage }; sectionProperties.AppendChild(sectionType); breakProperties.AppendChild(sectionProperties); breakParagraph.AppendChild(breakProperties); body.AppendChild(breakParagraph); AddSecondPageTable(body); } }
/// <summary> /// Pushes a new paragraph onto the <see cref="OpenXmlCompositeElement"/> stack, /// applying the paragraph styles elements that are currently on the paragraph format stack. /// If a matching paragraph style could not be found in the OpenXML document, an empty style is created. /// This leaves the user the chance to modify the paragraph style afterwards in the document. /// </summary> /// <returns>The newly created paragraph, with the paragraph style already appended.</returns> public Paragraph PushNewParagraph() { var paragraph = new Paragraph(); if (_currentParagraphFormatStack.Count == 0) { // there is no need to add paragraph properties here, because the "Normal" style is assumed in this case } else { var paragraphStyleId = GetOrCreateNewParagraphStyleRecursivelyFromParagraphStack(); var paragraphProperties = new ParagraphProperties { ParagraphStyleId = new ParagraphStyleId() { Val = paragraphStyleId } }; if (null != NumberingProperties) { paragraphProperties.AppendChild(NumberingProperties); NumberingProperties = null; } paragraph.AppendChild(paragraphProperties); } Push(paragraph); return(paragraph); }
private static TableCell CreateCell(string text, bool?isThead = false) { var cell = new TableCell(); var paragraph = new Paragraph(new Run(new Text(text))); ParagraphProperties paragraphProperties = new ParagraphProperties(); TableCellProperties cellProperties = new TableCellProperties(); cellProperties.AppendChild(new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }); cellProperties.AppendChild(new Justification() { Val = JustificationValues.Center }); TableCellWidth cellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Auto }; if (isThead.HasValue && isThead.Value) { paragraphProperties.AppendChild(new Justification() { Val = JustificationValues.Center }); } else { paragraphProperties.AppendChild(new Justification() { Val = JustificationValues.Left }); } paragraphProperties.AppendChild(new TextAlignment() { Val = VerticalTextAlignmentValues.Center }); cell.AppendChild(cellWidth); cell.AppendChild(cellProperties); cell.AppendChild(paragraphProperties); cell.AppendChild(paragraph); return(cell); }
private static ParagraphProperties GetJustifiedParagraphProperties(string heading) { var paragraphProperties = new ParagraphProperties(); if (heading != null) { var paragraphStyleId = new ParagraphStyleId() { Val = heading }; paragraphProperties.AppendChild(paragraphStyleId); } var justification = new Justification { Val = JustificationValues.Left }; paragraphProperties.AppendChild(justification); return(paragraphProperties); }
/// <summary> /// Add a style based on the given style name. /// </summary> /// <param name="style">The name of the style.</param> /// <returns></returns> protected virtual ParagraphProperties ooxmlParagraphProp(string style) { var paraProp = new ParagraphProperties(); var styleId = new ParagraphStyleId { Val = string.Format(CultureInfo.CurrentCulture, style) }; paraProp.AppendChild(styleId); return(paraProp); }
/// <summary> /// Add a style based on the given paragraph level. /// </summary> /// <param name="paragraphLevel">The level of the paragraph.</param> /// <returns></returns> protected virtual ParagraphProperties ooxmlParagraphProp(int paragraphLevel) { var paraProp = new ParagraphProperties(); var styleId = new ParagraphStyleId { Val = string.Format(CultureInfo.CurrentCulture, "normal{0}", paragraphLevel == 0 ? string.Empty : paragraphLevel.ToString(CultureInfo.CurrentCulture)) }; paraProp.AppendChild(styleId); return(paraProp); }
public void RemoveChildExceptionTest() { Assert.Throws <System.InvalidOperationException>(() => { Paragraph p = new Paragraph(); ParagraphProperties ppr = p.AppendChild(new ParagraphProperties()); var autoSpace = ppr.AppendChild(new AutoSpaceDE()); autoSpace.Val = true; p.RemoveChild(autoSpace); }); }
/// <summary> /// Add a style based on the given paragraph level. /// </summary> /// <param name="paragraphLevel">The level of the paragraph.</param> /// <returns></returns> protected override ParagraphProperties ooxmlParagraphProp(int paragraphLevel) { var paraProp = new ParagraphProperties(); var styleId = new ParagraphStyleId { Val = string.Format(CultureInfo.CurrentCulture, "Heading{0}", paragraphLevel == 0 ? "1" : paragraphLevel.ToString(CultureInfo.CurrentCulture)) }; paraProp.AppendChild(styleId); return(paraProp); }
public static ParagraphProperties GetParagraphProperties(ResumeParagraph resumeParagraph, IEnumerable <OpenXmlElement> insertBefore = null) { var pargraphProperties = new ParagraphProperties(); if (insertBefore != null) { pargraphProperties.Append(insertBefore); } pargraphProperties.AppendChild(GetJustification(resumeParagraph.HorisontalAlignment)); return(pargraphProperties); }
public static void Render(this Page page, Models.Document document, OpenXmlElement wdDoc, ContextModel context, MainDocumentPart mainDocumentPart, IFormatProvider formatProvider) { if (!string.IsNullOrWhiteSpace(page.ShowKey) && context.ExistItem <BooleanModel>(page.ShowKey) && !context.GetItem <BooleanModel>(page.ShowKey).Value) { return; } // add page content ((BaseElement)page).Render(document, wdDoc, context, mainDocumentPart, formatProvider); // add section to manage orientation. Last section is at the end of document var pageSize = new PageSize() { Orient = page.PageOrientation.ToOOxml(), Width = UInt32Value.FromUInt32(page.PageOrientation == PageOrientationValues.Landscape ? (uint)16839 : 11907), Height = UInt32Value.FromUInt32(page.PageOrientation == PageOrientationValues.Landscape ? (uint)11907 : 16839) }; var sectionProps = new SectionProperties(pageSize); // document margins if (page.Margin != null) { var pageMargins = new PageMargin() { Left = page.Margin.Left, Top = page.Margin.Top, Right = page.Margin.Right, Bottom = page.Margin.Bottom, Footer = page.Margin.Footer, Header = page.Margin.Header }; sectionProps.AppendChild(pageMargins); } var p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(); var ppr = new ParagraphProperties(); p.AppendChild(ppr); ppr.AppendChild(sectionProps); wdDoc.AppendChild(p); }
public static OpenXmlElement Convert(Paragraph paragraph, WordprocessingDocument document, int?defaultFontSize = null) { var docxParagraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(); var parameters = paragraph.Parameters; var paragraphProperties = new ParagraphProperties(); if (parameters.Alignment.HasValue) { paragraphProperties.Justification = new Justification { Val = GetJustificationValues(parameters.Alignment.Value) } } ; if (parameters.SpaceBetweenLines.HasValue) { paragraphProperties.SpacingBetweenLines = new SpacingBetweenLines { LineRule = LineSpacingRuleValues.Auto, Line = (240 * parameters.SpaceBetweenLines.Value).ToString() } } ; var runProperties = new RunProperties(); if (parameters.Bold) { runProperties.Bold = new Bold(); } var fontSize = parameters.FontSize ?? defaultFontSize; if (fontSize.HasValue) { var rPr = new ParagraphMarkRunProperties(new FontSize { Val = (fontSize.Value * 2).ToString() }, new FontSizeComplexScript { Val = (fontSize.Value * 2).ToString() }); paragraphProperties.AppendChild(rPr); runProperties.FontSize = new FontSize { Val = (fontSize.Value * 2).ToString() }; runProperties.FontSizeComplexScript = new FontSizeComplexScript { Val = (fontSize.Value * 2).ToString() }; } var defaultFont = FontFamily.GenericSansSerif.Name; runProperties.RunFonts = new RunFonts { Ascii = defaultFont, ComplexScript = defaultFont, HighAnsi = defaultFont }; if (parameters.BackgroundColor.HasValue) { runProperties.Shading = new Shading { Val = ShadingPatternValues.Clear, Color = "auto", Fill = parameters.BackgroundColor.Value.ToHex() } } ; foreach (var run in parameters.Parts.Select(x => GetRun(document, x, runProperties))) { docxParagraph.AppendChild(run); } docxParagraph.ParagraphProperties = paragraphProperties; return(docxParagraph); }
private void ValidatePpr(SdbSchemaDatas sdbSchemaDatas) { ValidationContext validationContext = new ValidationContext(); OpenXmlElement errorChild; ParagraphProperties pPr = new ParagraphProperties(); var particleConstraint = sdbSchemaDatas.GetSchemaTypeData(pPr).ParticleConstraint; var target = particleConstraint.ParticleValidator as ParticleValidator; validationContext.Element = pPr; var expected = pPr; //<xsd:complexType name="CT_PPr"> // <xsd:complexContent> // <xsd:extension base="CT_PPrBase"> // <xsd:sequence> // <xsd:element name="rPr" type="CT_ParaRPr" minOccurs="0"> // <xsd:element name="sectPr" type="CT_SectPr" minOccurs="0"> // <xsd:element name="pPrChange" type="CT_PPrChange" minOccurs="0"> // </xsd:sequence> // </xsd:extension> // </xsd:complexContent> //</xsd:complexType> //<xsd:complexType name="CT_PPrBase"> // <xsd:sequence> // <xsd:element name="pStyle" type="CT_String" minOccurs="0"> // <xsd:element name="keepNext" type="CT_OnOff" minOccurs="0"> // <xsd:element name="keepLines" type="CT_OnOff" minOccurs="0"> // <xsd:element name="pageBreakBefore" type="CT_OnOff" minOccurs="0"> // <xsd:element name="framePr" type="CT_FramePr" minOccurs="0"> // <xsd:element name="widowControl" type="CT_OnOff" minOccurs="0"> // <xsd:element name="numPr" type="CT_NumPr" minOccurs="0"> // <xsd:element name="suppressLineNumbers" type="CT_OnOff" minOccurs="0"> // <xsd:element name="pBdr" type="CT_PBdr" minOccurs="0"> // <xsd:element name="shd" type="CT_Shd" minOccurs="0"> // <xsd:element name="tabs" type="CT_Tabs" minOccurs="0"> // <xsd:element name="suppressAutoHyphens" type="CT_OnOff" minOccurs="0"> // <xsd:element name="kinsoku" type="CT_OnOff" minOccurs="0"> // <xsd:element name="wordWrap" type="CT_OnOff" minOccurs="0"> // <xsd:element name="overflowPunct" type="CT_OnOff" minOccurs="0"> // <xsd:element name="topLinePunct" type="CT_OnOff" minOccurs="0"> // <xsd:element name="autoSpaceDE" type="CT_OnOff" minOccurs="0"> // <xsd:element name="autoSpaceDN" type="CT_OnOff" minOccurs="0"> // <xsd:element name="bidi" type="CT_OnOff" minOccurs="0"> // <xsd:element name="adjustRightInd" type="CT_OnOff" minOccurs="0"> // <xsd:element name="snapToGrid" type="CT_OnOff" minOccurs="0"> // <xsd:element name="spacing" type="CT_Spacing" minOccurs="0"> // <xsd:element name="ind" type="CT_Ind" minOccurs="0"> // <xsd:element name="contextualSpacing" type="CT_OnOff" minOccurs="0"> // <xsd:element name="mirrorIndents" type="CT_OnOff" minOccurs="0"> // <xsd:element name="suppressOverlap" type="CT_OnOff" minOccurs="0"> // <xsd:element name="jc" type="CT_Jc" minOccurs="0"> // <xsd:element name="textDirection" type="CT_TextDirection" minOccurs="0"> // <xsd:element name="textAlignment" type="CT_TextAlignment" minOccurs="0"> // <xsd:element name="textboxTightWrap" type="CT_TextboxTightWrap" minOccurs="0"> // <xsd:element name="outlineLvl" type="CT_DecimalNumber" minOccurs="0"> // <xsd:element name="divId" type="CT_DecimalNumber" minOccurs="0"> // <xsd:element name="cnfStyle" type="CT_Cnf" minOccurs="0" maxOccurs="1"> // </xsd:sequence> //</xsd:complexType> // ***** good case ****** // empty is ok target.Validate(validationContext); Assert.True(validationContext.Valid); // pPr.AppendChild(new KeepLines()); target.Validate(validationContext); Assert.True(validationContext.Valid); // pPr.AppendChild(new Tabs()); target.Validate(validationContext); Assert.True(validationContext.Valid); // pPr.AppendChild(new Kinsoku()); target.Validate(validationContext); Assert.True(validationContext.Valid); // pPr.AppendChild(new OutlineLevel()); target.Validate(validationContext); Assert.True(validationContext.Valid); // pPr.AppendChild(new ConditionalFormatStyle()); target.Validate(validationContext); Assert.True(validationContext.Valid); // pPr.AppendChild(new ParagraphMarkRunProperties()); target.Validate(validationContext); Assert.True(validationContext.Valid); // pPr.AppendChild(new SectionProperties()); target.Validate(validationContext); Assert.True(validationContext.Valid); // pPr.AppendChild(new ParagraphPropertiesChange()); target.Validate(validationContext); Assert.True(validationContext.Valid); // ***** error case ****** // SectionProperties dup error errorChild = pPr.AppendChild(new SectionProperties()); target.Validate(validationContext); Assert.False(validationContext.Valid); Assert.Single(validationContext.Errors); Assert.Same(expected, validationContext.Errors[0].Node); Assert.Equal(ValidationErrorType.Schema, validationContext.Errors[0].ErrorType); Assert.Equal("Sch_UnexpectedElementContentExpectingComplex", validationContext.Errors[0].Id); Assert.DoesNotContain(ValidationErrorStrings.Fmt_ListOfPossibleElements, validationContext.Errors[0].Description); pPr.RemoveChild(errorChild); validationContext.Clear(); // SectionProperties order wrong errorChild = pPr.FirstChild; pPr.PrependChild(new SectionProperties()); target.Validate(validationContext); Assert.False(validationContext.Valid); Assert.Single(validationContext.Errors); Assert.Same(expected, validationContext.Errors[0].Node); Assert.Same(errorChild, validationContext.Errors[0].RelatedNode); Assert.Equal(ValidationErrorType.Schema, validationContext.Errors[0].ErrorType); Assert.Equal("Sch_UnexpectedElementContentExpectingComplex", validationContext.Errors[0].Id); Assert.DoesNotContain(ValidationErrorStrings.Fmt_ListOfPossibleElements, validationContext.Errors[0].Description); pPr.RemoveChild(pPr.FirstChild); }
public TFluent SetFontSize(double fontSize) { OpenXmlElement textBody = this.element.GetOrCreateTextBody(); BodyProperties bodyProperties = textBody.GetFirstChild <BodyProperties>() ?? textBody.AppendChild(new BodyProperties()); foreach (Paragraph paragraph in textBody.Elements <Paragraph>()) { ParagraphProperties paragraphProperties = paragraph.GetFirstChild <ParagraphProperties>() ?? paragraph.PrependChild(new ParagraphProperties()); DefaultRunProperties defaultRunProperties = paragraphProperties.GetFirstChild <DefaultRunProperties>() ?? paragraphProperties.AppendChild(new DefaultRunProperties()); defaultRunProperties.FontSize = (int)(fontSize * 100); foreach (Run run in paragraph.Elements <Run>()) { RunProperties runProperties = run.GetFirstChild <RunProperties>() ?? run.PrependChild(new RunProperties()); runProperties.FontSize = (int)(fontSize * 100); } } return(this.result); }
public TFluent SetFont(string typeface) { OpenXmlElement textBody = this.element.GetOrCreateTextBody(); BodyProperties bodyProperties = textBody.GetFirstChild <BodyProperties>() ?? textBody.AppendChild(new BodyProperties()); foreach (Paragraph paragraph in textBody.Elements <Paragraph>()) { ParagraphProperties paragraphProperties = paragraph.GetFirstChild <ParagraphProperties>() ?? paragraph.PrependChild(new ParagraphProperties()); DefaultRunProperties defaultRunProperties = paragraphProperties.GetFirstChild <DefaultRunProperties>() ?? paragraphProperties.AppendChild(new DefaultRunProperties()); defaultRunProperties.RemoveAllChildren <LatinFont>(); defaultRunProperties.RemoveAllChildren <ComplexScriptFont>(); defaultRunProperties.AppendChild(new LatinFont() { Typeface = typeface }); defaultRunProperties.AppendChild(new ComplexScriptFont() { Typeface = typeface }); foreach (Run run in paragraph.Elements <Run>()) { RunProperties runProperties = run.GetFirstChild <RunProperties>() ?? run.PrependChild(new RunProperties()); runProperties.RemoveAllChildren <LatinFont>(); runProperties.RemoveAllChildren <ComplexScriptFont>(); runProperties.AppendChild(new LatinFont() { Typeface = typeface }); runProperties.AppendChild(new ComplexScriptFont() { Typeface = typeface }); } } return(this.result); }
public TFluent SetFontColor(OpenXmlColor color) { OpenXmlElement textBody = this.element.GetOrCreateTextBody(); BodyProperties bodyProperties = textBody.GetFirstChild <BodyProperties>() ?? textBody.AppendChild(new BodyProperties()); foreach (Paragraph paragraph in textBody.Elements <Paragraph>()) { ParagraphProperties paragraphProperties = paragraph.GetFirstChild <ParagraphProperties>() ?? paragraph.PrependChild(new ParagraphProperties()); DefaultRunProperties defaultRunProperties = paragraphProperties.GetFirstChild <DefaultRunProperties>() ?? paragraphProperties.AppendChild(new DefaultRunProperties()); defaultRunProperties.RemoveAllChildren <NoFill>(); defaultRunProperties.RemoveAllChildren <SolidFill>(); defaultRunProperties.RemoveAllChildren <GradientFill>(); defaultRunProperties.RemoveAllChildren <BlipFill>(); defaultRunProperties.RemoveAllChildren <PatternFill>(); defaultRunProperties.RemoveAllChildren <GroupFill>(); defaultRunProperties.AppendChild(new SolidFill().AppendChildFluent(color.CreateColorElement())); foreach (Run run in paragraph.Elements <Run>()) { RunProperties runProperties = run.GetFirstChild <RunProperties>() ?? run.PrependChild(new RunProperties()); runProperties.RemoveAllChildren <NoFill>(); runProperties.RemoveAllChildren <SolidFill>(); runProperties.RemoveAllChildren <GradientFill>(); runProperties.RemoveAllChildren <BlipFill>(); runProperties.RemoveAllChildren <PatternFill>(); runProperties.RemoveAllChildren <GroupFill>(); runProperties.AppendChild(new SolidFill().AppendChildFluent(color.CreateColorElement())); } } return(this.result); }
private static TableCell SetupCell(string text, bool center = false) { try { TableCell tableCell = new TableCell(); TableCellProperties tableCellProperties = new TableCellProperties(); // border & padding TableCellBorders borders = new TableCellBorders(); TopBorder topBorder = new TopBorder { Val = new EnumValue <BorderValues>(BorderValues.Thick), Color = "A1A2A3" }; borders.AppendChild(topBorder); BottomBorder bottomBorder = new BottomBorder { Val = new EnumValue <BorderValues>(BorderValues.Thick), Color = "A1A2A3" }; borders.AppendChild(bottomBorder); RightBorder rightBorder = new RightBorder { Val = new EnumValue <BorderValues>(BorderValues.Nil) }; borders.AppendChild(rightBorder); LeftBorder leftBorder = new LeftBorder { Val = new EnumValue <BorderValues>(BorderValues.Nil) }; borders.AppendChild(leftBorder); TableCellMargin margin = new TableCellMargin(); TopMargin topMargin = new TopMargin() { Width = "40" }; BottomMargin bottomMargin = new BottomMargin() { Width = "40" }; margin.AppendChild(topMargin); margin.AppendChild(bottomMargin); tableCellProperties.AppendChild(borders); tableCellProperties.AppendChild(margin); tableCell.AppendChild(tableCellProperties); // add text (with specific formatting) Paragraph paragraph = new Paragraph() { RsidParagraphAddition = "00607D74", RsidRunAdditionDefault = "00607D74", ParagraphId = "6ED85602", TextId = "77777777" }; ParagraphProperties paragraphProperties = new ParagraphProperties(); ParagraphMarkRunProperties paragraphMarkRunProperties = new ParagraphMarkRunProperties(); paragraphMarkRunProperties.AppendChild(new Color { Val = "000000" }); paragraphMarkRunProperties.AppendChild(new RunFonts { Ascii = "Arial" }); paragraphMarkRunProperties.AppendChild(new FontSize() { Val = "7pt" }); Justification justification = new Justification() { Val = JustificationValues.Left }; if (center) { justification.Val = JustificationValues.Center; } paragraphProperties.AppendChild(paragraphMarkRunProperties); paragraphProperties.AppendChild(justification); paragraph.AppendChild(paragraphProperties); paragraph.AppendChild(new Text(text)); // add to table cell tableCell.AppendChild(paragraph); return(tableCell); } catch (Exception e) { Console.WriteLine(e); throw; } }
private static TableCell SetupHeaderCell(string text, string width, bool center = false) { try { TableCell tableCell = new TableCell(); TableCellProperties tableCellProperties = new TableCellProperties(); TableCellWidth tableCellWidth = new TableCellWidth() { Width = width, Type = TableWidthUnitValues.Dxa }; Shading shading = new Shading() { Val = ShadingPatternValues.Clear, Fill = "FFFFFF", Color = "auto" }; // border & padding TableCellBorders borders = new TableCellBorders(); TopBorder topBorder = new TopBorder { Val = new EnumValue <BorderValues>(BorderValues.Thick), Color = "000000" }; borders.AppendChild(topBorder); BottomBorder bottomBorder = new BottomBorder { Val = new EnumValue <BorderValues>(BorderValues.Thick), Color = "000000" }; borders.AppendChild(bottomBorder); TableCellMargin margin = new TableCellMargin(); TopMargin topMargin = new TopMargin() { Width = "40" }; BottomMargin bottomMargin = new BottomMargin() { Width = "40" }; margin.AppendChild(topMargin); margin.AppendChild(bottomMargin); tableCellProperties.AppendChild(tableCellWidth); tableCellProperties.AppendChild(shading); tableCellProperties.AppendChild(borders); tableCellProperties.AppendChild(margin); tableCellProperties.AppendChild(new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }); tableCell.AppendChild(tableCellProperties); // add text (with specific formatting) Paragraph paragraph = new Paragraph() { RsidParagraphAddition = "00607D74", RsidRunAdditionDefault = "00607D74", ParagraphId = "6ED85602", TextId = "77777777" }; ParagraphProperties paragraphProperties = new ParagraphProperties(); ParagraphMarkRunProperties paragraphMarkRunProperties = new ParagraphMarkRunProperties(); paragraphMarkRunProperties.AppendChild(new Color { Val = "000000" }); paragraphMarkRunProperties.AppendChild(new RunFonts { Ascii = "Arial" }); paragraphMarkRunProperties.AppendChild(new FontSize() { Val = "8pt" }); paragraphMarkRunProperties.AppendChild(new Bold()); Justification justification = new Justification() { Val = JustificationValues.Left }; if (center) { justification.Val = JustificationValues.Center; } paragraphProperties.AppendChild(paragraphMarkRunProperties); paragraphProperties.AppendChild(justification); paragraph.AppendChild(paragraphProperties); paragraph.AppendChild(new Text(text)); // add to table cell tableCell.AppendChild(paragraph); return(tableCell); } catch (Exception e) { Console.WriteLine(e); throw; } }
public TableCell ToTableCell() { TableCell tableCell = new TableCell(); TableCellProperties tableCellProperties = new TableCellProperties(); // Specify the width property of the table cell. TableCellWidth tableCellWidth = new TableCellWidth() { Width = Width.ToString(), Type = TableWidthUnitValues.Dxa }; TableCellMargin tableCellMargin = new TableCellMargin(); LeftMargin leftMargin = new LeftMargin() { Width = "100", Type = TableWidthUnitValues.Dxa }; RightMargin rightMargin = new RightMargin() { Width = "100", Type = TableWidthUnitValues.Dxa }; tableCellMargin.Append(leftMargin); tableCellMargin.Append(rightMargin); TableCellVerticalAlignment tableCellVerticalAlignment = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }; switch (TipoDeCelula) { case TipoDeCelula.HEADER: tableCellProperties.Append(new Shading() { Val = ShadingPatternValues.Percent10, Color = "000000", Fill = "auto" }); break; case TipoDeCelula.RESUME: tableCellProperties.Append(new Shading() { Val = ShadingPatternValues.Percent10, Color = "000000", Fill = "auto" }); break; default: // TipoDeCelula.NORMAL break; } tableCellProperties.Append(tableCellWidth); tableCellProperties.Append(tableCellMargin); tableCellProperties.Append(tableCellVerticalAlignment); switch (Merge) { case TipoDeMerge.RESTART: tableCellProperties.Append(new HorizontalMerge() { Val = MergedCellValues.Restart }); break; case TipoDeMerge.CONTINUE: tableCellProperties.Append(new HorizontalMerge() { Val = MergedCellValues.Continue }); break; default: // TipoDeMerge.NENHUM break; } tableCell.Append(tableCellProperties); Paragraph paragraph = new Paragraph(); ParagraphProperties paragraphProperties = new ParagraphProperties(); switch (Alinhamento) { case TipoDeAlinhamento.ESQUERDO: paragraphProperties.Append(new ParagraphStyleId() { Val = "LeftTextTable" }); paragraphProperties.AppendChild( new RunProperties(new Bold() { Val = OnOffValue.FromBoolean(true) })); break; case TipoDeAlinhamento.CENTRO: paragraphProperties.Append(new ParagraphStyleId() { Val = "CenteredTextTable" }); paragraphProperties.AppendChild( new RunProperties(new Bold() { Val = OnOffValue.FromBoolean(true) })); break; case TipoDeAlinhamento.DIREITO: paragraphProperties.Append(new ParagraphStyleId() { Val = "RightTextTable" }); paragraphProperties.AppendChild( new RunProperties(new Bold() { Val = OnOffValue.FromBoolean(true) })); break; default: //TipoDeAlinhamento.NENHUM: paragraphProperties.Append(new ParagraphStyleId() { Val = "NormalTextTable" }); paragraphProperties.AppendChild( new RunProperties(new Bold() { Val = OnOffValue.FromBoolean(true) })); break; } paragraph.AppendChild(paragraphProperties); foreach (OpenXmlElement run in _runList) { switch (TipoDeCelula) { case TipoDeCelula.HEADER: if (!run.Elements <RunProperties>().Any()) { run.AppendChild(new RunProperties()); } run.Elements <RunProperties>().First().AppendChild(new Bold() { Val = OnOffValue.FromBoolean(true) }); break; case TipoDeCelula.RESUME: if (!run.Elements <RunProperties>().Any()) { run.AppendChild(new RunProperties()); } run.Elements <RunProperties>().First().AppendChild(new Italic() { Val = OnOffValue.FromBoolean(true) }); run.Elements <RunProperties>().First().AppendChild(new Bold() { Val = OnOffValue.FromBoolean(true) }); break; } paragraph.AppendChild(run); } // Write some text in the cell. tableCell.Append(paragraph); return(tableCell); }
/// <summary> /// Render a cell in a word document /// </summary> /// <param name="cell"></param> /// <param name="document"></param> /// <param name="parent"></param> /// <param name="context"></param> /// <param name="documentPart"></param> /// <param name="isInAlternateRow"></param> /// <param name="formatProvider"></param> /// <returns></returns> public static TableCell Render(this Cell cell, Models.Document document, OpenXmlElement parent, ContextModel context, OpenXmlPart documentPart, bool isInAlternateRow, IFormatProvider formatProvider) { context.ReplaceItem(cell, formatProvider); TableCell wordCell = new TableCell(); TableCellProperties cellProp = new TableCellProperties(); wordCell.AppendChild(cellProp); if (isInAlternateRow) { cell.ReplaceAlternateConfiguration(); } cellProp.AddBorders(cell); cellProp.AddShading(cell); cellProp.AddMargin(cell); cellProp.AddJustifications(cell); if (cell.CellWidth != null) { cellProp.AppendChild(new TableCellWidth() { Width = cell.CellWidth.Width, Type = cell.CellWidth.Type.ToOOxml() }); } // manage cell column and row span if (cell.ColSpan > 1) { cellProp.AppendChild(new GridSpan() { Val = cell.ColSpan }); } if (cell.Fusion) { if (cell.FusionChild) { cellProp.AppendChild(new VerticalMerge() { Val = MergedCellValues.Continue }); } else { cellProp.AppendChild(new VerticalMerge() { Val = MergedCellValues.Restart }); } } if (!cell.Show) { return(wordCell); } if (cell.ChildElements.Any(x => x is Models.TemplateModel)) { // Need to replace elements : for (int i = 0; i < cell.ChildElements.Count; i++) { var e = cell.ChildElements[i]; if (e is TemplateModel) { var elements = (e as TemplateModel).ExtractTemplateItems(document); if (i == cell.ChildElements.Count - 1) { cell.ChildElements.AddRange(elements); } else { cell.ChildElements.InsertRange(i + 1, elements); } } } } if (cell.ChildElements.Any(x => x is Models.Paragraph) || cell.ChildElements.Any(x => x is ForEach)) { foreach (var element in cell.ChildElements) { element.InheritFromParent(cell); if (element is Models.Paragraph || element is ForEach) { element.Render(document, wordCell, context, documentPart, formatProvider); } else { var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(); if (cell.Justification.HasValue) { var ppr = new ParagraphProperties(); ppr.AppendChild(new Justification() { Val = cell.Justification.Value.ToOOxml() }); paragraph.AppendChild(ppr); } wordCell.AppendChild(paragraph); var r = new Run(); paragraph.AppendChild(r); element.Render(document, r, context, documentPart, formatProvider); } } } else { // fill cell content (need at least an empty paragraph) var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(); if (cell.Justification.HasValue) { var ppr = new ParagraphProperties(); ppr.AppendChild(new Justification() { Val = cell.Justification.Value.ToOOxml() }); paragraph.AppendChild(ppr); } wordCell.AppendChild(paragraph); var r = new Run(); paragraph.AppendChild(r); foreach (var element in cell.ChildElements) { element.InheritFromParent(cell); element.Render(document, r, context, documentPart, formatProvider); } } return(wordCell); }