本文整理汇总了C#中Run.AppendChild方法的典型用法代码示例。如果您正苦于以下问题:C# Run.AppendChild方法的具体用法?C# Run.AppendChild怎么用?C# Run.AppendChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Run
的用法示例。
在下文中一共展示了Run.AppendChild方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateTextParagraph
internal void CreateTextParagraph(WordprocessingDocument package, string style, HorizontalAlignmentType alignment, string text, FormatStyle formatStyle)
{
Body body = package.MainDocumentPart.Document.Body;
if (!string.IsNullOrEmpty(style) && !WordHelper.IsStyleIdInDocument(package, style)) {
throw new System.ArgumentOutOfRangeException(style);
}
Run textRun = new Run();
if (formatStyle != null) {
// Get a reference to the RunProperties object.
RunProperties runProperties = textRun.AppendChild(new RunProperties());
if (formatStyle.IsBold) {
Bold bold = new Bold();
//bold.Val = OnOffValue.FromBoolean(true);
runProperties.AppendChild(bold);
}
}
textRun.AppendChild(new Text(text));
// Add a paragraph with a run and some text.
Paragraph newParagraph = new Paragraph(textRun);
// If the paragraph has no ParagraphProperties object, create one.
WordHelper.CreateParagraphPropertiesIfNonExists(newParagraph);
// Set paragraph alignment
WordHelper.SetParagraphAlignment(newParagraph, alignment);
if (!string.IsNullOrEmpty(style)) {
// Get a reference to the ParagraphProperties object.
ParagraphProperties pPr = newParagraph.ParagraphProperties;
// If a ParagraphStyleId object doesn't exist, create one.
if (pPr.ParagraphStyleId == null)
pPr.ParagraphStyleId = new ParagraphStyleId();
// Set the style of the paragraph.
pPr.ParagraphStyleId.Val = style;
}
// Append new paragraph
body.AppendChild(newParagraph);
}
示例2: Main
static void Main(string[] args)
{
Console.Write("Username: ");
String user = Console.ReadLine();
WordprocessingDocument package = WordprocessingDocument.Create(@"C:\Users\" + user + @"\Desktop\test.docx", WordprocessingDocumentType.Document);
MainDocumentPart main = package.AddMainDocumentPart();
main.Document = new Document();
Document document1 = main.Document;
Body body = document1.AppendChild(new Body());
document1 = package.MainDocumentPart.Document;
document1.Save();
OpenXmlHelper document = new OpenXmlHelper(package, main);
//You will have to have all the files below if you want to successfully test this.
//Image Formats
Paragraph para = new Paragraph();
Run run = new Run();
run.AppendChild(new Text("These are Image Formats"));
para.AppendChild(run);
document.AddParagraph(para);
document.AddImage(@"C:\Users\" + user + @"\Desktop\test\test.jpg");
document.AddImage(@"C:\Users\" + user + @"\Desktop\test\test.gif");
document.AddImage(@"C:\Users\" + user + @"\Desktop\test\test.png");
document.AddImage(@"C:\Users\" + user + @"\Desktop\test\test.tif");
document.AddImage(@"C:\Users\" + user + @"\Desktop\test\test.bmp");
document.AddImage(@"C:\Users\" + user + @"\Desktop\test\test.ico");
//Office XML Formats
para = new Paragraph();
run = new Run();
run.AppendChild(new Text("These are Office XML Formats"));
para.AppendChild(run);
document.AddParagraph(para);
document.AddObject(@"C:\Users\" + user + @"\Desktop\test\test.docx","test.docx");
document.AddObject(@"C:\Users\" + user + @"\Desktop\test\test.xlsx", "test.xlsx");
document.AddObject(@"C:\Users\" + user + @"\Desktop\test\test.pptx", "test.pptx");
//Office Basic Formats
para = new Paragraph();
run = new Run();
run.AppendChild(new Text("These are Office Basic Formats"));
para.AppendChild(run);
document.AddParagraph(para);
document.AddObject(@"C:\Users\" + user + @"\Desktop\test\test.doc", "test.doc");
document.AddObject(@"C:\Users\" + user + @"\Desktop\test\test.xls", "test.xls");
document.AddObject(@"C:\Users\" + user + @"\Desktop\test\test.vsd", "test.vsd");
//Object Formats
para = new Paragraph();
run = new Run();
run.AppendChild(new Text("These are Object Formats"));
para.AppendChild(run);
document.AddParagraph(para);
document.AddObject(@"C:\Users\" + user + @"\Desktop\test\test.xml", "test.xml");
document.AddObject(@"C:\Users\" + user + @"\Desktop\test\test.txt", "test.txt");
document.AddObject(@"C:\Users\" + user + @"\Desktop\test\test.pdf", "test.pdf");
document.AddObject(@"C:\Users\" + user + @"\Desktop\test\test.zip", "test.zip");
document.Close();
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
示例3: GenerateRun
protected Run GenerateRun(RunProperties runProperties, string text)
{
SetFontRunProperties(runProperties);
var run = new Run() { RunProperties = runProperties };
run.AppendChild(new Text(text));
return run;
}
示例4: OXMLRunWrap
public OXMLRunWrap(string sText)
{
XMLRun = new Run();
if (sText != "")
{
Text XMLText = new Text(){Text = sText};
XMLRun.AppendChild(XMLText);
}
}
示例5: FillText
protected void FillText(ref Run runElement, string strFormat)
{
if (runElement.HasChildren)
{
runElement.GetFirstChild<Text>().Text = strFormat;
//runElement.RemoveAllChildren<Text>();
//runElement.AppendChild<Text>(new Text(GeneralFormatter.ToString(rows[i], property.FormatString)));
}
else
{
runElement.AppendChild<Text>(new Text(strFormat));
}
}
示例6: ProcessChildren
private void ProcessChildren(DocxNode currentNode, DocxNode newNode, Run run)
{
foreach (DocxNode child in currentNode.Children)
{
if (child.IsText)
{
if (!IsEmptyText(child.InnerHtml))
{
run.AppendChild(new Text()
{
Text = ClearHtml(child.InnerHtml),
Space = SpaceProcessingModeValues.Preserve
});
}
}
else
{
currentNode.CopyExtentedStyles(newNode);
ProcessTextElement(newNode);
}
}
}
示例7: TestBookmarkRange_AddsBookmarkStartBeforeRangeStart_StartIsFirstIndex
public void TestBookmarkRange_AddsBookmarkStartBeforeRangeStart_StartIsFirstIndex()
{
OxmlDocument doc = new OxmlDocument(this.oxml);
doc._paragraphs[0] = new Paragraph();
Run run1 = new Run();
Run run2 = new Run();
doc._paragraphs[0].AppendChild<Run>(run1);
doc._paragraphs[0].AppendChild<Run>(run2);
run1.AppendChild<Text>(new Text("This is a "));
run2.AppendChild<Text>(new Text("test string."));
doc.BookmarkRange(new OxmlRange_Accessor(0, 10, 15), "testbookmark");
string expected = "This is a ";
string actual = doc._paragraphs[0].GetFirstChild<BookmarkStart>()
.PreviousSibling<Run>().InnerText;
Assert.AreEqual(expected, actual);
}
示例8: TestBookmarkRange_AddsBookmarkEndAfterRangeEnd_StartEndInSameRun
public void TestBookmarkRange_AddsBookmarkEndAfterRangeEnd_StartEndInSameRun()
{
OxmlDocument doc = new OxmlDocument(this.oxml);
doc._paragraphs[0] = new Paragraph();
Run run1 = new Run();
doc._paragraphs[0].AppendChild<Run>(run1);
run1.AppendChild<Text>(new Text("This is a test string."));
doc.BookmarkRange(new OxmlRange_Accessor(0, 4, 15), "testbookmark");
string expected = "string.";
string actual = doc._paragraphs[0].GetFirstChild<BookmarkEnd>()
.NextSibling<Run>().InnerText;
Assert.AreEqual(expected, actual);
}
示例9: AcbSyntaxValidationTest
public void AcbSyntaxValidationTest()
{
var validator = new SchemaValidator();
var element = new Run();
var acFallback = new AlternateContentFallback();
var ac = element.AppendChild(new AlternateContent());
var errors = validator.Validate(ac).Errors;
// Error case: must have one choice, can not have AlternateContent as di
Assert.Equal(1, errors.Count());
Assert.Equal("Sch_IncompleteContentExpectingComplex", errors[0].Id);
ac.AddNamespaceDeclaration("o15", "http://o15.com");
//ac.NamespaceDeclarations
ac.AppendChild(new AlternateContentChoice() { Requires = "o15" });
errors = validator.Validate(ac).Errors;
Assert.Equal(0, errors.Count());
ac.AddNamespaceDeclaration("o14", "http://o14.com");
ac.PrependChild(new AlternateContentChoice() { Requires = "o14" });
errors = validator.Validate(ac).Errors;
Assert.Equal(0, errors.Count());
ac.AppendChild(acFallback);
errors = validator.Validate(ac).Errors;
Assert.Equal(0, errors.Count());
// Error case: should not contains AlternateContent directly as child.
ac.AppendChild(new AlternateContent());
errors = validator.Validate(ac).Errors;
Assert.Equal(1, errors.Count());
Assert.Equal(ValidationErrorType.MarkupCompatibility, errors.First().ErrorType);
Assert.Equal("Sch_InvalidElementContentExpectingComplex", errors.First().Id);
ac.RemoveChild(ac.LastChild);
// Error case: can only contains one Fallback.
ac.AppendChild(new AlternateContentFallback());
errors = validator.Validate(ac).Errors;
Assert.Equal(1, errors.Count());
Assert.Equal(ValidationErrorType.MarkupCompatibility, errors.First().ErrorType);
Assert.Equal("Sch_InvalidElementContentExpectingComplex", errors.First().Id);
ac.RemoveChild(ac.LastChild);
ac.RemoveChild(acFallback);
// Error case: wrong sequence
ac.PrependChild(acFallback);
errors = validator.Validate(ac).Errors;
Assert.Equal(1, errors.Count());
Assert.Equal(ValidationErrorType.MarkupCompatibility, errors.First().ErrorType);
Assert.Equal("Sch_IncompleteContentExpectingComplex", errors.First().Id);
ac.RemoveChild(acFallback);
ac.Append(acFallback);
var langAttribute = new OpenXmlAttribute("xml:lang", "http://www.w3.org/XML/1998/namespace", "en-us");
ac.SetAttribute(langAttribute);
errors = validator.Validate(ac).Errors;
Assert.Equal(1, errors.Count());
Assert.Equal(ValidationErrorType.MarkupCompatibility, errors.First().ErrorType);
Assert.Equal("MC_InvalidXmlAttribute", errors.First().Id);
Assert.Equal( "The AlternateContent element should not have an xml:lang or xml:space attribute.", errors[0].Description);
ac.RemoveAttribute(langAttribute.LocalName, langAttribute.NamespaceUri);
ac.FirstChild.SetAttribute(langAttribute);
errors = validator.Validate(ac).Errors;
Assert.Equal(1, errors.Count());
Assert.Same(ac.FirstChild, errors[0].Node);
Assert.Equal(ValidationErrorType.MarkupCompatibility, errors.First().ErrorType);
Assert.Equal("MC_InvalidXmlAttribute", errors.First().Id);
Assert.Equal("The Choice element should not have an xml:lang or xml:space attribute.", errors[0].Description);
ac.FirstChild.RemoveAttribute(langAttribute.LocalName, langAttribute.NamespaceUri);
ac.LastChild.SetAttribute(langAttribute);
errors = validator.Validate(ac).Errors;
Assert.Equal(1, errors.Count());
Assert.Same(ac.LastChild, errors[0].Node);
Assert.Equal(ValidationErrorType.MarkupCompatibility, errors.First().ErrorType);
Assert.Equal("MC_InvalidXmlAttribute", errors.First().Id);
Assert.Equal("The Fallback element should not have an xml:lang or xml:space attribute.", errors[0].Description);
ac.LastChild.RemoveAttribute(langAttribute.LocalName, langAttribute.NamespaceUri);
AlternateContentChoice choice1 = ac.FirstChild as AlternateContentChoice;
choice1.Requires = "o17 o15";
errors = validator.Validate(ac).Errors;
Assert.Equal(1, errors.Count());
Assert.Equal(ValidationErrorType.MarkupCompatibility, errors.First().ErrorType);
Assert.Same(choice1, errors[0].Node);
Assert.Equal("MC_InvalidRequiresAttribute", errors.First().Id);
Assert.Equal("The Requires attribute is invalid - The value 'o17 o15' contains an invalid prefix that is not defined.", errors[0].Description);
choice1.Requires = null;
errors = validator.Validate(ac).Errors;
Assert.Equal(ValidationErrorType.MarkupCompatibility, errors.First().ErrorType);
Assert.Equal(1, errors.Count());
Assert.Same(choice1, errors[0].Node);
Assert.Equal("MC_MissedRequiresAttribute", errors.First().Id);
Assert.Equal("All Choice elements must have a Requires attribute whose value contains a whitespace delimited list of namespace prefixes.", errors[0].Description);
}
示例10: GenerateBL
//.........这里部分代码省略.........
.Elements<TableCell>().ElementAt(1)
.Elements<Paragraph>().FirstOrDefault()
.Elements<SdtRun>().Where
(r => r.SdtProperties.GetFirstChild<Tag>().Val == ConstantBE.APPROVEDDATE).Single();
//set the approved date text
approvedDateTagRR.Descendants<Text>().Single().Text = blInformation.ApprovedDate.ToString("dd/MM/yyyy");
#endregion
#region Row 3
SdtRun referenceTagRR = infoBodyTable.Elements<TableRow>().ElementAt(2)
.Elements<TableCell>().ElementAt(0)
.Elements<Paragraph>().FirstOrDefault()
.Elements<SdtRun>().Where
(r => r.SdtProperties.GetFirstChild<Tag>().Val == ConstantBE.REFERENCE).Single();
//remove the vertical bar with blank space and set the reference text
referenceTagRR.Descendants<Text>().Single().Text = blInformation.Reference.Replace('|', ' ');
// if you want the each reference on each line just uncomment the following codes
/*
//split the string
String[] splitedReference = blInformation.Reference.Split('|');
//set the reference text
foreach (String item in splitedReference)
{
Run newRun = new Run();
//add a new line and the append the item
newRun.AppendChild(new Break());
newRun.AppendChild(new Text(item));
referenceTagRR.SdtContentRun.Append(newRun);
}
*/
#endregion
#region Row 4
//find the reference description tag
SdtRun referenceShortDescTagRR = infoBodyTable.Elements<TableRow>().ElementAt(3)
.Elements<TableCell>().ElementAt(0)
.Elements<Paragraph>().FirstOrDefault()
.Elements<SdtRun>().Where
(r => r.SdtProperties.GetFirstChild<Tag>().Val == ConstantBE.REFERENCESHORTDESC).Single();
//clear the tag
referenceShortDescTagRR.Descendants<Text>().Single().Text = " ";
//split the string
String[] splitedRefDescription = blInformation.ReferenceDescription.Split('|');
//set the reference short description text
foreach (String item in splitedRefDescription)
{
Run newRun = new Run();
//add a new line and the append the item
newRun.AppendChild(new Break());
newRun.AppendChild(new Text(item));
示例11: TestSplitRunAtIndex_TruncatesOriginalRun
public void TestSplitRunAtIndex_TruncatesOriginalRun()
{
Run target = new Run();
Paragraph paragraph = new Paragraph();
paragraph.AppendChild<Run>(target);
target.AppendChild<Text>(new Text("This is a test string."));
OxmlDocument.SplitRunAtIndex(target, 15);
string expected = "This is a test ";
string actual = target.InnerText;
Assert.AreEqual(expected, actual);
}
示例12: TestSplitRunAtIndex_DoesNothingIfIndexIsZero
public void TestSplitRunAtIndex_DoesNothingIfIndexIsZero()
{
Run target = new Run();
Paragraph paragraph = new Paragraph();
paragraph.AppendChild<Run>(target);
target.AppendChild<Text>(new Text("This is a test string."));
string expected = paragraph.OuterXml;
OxmlDocument.SplitRunAtIndex(target, 0);
string actual = paragraph.OuterXml;
Assert.AreEqual(expected, actual);
}
示例13: TestRemoveRunTextBeforeIndex_MidOfText
public void TestRemoveRunTextBeforeIndex_MidOfText()
{
Run target = new Run();
target.AppendChild<Text>(new Text("This is "));
target.AppendChild<Text>(new Text("a test string"));
target.AppendChild<Text>(new Text(" in 3 parts."));
OxmlDocument.RemoveRunTextBeforeIndex(target, 15);
string expected = "string in 3 parts.";
string actual = target.InnerText;
Assert.AreEqual(expected, actual);
}
示例14: TestBookmarkRange_HandlesNestedRuns
public void TestBookmarkRange_HandlesNestedRuns()
{
OxmlDocument doc = new OxmlDocument(this.oxml);
doc._paragraphs[0] = new Paragraph();
Run run1 = new Run();
Run run2 = new Run();
Hyperlink hyp = new Hyperlink();
hyp.AppendChild<Run>(run2);
doc._paragraphs[0].AppendChild<Run>(run1);
doc._paragraphs[0].AppendChild<Hyperlink>(hyp);
run1.AppendChild<Text>(new Text("This is a "));
run2.AppendChild<Text>(new Text("test string."));
doc.BookmarkRange(new OxmlRange_Accessor(0, 4, 15), "testbookmark");
Assert.AreEqual(4, doc._paragraphs[0].Descendants<Run>().ToArray().Length);
Assert.AreEqual(1, doc._paragraphs[0].Descendants<BookmarkStart>().ToArray().Length);
Assert.AreEqual(1, doc._paragraphs[0].Descendants<BookmarkEnd>().ToArray().Length);
}
示例15: CreateRow
//This method allows me to create either a row full of text cells or a row of text cells and a last row with a drawing
TableRow CreateRow(string[] cellText)
{
TableRow tr = new TableRow();
//create cells with simple text
foreach (string s in cellText) {
TableCell tc = new TableCell();
Paragraph p = new Paragraph();
Run r = new Run();
Text t = new Text(s);
r.AppendChild(t);
p.AppendChild(r);
tc.AppendChild(p);
tr.AppendChild(tc);
}
return tr;
}