本文整理汇总了C#中Paragraph.PrependChild方法的典型用法代码示例。如果您正苦于以下问题:C# Paragraph.PrependChild方法的具体用法?C# Paragraph.PrependChild怎么用?C# Paragraph.PrependChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Paragraph
的用法示例。
在下文中一共展示了Paragraph.PrependChild方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateParagraphPropertiesIfNonExists
internal static void CreateParagraphPropertiesIfNonExists(Paragraph paragraph)
{
// If the paragraph has no ParagraphProperties object, create one.
if (paragraph.Elements<ParagraphProperties>().Count() == 0) {
paragraph.PrependChild<ParagraphProperties>(new ParagraphProperties());
}
}
示例2: Main
static void Main(string[] args)
{
string FilePath = @"..\..\..\..\Sample Files\";
string File = FilePath + "Create and add a paragraph style - OpenXML.docx";
using (WordprocessingDocument doc =
WordprocessingDocument.Open(File, true))
{
// Get the Styles part for this document.
StyleDefinitionsPart part =
doc.MainDocumentPart.StyleDefinitionsPart;
// If the Styles part does not exist, add it and then add the style.
if (part == null)
{
part = AddStylesPartToPackage(doc);
}
// Set up a variable to hold the style ID.
string parastyleid = "OverdueAmountPara";
// Create and add a paragraph style to the specified styles part
// with the specified style ID, style name and aliases.
CreateAndAddParagraphStyle(part,
parastyleid,
"Overdue Amount Para",
"Late Due, Late Amount");
// Add a paragraph with a run and some text.
Paragraph p =
new Paragraph(
new Run(
new Text("This is some text in a run in a paragraph.")));
// Add the paragraph as a child element of the w:body element.
doc.MainDocumentPart.Document.Body.AppendChild(p);
// If the paragraph has no ParagraphProperties object, create one.
if (p.Elements<ParagraphProperties>().Count() == 0)
{
p.PrependChild<ParagraphProperties>(new ParagraphProperties());
}
// Get a reference to the ParagraphProperties object.
ParagraphProperties pPr = p.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 = parastyleid;
}
}
示例3: ApplyStyleToParagraph
// Apply a style to a paragraph.
public static void ApplyStyleToParagraph(StyleDefinitionsPart part,
string styleId, Paragraph p)
{
// If the paragraph has no ParagraphProperties object, create one.
if (p.Elements<ParagraphProperties>().Count() == 0)
{
p.PrependChild<ParagraphProperties>(new ParagraphProperties());
}
// Get the paragraph properties element of the paragraph.
ParagraphProperties pPr = p.Elements<ParagraphProperties>().First();
// Set the style of the paragraph.
pPr.ParagraphStyleId = new ParagraphStyleId() { Val = styleId };
}
示例4: ApplyStyleToParagraph
// Apply a style to a paragraph.
public void ApplyStyleToParagraph(WordprocessingDocument doc, string styleid, string stylename, Paragraph p)
{
// If the paragraph has no ParagraphProperties object, create one.
if (p.Elements<ParagraphProperties>().Count() == 0)
{
p.PrependChild(new ParagraphProperties());
}
// Get the paragraph properties element of the paragraph.
ParagraphProperties pPr = p.Elements<ParagraphProperties>().First();
// Get the Styles part for this document.
StyleDefinitionsPart part =
doc.MainDocumentPart.StyleDefinitionsPart;
// If the Styles part does not exist, add it and then add the style.
if (part == null)
{
part = this.AddStylesPartToPackage(doc);
AddNewStyle(part, styleid, stylename);
}
else
{
// If the style is not in the document, add it.
if (this.IsStyleIdInDocument(doc, styleid) != true)
{
// No match on styleid, so let's try style name.
string styleidFromName = this.GetStyleIdFromStyleName(doc, stylename);
if (styleidFromName == null)
{
AddNewStyle(part, styleid, stylename);
}
else
{
styleid = styleidFromName;
}
}
}
// Set the style of the paragraph.
pPr.ParagraphStyleId = new ParagraphStyleId { Val = styleid };
}
示例5: Test_03
public static void Test_03()
{
//Test_OpenXml_Style.SetDirectory();
string file = "test_01.docx";
Trace.WriteLine("create docx \"{0}\" using OXmlDoc", file);
// from How to: Create and add a paragraph style to a word processing document (Open XML SDK) https://msdn.microsoft.com/fr-fr/library/office/gg188062.aspx
using (WordprocessingDocument doc = WordprocessingDocument.Open(file, true))
{
// Get the Styles part for this document.
StyleDefinitionsPart part = doc.MainDocumentPart.StyleDefinitionsPart;
// If the Styles part does not exist, add it and then add the style.
if (part == null)
{
part = AddStylesPartToPackage(doc);
}
// Set up a variable to hold the style ID.
string parastyleid = "OverdueAmountPara";
// Create and add a paragraph style to the specified styles part
// with the specified style ID, style name and aliases.
CreateAndAddParagraphStyle(part, parastyleid, "Overdue Amount Para", "Late Due, Late Amount");
// Add a paragraph with a run and some text.
//Paragraph p = new Paragraph(new Run(new Text("This is some text in a run in a paragraph.")));
Paragraph p = new Paragraph(new Run(new DocumentFormat.OpenXml.Wordprocessing.Text("This is some text in a run in a paragraph.")));
// Add the paragraph as a child element of the w:body element.
doc.MainDocumentPart.Document.Body.AppendChild(p);
// If the paragraph has no ParagraphProperties object, create one.
if (p.Elements<ParagraphProperties>().Count() == 0)
{
p.PrependChild<ParagraphProperties>(new ParagraphProperties());
}
// Get a reference to the ParagraphProperties object.
ParagraphProperties pPr = p.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 = parastyleid;
}
}
示例6: setStyle
//public static StyleDefinitionsPart getStyles(string path)
//{
// try
// {
// StyleDefinitionsPart part = new StyleDefinitionsPart();
// using (WordprocessingDocument wordTemplate = WordprocessingDocument.Open(path, true))
// {
// foreach (var templateStyle in wordTemplate.MainDocumentPart.StyleDefinitionsPart.Styles)
// {
// part.Styles.Append(templateStyle.CloneNode(true));
// }
// }
// return part;
// }
// catch (Exception)
// {
// return null;
// }
//}
// Set new Style for paragraph.
public static void setStyle(WordprocessingDocument doc, Paragraph p, StyleRunProperties prop, string id, string stylename, string alias)
{
// Get the Styles part for this document. If it does not exist create one
StyleDefinitionsPart part = doc.MainDocumentPart.StyleDefinitionsPart;
if (part == null) { part = AddStylesPartToPackage(doc); }
CreateAndAddParagraphStyle(prop, part, id, stylename, alias);
// If the paragraph has no ParagraphProperties object, create one.
if (p.Elements<ParagraphProperties>().Count() == 0)
{
p.PrependChild<ParagraphProperties>(new ParagraphProperties());
}
// Get a reference to the ParagraphProperties object.
ParagraphProperties pPr = p.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 = id;
}
示例7: GetXPathTest
public void GetXPathTest()
{
XmlPath actual;
Paragraph p = new Paragraph(new Run(), new BookmarkEnd(), new Run());
Body body = new Body(p);
actual = XmlPath.GetXPath(p);
Assert.Null(actual.PartUri);
Assert.Equal(@"/w:body[1]/w:p[1]", actual.XPath);
Assert.Equal(1, actual.NamespacesDefinitions.Count);
Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]);
actual = XmlPath.GetXPath(p.FirstChild);
Assert.Null(actual.PartUri);
Assert.Equal(@"/w:body[1]/w:p[1]/w:r[1]", actual.XPath);
Assert.Equal(1, actual.NamespacesDefinitions.Count);
Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]);
body.PrependChild(new Paragraph());
actual = XmlPath.GetXPath(p.FirstChild.NextSibling());
Assert.Null(actual.PartUri);
Assert.Equal(@"/w:body[1]/w:p[2]/w:bookmarkEnd[1]", actual.XPath);
Assert.Equal(1, actual.NamespacesDefinitions.Count);
Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]);
var r = p.LastChild;
actual = XmlPath.GetXPath(r);
Assert.Null(actual.PartUri);
Assert.Equal(@"/w:body[1]/w:p[2]/w:r[2]", actual.XPath);
Assert.Equal(1, actual.NamespacesDefinitions.Count);
Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]);
var unknown1 = p.PrependChild(new OpenXmlUnknownElement("my:test", "http://my"));
actual = XmlPath.GetXPath(unknown1);
Assert.Null(actual.PartUri);
Assert.Equal(@"/w:body[1]/w:p[2]/my:test[1]", actual.XPath);
Assert.Equal(2, actual.NamespacesDefinitions.Count);
Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]);
Assert.Equal(@"xmlns:my=""http://my""", actual.NamespacesDefinitions[1]);
var unknown2 = p.AppendChild(new OpenXmlUnknownElement("my:test", "http://my"));
actual = XmlPath.GetXPath(unknown2);
Assert.Null(actual.PartUri);
Assert.Equal(@"/w:body[1]/w:p[2]/my:test[2]", actual.XPath);
Assert.Equal(2, actual.NamespacesDefinitions.Count);
Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]);
Assert.Equal(@"xmlns:my=""http://my""", actual.NamespacesDefinitions[1]);
var miscNode = p.AppendChild(new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, "<!-- comment -->"));
actual = XmlPath.GetXPath(miscNode);
Assert.Null(actual.PartUri);
Assert.Equal(@"/w:body[1]/w:p[2]/<!-- comment -->", actual.XPath);
Assert.Equal(1, actual.NamespacesDefinitions.Count);
Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]);
actual = XmlPath.GetXPath(r);
Assert.Null(actual.PartUri);
Assert.Equal(@"/w:body[1]/w:p[2]/w:r[2]", actual.XPath);
Assert.Equal(1, actual.NamespacesDefinitions.Count);
Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]);
using (var stream = new MemoryStream())
using (var doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document))
{
doc.AddMainDocumentPart();
doc.MainDocumentPart.Document = new Document(body);
actual = XmlPath.GetXPath(r);
Assert.Equal(doc.MainDocumentPart.Uri, actual.PartUri);
Assert.Equal(new Uri("/word/document.xml", UriKind.Relative), actual.PartUri);
Assert.Equal(@"/w:document[1]/w:body[1]/w:p[2]/w:r[2]", actual.XPath);
Assert.Equal(1, actual.NamespacesDefinitions.Count);
Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]);
}
}
示例8: CreateTableCell
private TableCell CreateTableCell(string contents, string cell_color = "ffffff", int width = 0)
{
// Create the TableCell object
TableCell tc = new TableCell();
TableCellProperties tcp;
if (width == 0)
{
// Create the TableCellProperties object
tcp = new TableCellProperties(
new TableCellWidth { Type = TableWidthUnitValues.Auto }
);
}
else
{
tcp = new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = width.ToString() }
);
}
// Create the Shading object
DocumentFormat.OpenXml.Wordprocessing.Shading shading =
new DocumentFormat.OpenXml.Wordprocessing.Shading()
{
Color = "auto",
Fill = cell_color,
Val = ShadingPatternValues.Clear
};
// Add the Shading object to the TableCellProperties object
tcp.Append(shading);
// Add the TableCellProperties object to the TableCell object
tc.PrependChild<TableCellProperties>(tcp);
// also need to ensure you include the text, otherwise it causes an error (it did for me!)
Text text = new Text(contents);
// Specify the table cell content.
Run run = new Run();
RunProperties runProp = new RunProperties(); // Create run properties.
RunFonts runFont = new RunFonts(); // Create font
runFont.Ascii = "Arial"; // Specify font family
FontSize size = new FontSize();
size.Val = new StringValue("22"); // 48 half-point font size
runProp.Append(runFont);
runProp.Append(size);
run.PrependChild<RunProperties>(runProp);
run.Append(text);
ParagraphProperties pProperties = new ParagraphProperties();
ContextualSpacing cs = new ContextualSpacing();
pProperties.Append(cs);
Paragraph paragraph = new Paragraph(run);
paragraph.PrependChild<ParagraphProperties>(pProperties);
tc.Append(paragraph);
return tc;
}