2012-08-02 12 views
5

के साथ तालिका से टेक्स्ट में औचित्य मैं OpenXML के साथ तालिका में तालिका कक्ष में एक टेक्स्ट संरेखण लागू करना चाहता हूं।ओपनएक्सएमएल एसडीके 2.0

मुझे समझ में नहीं आता कि यह क्यों लागू नहीं है।

Table table = new Table(); 
TableRow tableHeader = new TableRow(); 
table.AppendChild<TableRow>(tableHeader); 
TableCell tableCell = new TableCell(); 
tableHeader.AppendChild<TableCell>(tableCell); 
Paragraph paragraph = new Paragraph(new Run(new Text("test"))); 
ParagraphProperties paragraphProperties = new ParagraphProperties(); 
JustificationValues? justification = GetJustificationFromString("centre"); 
if (justification != null) 
{ 
    paragraphProperties.AppendChild<Justification>(new Justification() { Val = justification }); 
} 
paragraph.AppendChild<ParagraphProperties>(paragraphProperties); 
tableCell.AppendChild<Paragraph>(paragraph); 


public static JustificationValues? GetJustificationFromString(string alignment) 
{ 
    switch(alignment) 
    { 
     case "centre" : return JustificationValues.Center; 
     case "droite" : return JustificationValues.Right; 
     case "gauche" : return JustificationValues.Left; 
     default: return null; 
    } 
} 

Thx आपकी मदद के लिए!

+0

ठीक लग रहा है, क्या आपने जस्टिफिकेशन वैल्यूज़ से इस प्रकार को बदलकर कोशिश की? JustificationValues ​​ – Kiru

+0

मैंने इसे किया लेकिन कुछ भी नहीं बदलता – Aelios

उत्तर

14

क्या यह काम करता है यदि आप पैराग्राफ के बजाय पैराग्राफप्रोपर्टीज को मूल कक्ष में लागू करना चाहते हैं?

Table table = new Table(); 
TableRow tableHeader = new TableRow(); 
table.AppendChild<TableRow>(tableHeader); 
TableCell tableCell = new TableCell(); 
tableHeader.AppendChild<TableCell>(tableCell); 
ParagraphProperties paragraphProperties = new ParagraphProperties(); 
Paragraph paragraph = new Paragraph(new Run(new Text("test"))); 
JustificationValues? justification = GetJustificationFromString("centre"); 

// Use System.Nullable<T>.HasValue instead of the null check. 
if (justification.HasValue) 
{ 
    // Using System.Nullable<T>.Value to obtain the value and resolve a warning 
    // that occurs when using 'justification' by itself. 
    paragraphProperties.AppendChild<Justification>(new Justification() { Val = justification.Value }); 
} 

// append the paragraphProperties to the tableCell rather than the paragraph element 
tableCell.AppendChild<ParagraphProperties>(paragraphProperties); 
tableCell.AppendChild<Paragraph>(paragraph); 
Console.WriteLine(table.OuterXml); 

table.OuterXml से पहले:

<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:tr> 
    <w:tc> 
     <w:p> 
     <w:r> 
      <w:t>test</w:t> 
     </w:r> 
     <w:pPr> 
      <w:jc w:val="center" /> 
     </w:pPr> 
     </w:p> 
    </w:tc> 
    </w:tr> 
</w:tbl> 

table.OuterXml के बाद:

<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:tr> 
    <w:tc> 
     <w:pPr> 
     <w:jc w:val="center" /> 
     </w:pPr> 
     <w:p> 
     <w:r> 
      <w:t>test</w:t> 
     </w:r> 
     </w:p> 
    </w:tc> 
    </w:tr> 
</w:tbl> 

मैं काफी OPENXML के लिए नया हूँ। क्या परिणाम किसी शब्द दस्तावेज़ में सहेजा गया है और शब्द में देखा गया है?

+0

अच्छा, अभी ठीक है! धन्यवाद – Aelios

+0

उत्पादकता टूल के साथ खोले जाने पर इसे ओपनएक्सएमएल अज्ञात एलिमेंट के रूप में प्रस्तुत किया जाता है। इस प्रकार पीडीएफ, आदि में परिवर्तित नहीं किया जा सकता है। इसे हल करने का कोई तरीका? –

+0

नई समस्या, कृपया एक अलग धागा पोस्ट करें। – Nicodemeus