public void OpenReplaceSimple() { string fileName = "replace-simple-1"; string fileTargetPath = Path.Combine(s_BasePath, fileName + ".docx"); using (WordServerDocument doc = new WordServerDocument()) { File.Copy($"{s_BasePath}\\SimpleDocument.docx", fileTargetPath, true); Assert.AreEqual(doc.Open(fileTargetPath, out _), OutputCode.OK); doc.AddText("Text to be replaced", new List <string>()); doc.AddText("Start Programmatically: " + DateTime.Now, new List <string>()); doc.ReplaceText("Aggregate", "AggregateReplacedText", false); doc.ReplaceTextWithStyle("clarati", "YYYYY", false, new List <string>() { "fontsize:20", "color:red" }); doc.ReplaceTextWithStyle("Text to BE REPLACED", "ReplacedTextOK", false, new List <string>() { "fontsize:25", "color:blue" }); doc.AddText("End Programmatically: " + DateTime.Now, new List <string>()); doc.Save(); } ExtractXlsx(fileTargetPath, Path.Combine(s_BasePath, fileName)); }
public void CreationParagraphsWithRectangleShapeAndImage() { string fileName = "shape-image"; string fileTargetPath = Path.Combine(s_BasePath, fileName + ".docx"); using (WordServerDocument doc = new WordServerDocument()) { File.Delete(fileTargetPath); Assert.AreEqual(doc.Create(fileTargetPath, true, out _), OutputCode.OK); doc.AddImage($"{s_BasePath}\\tres.jpeg", 20, 20); doc.AddText("Inline text. ", new List <string>()); doc.AddShapeWithText("", "SQUARE2", 3, 3, 0, 0, new List <string>() { "strokewidth:4", "color:blue" } , new List <string>() { "fontsize:30", "color:#32a852" }); doc.Save(); } ExtractXlsx(fileTargetPath, Path.Combine(s_BasePath, fileName)); }
public void OpenFromTemplate() { WordServerDocument doc = new WordServerDocument(); Assert.AreEqual(doc.Open($"{s_BasePath}\\TemplateSample.docx", out _), OutputCode.OK); doc.Close(); }
public void CreationTestWithoutOverwrite() { WordServerDocument doc = new WordServerDocument(); Assert.AreEqual(doc.Create($"{s_BasePath}\\test.docx", false, out _), OutputCode.FILE_ALREADY_EXISTS); doc.Close(); }
public void ReplaceComplexTextNoStyle() { string fileName = "replaced-complex-test-no-style-2"; string fileTargetPath = Path.Combine(s_BasePath, fileName + ".docx"); using (WordServerDocument doc = new WordServerDocument()) { File.Copy($"{s_BasePath}\\SampleFull.docx", fileTargetPath, true); Assert.AreEqual(doc.Open(fileTargetPath, out _), OutputCode.OK); // Assert.IsTrue(doc.ReplaceText("Aggregate", "AggregateReplacedText", false) > 0); Assert.IsTrue(doc.ReplaceTextWithStyle("Declaration", "OtherText", true, new List <string>() { "fontsize:54", "color:pink" }) > 0); Assert.IsTrue(doc.ReplaceTextWithStyle("onsumed", "ONSUMED", true, new List <string>() { "fontsize:20", "color:pink" }) > 0); doc.AddText("FAX:", new List <string>() { "fontsize:54", "color:pink" }); doc.Save(); } ExtractXlsx(fileTargetPath, Path.Combine(s_BasePath, fileName)); }
public void CreateReplaceSimple() { string fileName = "create-replace-simple-1"; string fileTargetPath = Path.Combine(s_BasePath, fileName + ".docx"); using (WordServerDocument doc = new WordServerDocument()) { Assert.AreEqual(doc.Create(fileTargetPath, true, out _), OutputCode.OK); doc.AddText("Text with no Bold no formatting style", new List <string>()); doc.AddText("Text with Bold", new List <string>() { "bold" }); doc.AddText("Text with bold and italic", new List <string>() { "bold", "italic" }); doc.AddText("Text with bold and italic and red color", new List <string>() { "bold", "italic", "color:red" }); Assert.AreEqual(doc.ReplaceTextWithStyle("bold", "BOLD", false, new List <string>() { "color:blue" }), 4); doc.Save(); } ExtractXlsx(fileTargetPath, Path.Combine(s_BasePath, fileName)); }
public void CreationTest() { WordServerDocument doc = new WordServerDocument(); Assert.AreEqual(doc.Create($"{s_BasePath}\\test.docx", true, out _), OutputCode.OK); doc.Save(); doc.Close(); }
public void CreateShapeWithTextAlignment() { WordServerDocument doc = new WordServerDocument(); string filePath = $"{s_BasePath}\\rectangle-shape-text-alignment.docx"; File.Delete(filePath); Assert.AreEqual(doc.Create(filePath, true, out _), OutputCode.OK); doc.AddText("Bold", new List <string>() { "bold" }); doc.AddShapeWithText("", "TOP-RIGHT", 6, 6, 0, 0, new List <string>() { "strokewidth:4", "color:blue", "verticalalignment:" + VerticalAlignment.Top, "horizontalalignment:" + HorizontalAlignment.Right } , new List <string>() { "fontsize:35", "color:#32a852" }); doc.AddShapeWithText("", "BOTTOM-CENTER", 10, 5, 0, 6, new List <string>() { "strokewidth:4", "color:blue", "verticalalignment:" + VerticalAlignment.Bottom, "horizontalalignment:" + HorizontalAlignment.Center } , new List <string>() { "fontsize:35", "color:#32a852" }); doc.AddShapeWithText("", "Middle-Left", 10, 5, 0, 12, new List <string>() { "strokewidth:4", "color:blue", "verticalalignment:" + VerticalAlignment.Middle, "horizontalalignment:" + HorizontalAlignment.Left } , new List <string>() { "fontsize:35", "color:#32a852" }); doc.Save(); doc.Close(); }
public void AppendAndReplace() { using (WordServerDocument doc = new WordServerDocument()) { string fileTargetPath = $"{s_BasePath}\\append-replace-test.docx"; Assert.AreEqual(doc.Create(fileTargetPath, true, out _), OutputCode.OK); Assert.AreEqual(doc.AddText("Aggregate", new List <string>()), OutputCode.OK); Assert.AreEqual(doc.AddText("Declaration", new List <string>()), OutputCode.OK); doc.ReplaceText("Aggregate", "AggregateReplacedText", false); doc.ReplaceText("Declaration", "OtherText", false); doc.Save(); } }
public void CreationWithImage() { WordServerDocument doc = new WordServerDocument(); Assert.AreEqual(doc.Create($"{s_BasePath}\\testImage.docx", true, out _), OutputCode.OK); doc.AddText("Bold", new List <string>() { "bold" }); doc.AddImage($"{s_BasePath}\\uno.gif", 100, 100); doc.AddImage($"{s_BasePath}\\dos.png", 50, 50); doc.AddImage($"{s_BasePath}\\tres.jpeg", 20, 20); doc.Save(); doc.Close(); }
public void ReplaceAndAddWithStyle() { using (WordServerDocument doc = new WordServerDocument()) { File.Copy($"{s_BasePath}\\TemplateSample.docx", $"{s_BasePath}\\Sample.docx", true); Assert.AreEqual(doc.Open($"{s_BasePath}\\Sample.docx", out _), OutputCode.OK); Assert.AreEqual(doc.ReplaceTextWithStyle("REP01", "新潟県新潟市", true, new List <string>() { "color:blue", "italic", "fontsize:24", "fontfamily:MS PMincho" }), 2); Assert.AreEqual(doc.ReplaceTextWithStyle("REP02", "中央区米山", true, new List <string>() { "italic", "bold", "color:green", "fontfamily:MS PGothic" }), 1); Assert.AreEqual(doc.ReplaceTextWithStyle("REP03", "Remplazo con bold blue italic", true, new List <string>() { "italic", "bold", "color:blue" }), 1); doc.AddText("製品企画室", new List <string>() { "fontsize:110" }); doc.AddText("TEL:", new List <string>() { "italic" }); doc.AddText("FAX:", new List <string>() { "fontsize:54", "color:pink" }); doc.Save(); } }
public void CreationParagraphsWithRectangleShape() { WordServerDocument doc = new WordServerDocument(); string filePath = $"{s_BasePath}\\rectangle-shape.docx"; File.Delete(filePath); Assert.AreEqual(doc.Create($"{s_BasePath}\\rectangle-shape.docx", true, out _), OutputCode.OK); doc.AddText("Bold", new List <string>() { "bold" }); doc.AddText("Italic", new List <string>() { "italic" }); doc.AddText("FontSize 54", new List <string>() { "fontsize:54" }); doc.AddText("color red italic", new List <string>() { "color:red", "italic" }); doc.AddText("This text without format", new List <string>()); doc.AddText("This text without format", new List <string>()); doc.StartParagraph(); doc.AddText("Inline text. ", new List <string>()); doc.EndParagraph(); for (var i = 0; i < 4; i++) { doc.StartParagraph(); doc.AddShapeWithText("", "R" + i, 1.5, 1, (-0.4 * i), (0.15 * i), new List <string>() { "strokewidth:1", "color:red", "fillcolor:white" }); doc.AddText("This text without format for line: " + i, new List <string>()); doc.EndParagraph(); } doc.AddShapeWithText("", "SQUARE", 3, 3, 0, 0, new List <string>() { "strokewidth:5", "color:#32a852", "fillcolor:silver" } , new List <string>() { "fontsize:30", "color:red" }); doc.AddRuledLine(10); doc.AddShapeWithText("", "SQUARE2", 3, 3, 0, 0, new List <string>() { "strokewidth:4", "color:blue" } , new List <string>() { "fontsize:30", "color:#32a852" }); doc.Save(); doc.Close(); }
public void CreationParagraphsWithProperties() { WordServerDocument doc = new WordServerDocument(); Assert.AreEqual(doc.Create($"{s_BasePath}\\testFormats.docx", true, out _), OutputCode.OK); doc.AddText("Bold", new List <string>() { "bold" }); doc.AddText("Italic", new List <string>() { "italic" }); doc.AddText("FontSize 54", new List <string>() { "fontsize:54" }); doc.AddText("color red italic", new List <string>() { "color:red", "italic" }); doc.AddText("This text without format", new List <string>()); doc.AddText("Bold", new List <string>() { "bold", "numbering" }); doc.AddText("Italic", new List <string>() { "italic", "numbering" }); doc.AddText("FontSize 54", new List <string>() { "fontsize:54", "numbering" }); doc.AddText("color red italic", new List <string>() { "color:red", "italic", "numbering" }); doc.AddText("This text without format", new List <string>()); doc.AddText("Bold", new List <string>() { "bold", "numbering", "level:0" }); doc.AddText("Italic", new List <string>() { "italic", "numbering", "level:1" }); doc.AddText("FontSize 54", new List <string>() { "fontsize:54", "numbering", "level:1" }); doc.AddText("color red italic", new List <string>() { "color:red", "italic", "numbering", "level:0" }); doc.AddText("This text without format before page", new List <string>()); doc.AddPageBreak(); doc.AddText("Bold", new List <string>() { "bold", "bullet" }); doc.AddText("Italic", new List <string>() { "italic", "bullet" }); doc.AddText("FontSize 54", new List <string>() { "fontsize:54", "bullet" }); doc.AddText("color red italic", new List <string>() { "color:red", "italic", "bullet" }); doc.AddText("This text without format after page", new List <string>()); doc.AddText("Bold", new List <string>() { "bold", "bullet", "level:0" }); doc.AddText("Italic", new List <string>() { "italic", "bullet", "level:1" }); doc.AddText("FontSize 54", new List <string>() { "fontsize:54", "bullet", "level:1" }); doc.AddText("color red italic", new List <string>() { "color:red", "italic", "bullet", "level:2" }); doc.AddText("This text without format", new List <string>()); doc.AddRuledLine(60); doc.AddText("Bold", new List <string>() { "bold", "numbering" }); doc.AddText("Italic", new List <string>() { "italic", "numbering" }); doc.AddText("FontSize 54", new List <string>() { "fontsize:54", "numbering" }); doc.AddText("color red italic", new List <string>() { "color:red", "italic", "numbering" }); doc.AddText("This text without format", new List <string>()); doc.AddText(" d ", new List <string>() { "underline" }); doc.AddRuledLine(40); doc.Save(); doc.Close(); }
public override OpenXmlCompositeElement Build() { string sElementId = Guid.NewGuid().ToString(); uint docPropId = Id; AlternateContent altContent = new AlternateContent(); AlternateContentChoice alternateContentChoice1 = new AlternateContentChoice() { Requires = "wps" }; Drawing drawing1 = new Drawing(); Wp.Anchor anchor = new Wp.Anchor() { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)114300U, DistanceFromRight = (UInt32Value)114300U, SimplePos = false, RelativeHeight = (UInt32Value)251645952U, BehindDoc = false, Locked = false, LayoutInCell = true, AllowOverlap = true, SimplePosition = new Wp.SimplePosition() { X = 0L, Y = 0L }, HorizontalPosition = new Wp.HorizontalPosition() { RelativeFrom = Wp.HorizontalRelativePositionValues.Column, PositionOffset = new Wp.PositionOffset() { Text = MathOpenXml.CentimetersToEMU(ShapeProperties.PositionLeft).ToString() } }, VerticalPosition = new Wp.VerticalPosition() { RelativeFrom = Wp.VerticalRelativePositionValues.Paragraph, PositionOffset = new Wp.PositionOffset() { Text = MathOpenXml.CentimetersToEMU(ShapeProperties.PositionTop).ToString() } }, }; Wp.Extent extent1 = new Wp.Extent() { Cx = MathOpenXml.CentimetersToEMU(ShapeProperties.Width), Cy = MathOpenXml.CentimetersToEMU(ShapeProperties.Height) }; Wp.EffectExtent effectExtent1 = new Wp.EffectExtent() { LeftEdge = 15240L, TopEdge = 15875L, RightEdge = 12065L, BottomEdge = 10795L }; Wp.WrapNone wrapNone1 = new Wp.WrapNone(); Wp.DocProperties docProperties1 = new Wp.DocProperties() { Id = docPropId, Name = "Text Box " + docPropId }; A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks(); graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main"); Wp.NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties(graphicFrameLocks1); A.Graphic graphic1 = new A.Graphic(); graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main"); A.GraphicData graphicData1 = new A.GraphicData() { Uri = "http://schemas.microsoft.com/office/word/2010/wordprocessingShape" }; Wps.WordprocessingShape wordprocessingShape1 = new Wps.WordprocessingShape(); Wps.NonVisualDrawingShapeProperties nonVisualDrawingShapeProperties1 = new Wps.NonVisualDrawingShapeProperties(new A.ShapeLocks() { NoChangeArrowheads = true }) { TextBox = true }; Wps.ShapeProperties shapeProperties1 = new Wps.ShapeProperties() { BlackWhiteMode = A.BlackWhiteModeValues.Auto }; A.Transform2D transform2D1 = new A.Transform2D(); A.Offset offset1 = new A.Offset() { X = 0L, Y = 0L }; A.Extents extents1 = new A.Extents() { Cx = 382270L, Cy = 230505L }; transform2D1.Append(offset1); transform2D1.Append(extents1); A.PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle }; A.AdjustValueList adjustValueList1 = new A.AdjustValueList(); presetGeometry1.Append(adjustValueList1); A.SolidFill solidFill1 = new A.SolidFill(new A.RgbColorModelHex() { Val = Helper.ToRGBHexColor(ShapeProperties.FillColor, DEFAULT_FILL_HEX_COLOR) }); A.Outline outline1 = new A.Outline() { Width = Math.Max(MIN_STROKE_WIDTH, (Int32Value)(MIN_STROKE_WIDTH * ShapeProperties.StrokeWidth)) }; A.SolidFill solidFill2 = new A.SolidFill(new A.RgbColorModelHex() { Val = Helper.ToRGBHexColor(ShapeProperties.StrokeColor, DEFAULT_STROKE_HEX_COLOR) }); A.HeadEnd headEnd1 = new A.HeadEnd(); A.TailEnd tailEnd1 = new A.TailEnd(); outline1.Append(solidFill2); outline1.Append(new A.Miter()); outline1.Append(headEnd1); outline1.Append(tailEnd1); shapeProperties1.Append(transform2D1); shapeProperties1.Append(presetGeometry1); shapeProperties1.Append(solidFill1); shapeProperties1.Append(outline1); Wps.TextBoxInfo2 txtInfo = new Wps.TextBoxInfo2(); TextBoxContent txtContent = new TextBoxContent(); Paragraph paragraph = new Paragraph(); ParagraphProperties paragraphProps = new ParagraphProperties(new Justification() { Val = (JustificationValues)Enum.Parse(typeof(JustificationValues), ((int)ShapeProperties.HorizontalAlignment).ToString()) }); Run textRun = WordServerDocument.GetTextRun(ShapeProperties.InnerText, TextProperties); paragraph.Append(paragraphProps); paragraph.Append(textRun); txtContent.Append(paragraph); txtInfo.Append(txtContent); wordprocessingShape1.Append(nonVisualDrawingShapeProperties1); wordprocessingShape1.Append(shapeProperties1); wordprocessingShape1.Append(txtInfo); wordprocessingShape1.Append(new Wps.TextBodyProperties(new A.NoAutoFit()) { Rotation = 0, Vertical = A.TextVerticalValues.Horizontal, Wrap = A.TextWrappingValues.Square, LeftInset = 74295, TopInset = 8890, RightInset = 74295, BottomInset = 8890, Anchor = (A.TextAnchoringTypeValues)Enum.Parse(typeof(A.TextAnchoringTypeValues), ((int)ShapeProperties.VerticalAlignment).ToString()), AnchorCenter = true, UpRight = false }); graphicData1.Append(wordprocessingShape1); graphic1.Append(graphicData1); Wp14.RelativeWidth relativeWidth1 = new Wp14.RelativeWidth() { ObjectId = Wp14.SizeRelativeHorizontallyValues.Page }; Wp14.PercentageWidth percentageWidth1 = new Wp14.PercentageWidth(); percentageWidth1.Text = "0"; relativeWidth1.Append(percentageWidth1); Wp14.RelativeHeight relativeHeight1 = new Wp14.RelativeHeight( new Wp14.PercentageHeight() { Text = "0" } ) { RelativeFrom = Wp14.SizeRelativeVerticallyValues.Page }; anchor.Append(extent1); anchor.Append(effectExtent1); anchor.Append(wrapNone1); anchor.Append(docProperties1); anchor.Append(nonVisualGraphicFrameDrawingProperties1); anchor.Append(graphic1); anchor.Append(relativeWidth1); anchor.Append(relativeHeight1); drawing1.Append(anchor); alternateContentChoice1.Append(drawing1); altContent.Append(alternateContentChoice1); return(altContent); }