本文整理汇总了C#中Paragraph.Elements方法的典型用法代码示例。如果您正苦于以下问题:C# Paragraph.Elements方法的具体用法?C# Paragraph.Elements怎么用?C# Paragraph.Elements使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Paragraph
的用法示例。
在下文中一共展示了Paragraph.Elements方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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 };
}
示例2: 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());
}
}
示例3: ParagraphIndexer
public ParagraphIndexer(Paragraph toIndex)
{
if (toIndex == null)
{
throw new ArgumentNullException("paragraph");
}
this.Paragraph = toIndex;
this.Properties = toIndex.Elements<ParagraphProperties>().FirstOrDefault();
// Process runs.
foreach (var run in toIndex.Elements<Run>())
{
this.runs.Add(new RunIndexer(run));
}
}
示例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: 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;
}
}
示例6: 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;
}
}
示例7: 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;
}
示例8: AddParagraph
/// <summary>
/// Add paragraph
/// </summary>
/// <param name="paragraph"></param>
/// <returns></returns>
private string AddParagraph(Paragraph paragraph)
{
try
{
string paragraphTemplate = "<p class=STYLE>PARAGRAPH_CONTENT</p>";
string paragraphStyle = paragraph.ParagraphProperties != null ? AddFormatParagraph(paragraph.ParagraphProperties) : "\"\"";
string paragraphContent = "";
foreach (OpenXmlElement element in paragraph.Elements())
if (element is Run)
paragraphContent += AddRun((Run)element);
else if (element is Hyperlink)
paragraphContent += AddHyperlink((Hyperlink)element);
return paragraphTemplate.Replace("STYLE", paragraphStyle)
.Replace("PARAGRAPH_CONTENT", paragraphContent == "" ? " " : paragraphContent) + Environment.NewLine;
}
catch (Exception ex)
{
throw ex;
}
}