2009-12-21 14 views
12

ओपनएक्सएमएल एसडीके, 2.0 सीटीपी का उपयोग करके, मैं प्रोग्रामेटिक रूप से वर्ड डॉक्यूमेंट बनाने की कोशिश कर रहा हूं। मेरे दस्तावेज़ में मुझे बुलेट सूची डालना है, सूची के कुछ तत्वों को रेखांकित किया जाना चाहिए। मैं यह कैसे कर सकता हूँ?ओपनएक्सएमएल 2 एसडीके - वर्ड डॉक्यूमेंट - बुलेटेड सूची प्रोग्रामेटिक रूप से

उत्तर

24

ओपनएक्सएमएल में सूचियां थोड़ा उलझन में हैं।

नंबरिंग डिफिनिशन पार्ट जो दस्तावेज़ में सभी सूचियों का वर्णन करता है। इसमें जानकारी शामिल है कि सूचियों को कैसे दिखाना चाहिए (बुलेट, क्रमांकित, आदि) और प्रत्येक को असाइन और आईडी भी निर्दिष्ट करें।

फिर MainDocumentPart में, सूची आप बनाना चाहते हैं में हर आइटम के लिए, आप एक नया पैरा जोड़ सकते हैं और सूची आपको लगता है कि पैरा करना चाहते हैं उसका पहचान प्रदान करेगा।

  • हैलो,
  • दुनिया:

    तो जैसे एक गोली सूची बनाने के लिए!

आपको सबसे पहले एक NumberingDefinitionsPart बनाना होगा:,

NumberingDefinitionsPart numberingPart = 
    mainDocumentPart.AddNewPart<NumberingDefinitionsPart>("someUniqueIdHere"); 

Numbering element = 
    new Numbering(
    new AbstractNum(
     new Level(
     new NumberingFormat() {Val = NumberFormatValues.Bullet}, 
     new LevelText() {Val = "·"} 
    ) {LevelIndex = 0} 
    ){AbstractNumberId = 1}, 
    new NumberingInstance(
     new AbstractNumId(){Val = 1} 
    ){NumberID = 1}); 

element.Save(numberingPart); 

तो फिर तुम MainDocumentPart सामान्य रूप से, बनाने के पैरा गुण को छोड़कर नंबर पहचान प्रदान करेगा:

MainDocumentPart mainDocumentPart = 
    package.AddMainDocumentPart(); 

Document element = 
    new Document(
    new Body(
     new Paragraph(
     new ParagraphProperties(
      new NumberingProperties(
      new NumberingLevelReference(){ Val = 0 }, 
      new NumberingId(){ Val = 1 })), 
     new Run(
      new RunProperties(), 
      new Text("Hello, "){ Space = "preserve" })), 
     new Paragraph(
     new ParagraphProperties(
      new NumberingProperties(
      new NumberingLevelReference(){ Val = 0 }, 
      new NumberingId(){ Val = 1 })), 
     new Run(
      new RunProperties(), 
      new Text("world!"){ Space = "preserve" })))); 

element.Save(mainDocumentPart); 

धारा 2.9 में OpenXML reference guide में उपलब्ध विकल्पों का एक बेहतर स्पष्टीकरण है।

3

एडम जवाब ऊपर को छोड़कर सही है साथ ही नए NumberingInstance (नए अंक के बजाय (एक टिप्पणी में बताया गया है है।

, यदि आप एकाधिक सूचियों है, तो आप कई नंबरिंग तत्वों होना चाहिए (अपने आप आईडी के साथ प्रत्येक उदाहरण के लिए 1, 2, 3 इत्यादि - दस्तावेज़ में प्रत्येक सूची के लिए एक। यह बुलेट सूचियों में कोई समस्या नहीं प्रतीत होता है, लेकिन क्रमांकित सूचियां समान संख्या अनुक्रम का उपयोग जारी रखेगी (जैसा कि 1 से फिर से शुरू करने के विपरीत) । क्योंकि यह लगता है कि यह एक ही सूची है NumberingId इस तरह अपने पैराग्राफ में संदर्भित किया जाना है:

ParagraphProperties paragraphProperties1 = new ParagraphProperties(); 
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "ListParagraph" }; 
NumberingProperties numberingProperties1 = new NumberingProperties(); 
NumberingLevelReference numberingLevelReference1 = new NumberingLevelReference() { Val = 0 }; 

NumberingId numberingId1 = new NumberingId(){ Val = 1 }; //Val is 1, 2, 3 etc based on your numberingid in your numbering element 
numberingProperties1.Append(numberingLevelReference1); 
numberingProperties1.Append(numberingId1); 
paragraphProperties1.Append(paragraphStyleId1); 
paragraphProperties1.Append(numberingProperties1); 

स्तर तत्व के बच्चे बुलेट के प्रकार, और इंडेंटेशन पर असर डालेंगे। जब तक मैं भी स्तर तत्व को यह तत्व जोड़े

new NumberingSymbolRunProperties(
    new RunFonts() { Hint = FontTypeHintValues.Default, Ascii = "Symbol", HighAnsi = "Symbol" }) 

इंडेंटेशन एक समस्या थी: जब तक मैं स्तर तत्व को यह जोड़ा मेरे गोलियों बहुत छोटा था

new PreviousParagraphProperties(
    new Indentation() { Left = "864", Hanging = "360" }) 
3

और आप की तरह कर रहे हैं मुझे - एक टेम्पलेट से एक दस्तावेज़ बनाने, तो आप दोनों परिस्थितियों से निपटने के लिए इस कोड का उपयोग करने के लिए कर सकते हैं - जब अपने टेम्पलेट करता है या किसी नंबरिंग परिभाषाएँ शामिल नहीं है:

// Introduce bulleted numbering in case it will be needed at some point 
NumberingDefinitionsPart numberingPart = document.MainDocumentPart.NumberingDefinitionsPart; 
if (numberingPart == null) 
{ 
    numberingPart = document.MainDocumentPart.AddNewPart<NumberingDefinitionsPart>("NumberingDefinitionsPart001"); 
} 
7

मुझे ऐसा कुछ चाहिए जो मुझे दस्तावेज़ में एक से अधिक बुलेट सूची जोड़ने की अनुमति देगा। थोड़ी देर के लिए मेरे डेस्क के खिलाफ अपने सिर को टक्कर देने के बाद, मैंने विभिन्न पदों का एक समूह गठबंधन करने और ओपन एक्सएमएल एसडीके 2 के साथ अपने दस्तावेज़ की जांच करने में कामयाब रहे।0 उत्पादकता उपकरण और कुछ सामान निकाला। यह दस्तावेज़ अब उत्पादित करता है जो एसडीके उत्पादकता उपकरण के संस्करण 2.0 और 2.5 द्वारा सत्यापन पास करता है।

यहां कोड है; उम्मीद है कि यह किसी को कुछ समय और उत्तेजना बचाता है।

उपयोग:

const string fileToCreate = "C:\\temp\\bulletTest.docx"; 

if (File.Exists(fileToCreate)) 
    File.Delete(fileToCreate); 

var writer = new SimpleDocumentWriter(); 
List<string> fruitList = new List<string>() { "Apple", "Banana", "Carrot"}; 
writer.AddBulletList(fruitList); 
writer.AddParagraph("This is a spacing paragraph 1."); 

List<string> animalList = new List<string>() { "Dog", "Cat", "Bear" }; 
writer.AddBulletList(animalList); 
writer.AddParagraph("This is a spacing paragraph 2."); 

List<string> stuffList = new List<string>() { "Ball", "Wallet", "Phone" }; 
writer.AddBulletList(stuffList); 
writer.AddParagraph("Done."); 

writer.SaveToFile(fileToCreate); 

का उपयोग करते हुए बयान:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using DocumentFormat.OpenXml; 
using DocumentFormat.OpenXml.Packaging; 
using DocumentFormat.OpenXml.Wordprocessing;  

कोड

public class SimpleDocumentWriter : IDisposable 
{ 
    private MemoryStream _ms; 
    private WordprocessingDocument _wordprocessingDocument; 

    public SimpleDocumentWriter() 
    { 
     _ms = new MemoryStream(); 
     _wordprocessingDocument = WordprocessingDocument.Create(_ms, WordprocessingDocumentType.Document); 
     var mainDocumentPart = _wordprocessingDocument.AddMainDocumentPart(); 
     Body body = new Body(); 
     mainDocumentPart.Document = new Document(body); 
    } 

    public void AddParagraph(string sentence) 
    { 
     List<Run> runList = ListOfStringToRunList(new List<string> { sentence}); 
     AddParagraph(runList); 
    } 
    public void AddParagraph(List<string> sentences) 
    { 
     List<Run> runList = ListOfStringToRunList(sentences); 
     AddParagraph(runList); 
    } 

    public void AddParagraph(List<Run> runList) 
    { 
     var para = new Paragraph(); 
     foreach (Run runItem in runList) 
     { 
      para.AppendChild(runItem); 
     } 

     Body body = _wordprocessingDocument.MainDocumentPart.Document.Body; 
     body.AppendChild(para); 
    } 

    public void AddBulletList(List<string> sentences) 
    { 
     var runList = ListOfStringToRunList(sentences); 

     AddBulletList(runList); 
    } 


    public void AddBulletList(List<Run> runList) 
    { 
     // Introduce bulleted numbering in case it will be needed at some point 
     NumberingDefinitionsPart numberingPart = _wordprocessingDocument.MainDocumentPart.NumberingDefinitionsPart; 
     if (numberingPart == null) 
     { 
      numberingPart = _wordprocessingDocument.MainDocumentPart.AddNewPart<NumberingDefinitionsPart>("NumberingDefinitionsPart001"); 
      Numbering element = new Numbering(); 
      element.Save(numberingPart); 
     } 

     // Insert an AbstractNum into the numbering part numbering list. The order seems to matter or it will not pass the 
     // Open XML SDK Productity Tools validation test. AbstractNum comes first and then NumberingInstance and we want to 
     // insert this AFTER the last AbstractNum and BEFORE the first NumberingInstance or we will get a validation error. 
     var abstractNumberId = numberingPart.Numbering.Elements<AbstractNum>().Count() + 1; 
     var abstractLevel = new Level(new NumberingFormat() {Val = NumberFormatValues.Bullet}, new LevelText() {Val = "·"}) {LevelIndex = 0}; 
     var abstractNum1 = new AbstractNum(abstractLevel) {AbstractNumberId = abstractNumberId}; 

     if (abstractNumberId == 1) 
     { 
      numberingPart.Numbering.Append(abstractNum1); 
     } 
     else 
     { 
      AbstractNum lastAbstractNum = numberingPart.Numbering.Elements<AbstractNum>().Last(); 
      numberingPart.Numbering.InsertAfter(abstractNum1, lastAbstractNum); 
     } 

     // Insert an NumberingInstance into the numbering part numbering list. The order seems to matter or it will not pass the 
     // Open XML SDK Productity Tools validation test. AbstractNum comes first and then NumberingInstance and we want to 
     // insert this AFTER the last NumberingInstance and AFTER all the AbstractNum entries or we will get a validation error. 
     var numberId = numberingPart.Numbering.Elements<NumberingInstance>().Count() + 1; 
     NumberingInstance numberingInstance1 = new NumberingInstance() {NumberID = numberId}; 
     AbstractNumId abstractNumId1 = new AbstractNumId() {Val = abstractNumberId}; 
     numberingInstance1.Append(abstractNumId1); 

     if (numberId == 1) 
     { 
      numberingPart.Numbering.Append(numberingInstance1); 
     } 
     else 
     { 
      var lastNumberingInstance = numberingPart.Numbering.Elements<NumberingInstance>().Last(); 
      numberingPart.Numbering.InsertAfter(numberingInstance1, lastNumberingInstance); 
     } 

     Body body = _wordprocessingDocument.MainDocumentPart.Document.Body; 

     foreach (Run runItem in runList) 
     { 
      // Create items for paragraph properties 
      var numberingProperties = new NumberingProperties(new NumberingLevelReference() {Val = 0}, new NumberingId() {Val = numberId}); 
      var spacingBetweenLines1 = new SpacingBetweenLines() { After = "0" }; // Get rid of space between bullets 
      var indentation = new Indentation() { Left = "720", Hanging = "360" }; // correct indentation 

      ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties(); 
      RunFonts runFonts1 = new RunFonts() { Ascii = "Symbol", HighAnsi = "Symbol" }; 
      paragraphMarkRunProperties1.Append(runFonts1); 

      // create paragraph properties 
      var paragraphProperties = new ParagraphProperties(numberingProperties, spacingBetweenLines1, indentation, paragraphMarkRunProperties1); 

      // Create paragraph 
      var newPara = new Paragraph(paragraphProperties); 

      // Add run to the paragraph 
      newPara.AppendChild(runItem); 

      // Add one bullet item to the body 
      body.AppendChild(newPara); 
     } 
    } 


    public void Dispose() 
    { 
     CloseAndDisposeOfDocument(); 
     if (_ms != null) 
     { 
      _ms.Dispose(); 
      _ms = null; 
     } 
    } 

    public MemoryStream SaveToStream() 
    { 
     _ms.Position = 0; 
     return _ms; 
    } 

    public void SaveToFile(string fileName) 
    { 
     if (_wordprocessingDocument != null) 
     { 
      CloseAndDisposeOfDocument(); 
     } 

     if (_ms == null) 
      throw new ArgumentException("This object has already been disposed of so you cannot save it!"); 

     using (var fs = File.Create(fileName)) 
     { 
      _ms.WriteTo(fs); 
     } 
    } 

    private void CloseAndDisposeOfDocument() 
    { 
     if (_wordprocessingDocument != null) 
     { 
      _wordprocessingDocument.Close(); 
      _wordprocessingDocument.Dispose(); 
      _wordprocessingDocument = null; 
     } 
    } 

    private static List<Run> ListOfStringToRunList(List<string> sentences) 
    { 
     var runList = new List<Run>(); 
     foreach (string item in sentences) 
     { 
      var newRun = new Run(); 
      newRun.AppendChild(new Text(item)); 
      runList.Add(newRun); 
     } 

     return runList; 
    } 
} 
+0

मुझे दिखा कैसे एक दस्तावेज़ में सूची जोड़ने के लिए के लिए बहुत बहुत शुक्रिया। मैं नंबरिंग का उपयोग कर रहा था। ऐपेंड (abstactNum, numberingInstance) और यह समझ में नहीं आया कि यह – Dan

+0

क्यों काम नहीं करता है: उस के लिए धन्यवाद साथी! जैसे सचमुच ! :) –

+0

@ मैरियस कॉनजेड आपका स्वागत है। मैंने सरल शब्द दस्तावेज़ लिखने के लिए कुछ बनाया है जिसका उपयोग करने के लिए आपका स्वागत है (https://github.com/madcodemonkey/SimpleDocument.OpenXML देखें) –

संबंधित मुद्दे