| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049 |
- /*************************************************************************************
-
- DocX – DocX is the community edition of Xceed Words for .NET
-
- Copyright (C) 2009-2016 Xceed Software Inc.
-
- This program is provided to you under the terms of the Microsoft Public
- License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
-
- For more features and fast professional support,
- pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
-
- ***********************************************************************************/
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Xml.Linq;
- using System.IO.Packaging;
- using System.IO;
- using System.Drawing;
- using System.Globalization;
- using System.Collections.ObjectModel;
-
- namespace Xceed.Words.NET
- {
- /// <summary>
- /// Represents a Table in a document.
- /// </summary>
- public class Table : InsertBeforeOrAfter
- {
- #region Private Members
-
- private Alignment _alignment;
- private AutoFit _autofit;
- private float[] _columnWidths;
- private TableDesign _design;
- /// <summary>
- /// The custom design\style to apply to this table.
- ///
- /// </summary>
- private string _customTableDesignName;
- private int _cachedColumnCount = -1;
-
- #endregion
-
- #region Public Properties
-
- /// <summary>
- /// Returns a list of all Paragraphs inside this container.
- /// </summary>
- ///
- public virtual List<Paragraph> Paragraphs
- {
- get
- {
- var paragraphs = new List<Paragraph>();
-
- foreach( Row r in Rows )
- paragraphs.AddRange( r.Paragraphs );
-
- return paragraphs;
- }
- }
-
- /// <summary>
- /// Returns a list of all Pictures in a Table.
- /// </summary>
- /// <example>
- /// Returns a list of all Pictures in a Table.
- /// <code>
- /// <![CDATA[
- /// // Create a document.
- /// using (DocX document = DocX.Load(@"Test.docx"))
- /// {
- /// // Get the first Table in a document.
- /// Table t = document.Tables[0];
- ///
- /// // Get all of the Pictures in this Table.
- /// List<Picture> pictures = t.Pictures;
- ///
- /// // Save this document.
- /// document.Save();
- /// }
- /// ]]>
- /// </code>
- /// </example>
- public List<Picture> Pictures
- {
- get
- {
- var pictures = new List<Picture>();
-
- foreach( Row r in Rows )
- {
- pictures.AddRange( r.Pictures );
- }
-
- return pictures;
- }
- }
-
- /// <summary>
- /// Get all of the Hyperlinks in this Table.
- /// </summary>
- /// <example>
- /// Get all of the Hyperlinks in this Table.
- /// <code>
- /// // Create a document.
- /// using (DocX document = DocX.Load(@"Test.docx"))
- /// {
- /// // Get the first Table in this document.
- /// Table t = document.Tables[0];
- ///
- /// // Get a list of all Hyperlinks in this Table.
- /// List<Hyperlink> hyperlinks = t.Hyperlinks;
- ///
- /// // Save this document.
- /// document.Save();
- /// }
- /// </code>
- /// </example>
- public List<Hyperlink> Hyperlinks
- {
- get
- {
- var hyperlinks = new List<Hyperlink>();
-
- foreach( Row r in Rows )
- {
- hyperlinks.AddRange( r.Hyperlinks );
- }
-
- return hyperlinks;
- }
- }
-
- /// <summary>
- /// Returns the number of rows in this table.
- /// </summary>
- public Int32 RowCount
- {
- get
- {
- return this.Xml.Elements( XName.Get( "tr", DocX.w.NamespaceName ) ).Count();
- }
- }
-
- /// <summary>
- /// Returns the number of columns in this table.
- /// </summary>
- public Int32 ColumnCount
- {
- get
- {
- if( this.RowCount == 0 )
- return 0;
- if( _cachedColumnCount == -1 )
- {
- foreach( var r in this.Rows )
- {
- _cachedColumnCount = Math.Max( _cachedColumnCount, r.ColumnCount );
- }
- }
- return _cachedColumnCount;
- }
- }
-
- /// <summary>
- /// Returns a list of rows in this table.
- /// </summary>
- public List<Row> Rows
- {
- get
- {
- var rows =
- (
- from r in Xml.Elements( XName.Get( "tr", DocX.w.NamespaceName ) )
- select new Row( this, Document, r )
- ).ToList();
-
- return rows;
- }
- }
-
- public Alignment Alignment
- {
- get
- {
- return _alignment;
- }
- set
- {
- string alignmentString = string.Empty;
- switch( value )
- {
- case Alignment.left:
- {
- alignmentString = "left";
- break;
- }
-
- case Alignment.both:
- {
- alignmentString = "both";
- break;
- }
-
-
- case Alignment.right:
- {
- alignmentString = "right";
- break;
- }
-
- case Alignment.center:
- {
- alignmentString = "center";
- break;
- }
- }
-
- XElement tblPr = Xml.Descendants( XName.Get( "tblPr", DocX.w.NamespaceName ) ).First();
- XElement jc = tblPr.Descendants( XName.Get( "jc", DocX.w.NamespaceName ) ).FirstOrDefault();
-
- jc?.Remove();
-
- jc = new XElement( XName.Get( "jc", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), alignmentString ) );
- tblPr.Add( jc );
- _alignment = value;
- }
- }
-
- /// <summary>
- /// Auto size this table according to some rule.
- /// </summary>
- public AutoFit AutoFit
- {
- get
- {
- return _autofit;
- }
-
- set
- {
- string tableAttributeValue = string.Empty;
- string columnAttributeValue = string.Empty;
- switch( value )
- {
- case AutoFit.ColumnWidth:
- {
- tableAttributeValue = "auto";
- columnAttributeValue = "dxa";
-
- // Disable "Automatically resize to fit contents" option
- var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- if( tblPr != null )
- {
- var layout = tblPr.Element( XName.Get( "tblLayout", DocX.w.NamespaceName ) );
- if( layout == null )
- {
- tblPr.Add( new XElement( XName.Get( "tblLayout", DocX.w.NamespaceName ) ) );
- layout = tblPr.Element( XName.Get( "tblLayout", DocX.w.NamespaceName ) );
- }
-
- var type = layout.Attribute( XName.Get( "type", DocX.w.NamespaceName ) );
- if( type == null )
- {
- layout.Add( new XAttribute( XName.Get( "type", DocX.w.NamespaceName ), String.Empty ) );
- type = layout.Attribute( XName.Get( "type", DocX.w.NamespaceName ) );
- }
-
- type.Value = "fixed";
- }
-
- break;
- }
-
- case AutoFit.Contents:
- {
- tableAttributeValue = columnAttributeValue = "auto";
- break;
- }
-
- case AutoFit.Window:
- {
- tableAttributeValue = columnAttributeValue = "pct";
- break;
- }
-
- case AutoFit.Fixed:
- {
- tableAttributeValue = columnAttributeValue = "dxa";
- var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- var tblLayout = tblPr.Element( XName.Get( "tblLayout", DocX.w.NamespaceName ) );
-
- if( tblLayout == null )
- {
- var tmp = tblPr.Element( XName.Get( "tblInd", DocX.w.NamespaceName ) ) ?? tblPr.Element( XName.Get( "tblW", DocX.w.NamespaceName ) );
-
- tmp.AddAfterSelf( new XElement( XName.Get( "tblLayout", DocX.w.NamespaceName ) ) );
- tmp = tblPr.Element( XName.Get( "tblLayout", DocX.w.NamespaceName ) );
- tmp.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "fixed" );
- tmp = tblPr.Element( XName.Get( "tblW", DocX.w.NamespaceName ) );
-
- Double totalWidth = 0;
- foreach( Double columnWidth in ColumnWidths )
- {
- totalWidth += columnWidth;
- }
-
- tmp.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), totalWidth.ToString() );
- break;
- }
- else
- {
- var types = from d in Xml.Descendants()
- let type = d.Attribute( XName.Get( "type", DocX.w.NamespaceName ) )
- where ( d.Name.LocalName == "tblLayout" ) && type != null
- select type;
-
- foreach( XAttribute type in types )
- {
- type.Value = "fixed";
- }
-
- var tmp = tblPr.Element( XName.Get( "tblW", DocX.w.NamespaceName ) );
-
- Double totalWidth = 0;
- foreach( Double columnWidth in ColumnWidths )
- {
- totalWidth += columnWidth;
- }
-
- tmp.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), totalWidth.ToString() );
- break;
- }
- }
- }
-
- // Set table attributes
- var query = from d in Xml.Descendants()
- let type = d.Attribute( XName.Get( "type", DocX.w.NamespaceName ) )
- where ( d.Name.LocalName == "tblW" ) && type != null
- select type;
-
- foreach( XAttribute type in query )
- {
- type.Value = tableAttributeValue;
- }
-
- // Set column attributes
- query = from d in Xml.Descendants()
- let type = d.Attribute( XName.Get( "type", DocX.w.NamespaceName ) )
- where ( d.Name.LocalName == "tcW" ) && type != null
- select type;
-
- foreach( XAttribute type in query )
- {
- type.Value = columnAttributeValue;
- }
-
- _autofit = value;
- }
- }
- /// <summary>
- /// The design\style to apply to this table.
- /// </summary>
- public TableDesign Design
- {
- get
- {
- return _design;
- }
- set
- {
- XElement tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- XElement style = tblPr.Element( XName.Get( "tblStyle", DocX.w.NamespaceName ) );
- if( style == null )
- {
- tblPr.Add( new XElement( XName.Get( "tblStyle", DocX.w.NamespaceName ) ) );
- style = tblPr.Element( XName.Get( "tblStyle", DocX.w.NamespaceName ) );
- }
-
- XAttribute val = style.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
- if( val == null )
- {
- style.Add( new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), "" ) );
- val = style.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
- }
-
- _design = value;
-
- if( _design == TableDesign.None )
- {
- if( style != null )
- style.Remove();
- }
-
- if( _design != TableDesign.Custom )
- {
- switch( _design )
- {
- case TableDesign.TableNormal:
- val.Value = "TableNormal";
- break;
- case TableDesign.TableGrid:
- val.Value = "TableGrid";
- break;
- case TableDesign.LightShading:
- val.Value = "LightShading";
- break;
- case TableDesign.LightShadingAccent1:
- val.Value = "LightShading-Accent1";
- break;
- case TableDesign.LightShadingAccent2:
- val.Value = "LightShading-Accent2";
- break;
- case TableDesign.LightShadingAccent3:
- val.Value = "LightShading-Accent3";
- break;
- case TableDesign.LightShadingAccent4:
- val.Value = "LightShading-Accent4";
- break;
- case TableDesign.LightShadingAccent5:
- val.Value = "LightShading-Accent5";
- break;
- case TableDesign.LightShadingAccent6:
- val.Value = "LightShading-Accent6";
- break;
- case TableDesign.LightList:
- val.Value = "LightList";
- break;
- case TableDesign.LightListAccent1:
- val.Value = "LightList-Accent1";
- break;
- case TableDesign.LightListAccent2:
- val.Value = "LightList-Accent2";
- break;
- case TableDesign.LightListAccent3:
- val.Value = "LightList-Accent3";
- break;
- case TableDesign.LightListAccent4:
- val.Value = "LightList-Accent4";
- break;
- case TableDesign.LightListAccent5:
- val.Value = "LightList-Accent5";
- break;
- case TableDesign.LightListAccent6:
- val.Value = "LightList-Accent6";
- break;
- case TableDesign.LightGrid:
- val.Value = "LightGrid";
- break;
- case TableDesign.LightGridAccent1:
- val.Value = "LightGrid-Accent1";
- break;
- case TableDesign.LightGridAccent2:
- val.Value = "LightGrid-Accent2";
- break;
- case TableDesign.LightGridAccent3:
- val.Value = "LightGrid-Accent3";
- break;
- case TableDesign.LightGridAccent4:
- val.Value = "LightGrid-Accent4";
- break;
- case TableDesign.LightGridAccent5:
- val.Value = "LightGrid-Accent5";
- break;
- case TableDesign.LightGridAccent6:
- val.Value = "LightGrid-Accent6";
- break;
- case TableDesign.MediumShading1:
- val.Value = "MediumShading1";
- break;
- case TableDesign.MediumShading1Accent1:
- val.Value = "MediumShading1-Accent1";
- break;
- case TableDesign.MediumShading1Accent2:
- val.Value = "MediumShading1-Accent2";
- break;
- case TableDesign.MediumShading1Accent3:
- val.Value = "MediumShading1-Accent3";
- break;
- case TableDesign.MediumShading1Accent4:
- val.Value = "MediumShading1-Accent4";
- break;
- case TableDesign.MediumShading1Accent5:
- val.Value = "MediumShading1-Accent5";
- break;
- case TableDesign.MediumShading1Accent6:
- val.Value = "MediumShading1-Accent6";
- break;
- case TableDesign.MediumShading2:
- val.Value = "MediumShading2";
- break;
- case TableDesign.MediumShading2Accent1:
- val.Value = "MediumShading2-Accent1";
- break;
- case TableDesign.MediumShading2Accent2:
- val.Value = "MediumShading2-Accent2";
- break;
- case TableDesign.MediumShading2Accent3:
- val.Value = "MediumShading2-Accent3";
- break;
- case TableDesign.MediumShading2Accent4:
- val.Value = "MediumShading2-Accent4";
- break;
- case TableDesign.MediumShading2Accent5:
- val.Value = "MediumShading2-Accent5";
- break;
- case TableDesign.MediumShading2Accent6:
- val.Value = "MediumShading2-Accent6";
- break;
- case TableDesign.MediumList1:
- val.Value = "MediumList1";
- break;
- case TableDesign.MediumList1Accent1:
- val.Value = "MediumList1-Accent1";
- break;
- case TableDesign.MediumList1Accent2:
- val.Value = "MediumList1-Accent2";
- break;
- case TableDesign.MediumList1Accent3:
- val.Value = "MediumList1-Accent3";
- break;
- case TableDesign.MediumList1Accent4:
- val.Value = "MediumList1-Accent4";
- break;
- case TableDesign.MediumList1Accent5:
- val.Value = "MediumList1-Accent5";
- break;
- case TableDesign.MediumList1Accent6:
- val.Value = "MediumList1-Accent6";
- break;
- case TableDesign.MediumList2:
- val.Value = "MediumList2";
- break;
- case TableDesign.MediumList2Accent1:
- val.Value = "MediumList2-Accent1";
- break;
- case TableDesign.MediumList2Accent2:
- val.Value = "MediumList2-Accent2";
- break;
- case TableDesign.MediumList2Accent3:
- val.Value = "MediumList2-Accent3";
- break;
- case TableDesign.MediumList2Accent4:
- val.Value = "MediumList2-Accent4";
- break;
- case TableDesign.MediumList2Accent5:
- val.Value = "MediumList2-Accent5";
- break;
- case TableDesign.MediumList2Accent6:
- val.Value = "MediumList2-Accent6";
- break;
- case TableDesign.MediumGrid1:
- val.Value = "MediumGrid1";
- break;
- case TableDesign.MediumGrid1Accent1:
- val.Value = "MediumGrid1-Accent1";
- break;
- case TableDesign.MediumGrid1Accent2:
- val.Value = "MediumGrid1-Accent2";
- break;
- case TableDesign.MediumGrid1Accent3:
- val.Value = "MediumGrid1-Accent3";
- break;
- case TableDesign.MediumGrid1Accent4:
- val.Value = "MediumGrid1-Accent4";
- break;
- case TableDesign.MediumGrid1Accent5:
- val.Value = "MediumGrid1-Accent5";
- break;
- case TableDesign.MediumGrid1Accent6:
- val.Value = "MediumGrid1-Accent6";
- break;
- case TableDesign.MediumGrid2:
- val.Value = "MediumGrid2";
- break;
- case TableDesign.MediumGrid2Accent1:
- val.Value = "MediumGrid2-Accent1";
- break;
- case TableDesign.MediumGrid2Accent2:
- val.Value = "MediumGrid2-Accent2";
- break;
- case TableDesign.MediumGrid2Accent3:
- val.Value = "MediumGrid2-Accent3";
- break;
- case TableDesign.MediumGrid2Accent4:
- val.Value = "MediumGrid2-Accent4";
- break;
- case TableDesign.MediumGrid2Accent5:
- val.Value = "MediumGrid2-Accent5";
- break;
- case TableDesign.MediumGrid2Accent6:
- val.Value = "MediumGrid2-Accent6";
- break;
- case TableDesign.MediumGrid3:
- val.Value = "MediumGrid3";
- break;
- case TableDesign.MediumGrid3Accent1:
- val.Value = "MediumGrid3-Accent1";
- break;
- case TableDesign.MediumGrid3Accent2:
- val.Value = "MediumGrid3-Accent2";
- break;
- case TableDesign.MediumGrid3Accent3:
- val.Value = "MediumGrid3-Accent3";
- break;
- case TableDesign.MediumGrid3Accent4:
- val.Value = "MediumGrid3-Accent4";
- break;
- case TableDesign.MediumGrid3Accent5:
- val.Value = "MediumGrid3-Accent5";
- break;
- case TableDesign.MediumGrid3Accent6:
- val.Value = "MediumGrid3-Accent6";
- break;
-
- case TableDesign.DarkList:
- val.Value = "DarkList";
- break;
- case TableDesign.DarkListAccent1:
- val.Value = "DarkList-Accent1";
- break;
- case TableDesign.DarkListAccent2:
- val.Value = "DarkList-Accent2";
- break;
- case TableDesign.DarkListAccent3:
- val.Value = "DarkList-Accent3";
- break;
- case TableDesign.DarkListAccent4:
- val.Value = "DarkList-Accent4";
- break;
- case TableDesign.DarkListAccent5:
- val.Value = "DarkList-Accent5";
- break;
- case TableDesign.DarkListAccent6:
- val.Value = "DarkList-Accent6";
- break;
-
- case TableDesign.ColorfulShading:
- val.Value = "ColorfulShading";
- break;
- case TableDesign.ColorfulShadingAccent1:
- val.Value = "ColorfulShading-Accent1";
- break;
- case TableDesign.ColorfulShadingAccent2:
- val.Value = "ColorfulShading-Accent2";
- break;
- case TableDesign.ColorfulShadingAccent3:
- val.Value = "ColorfulShading-Accent3";
- break;
- case TableDesign.ColorfulShadingAccent4:
- val.Value = "ColorfulShading-Accent4";
- break;
- case TableDesign.ColorfulShadingAccent5:
- val.Value = "ColorfulShading-Accent5";
- break;
- case TableDesign.ColorfulShadingAccent6:
- val.Value = "ColorfulShading-Accent6";
- break;
-
- case TableDesign.ColorfulList:
- val.Value = "ColorfulList";
- break;
- case TableDesign.ColorfulListAccent1:
- val.Value = "ColorfulList-Accent1";
- break;
- case TableDesign.ColorfulListAccent2:
- val.Value = "ColorfulList-Accent2";
- break;
- case TableDesign.ColorfulListAccent3:
- val.Value = "ColorfulList-Accent3";
- break;
- case TableDesign.ColorfulListAccent4:
- val.Value = "ColorfulList-Accent4";
- break;
- case TableDesign.ColorfulListAccent5:
- val.Value = "ColorfulList-Accent5";
- break;
- case TableDesign.ColorfulListAccent6:
- val.Value = "ColorfulList-Accent6";
- break;
-
- case TableDesign.ColorfulGrid:
- val.Value = "ColorfulGrid";
- break;
- case TableDesign.ColorfulGridAccent1:
- val.Value = "ColorfulGrid-Accent1";
- break;
- case TableDesign.ColorfulGridAccent2:
- val.Value = "ColorfulGrid-Accent2";
- break;
- case TableDesign.ColorfulGridAccent3:
- val.Value = "ColorfulGrid-Accent3";
- break;
- case TableDesign.ColorfulGridAccent4:
- val.Value = "ColorfulGrid-Accent4";
- break;
- case TableDesign.ColorfulGridAccent5:
- val.Value = "ColorfulGrid-Accent5";
- break;
- case TableDesign.ColorfulGridAccent6:
- val.Value = "ColorfulGrid-Accent6";
- break;
-
- default:
- break;
- }
- }
-
- if( Document._styles == null )
- {
- PackagePart word_styles = Document._package.GetPart( new Uri( "/word/styles.xml", UriKind.Relative ) );
- using( TextReader tr = new StreamReader( word_styles.GetStream() ) )
- Document._styles = XDocument.Load( tr );
- }
-
- var tableStyle =
- (
- from e in Document._styles.Descendants()
- let styleId = e.Attribute( XName.Get( "styleId", DocX.w.NamespaceName ) )
- where ( styleId != null && styleId.Value == val.Value )
- select e
- ).FirstOrDefault();
-
- if( tableStyle == null )
- {
- XDocument external_style_doc = HelperFunctions.DecompressXMLResource( "Xceed.Words.NET.Resources.styles.xml.gz" );
-
- var styleElement =
- (
- from e in external_style_doc.Descendants()
- let styleId = e.Attribute( XName.Get( "styleId", DocX.w.NamespaceName ) )
- where ( styleId != null && styleId.Value == val.Value )
- select e
- ).FirstOrDefault();
-
- if( styleElement != null )
- Document._styles.Element( XName.Get( "styles", DocX.w.NamespaceName ) ).Add( styleElement );
- }
- }
- }
-
- /// <summary>
- /// Returns the index of this Table.
- /// </summary>
- /// <example>
- /// Replace the first table in this document with a new Table.
- /// <code>
- /// // Load a document into memory.
- /// using (DocX document = DocX.Load(@"Test.docx"))
- /// {
- /// // Get the first Table in this document.
- /// Table t = document.Tables[0];
- ///
- /// // Get the character index of Table t in this document.
- /// int index = t.Index;
- ///
- /// // Remove Table t.
- /// t.Remove();
- ///
- /// // Insert a new Table at the original index of Table t.
- /// Table newTable = document.InsertTable(index, 4, 4);
- ///
- /// // Set the design of this new Table, so that we can see it.
- /// newTable.Design = TableDesign.LightShadingAccent1;
- ///
- /// // Save all changes made to the document.
- /// document.Save();
- /// } // Release this document from memory.
- /// </code>
- /// </example>
- public int Index
- {
- get
- {
- int index = 0;
- IEnumerable<XElement> previous = Xml.ElementsBeforeSelf();
-
- foreach( XElement e in previous )
- index += Paragraph.GetElementTextLength( e );
-
- return index;
- }
- }
-
- /// <summary>
- /// The custom design/style to apply to this table.
- /// </summary>
- public string CustomTableDesignName
- {
- get
- {
- return _customTableDesignName;
- }
- set
- {
- _customTableDesignName = value;
- this.Design = TableDesign.Custom;
- }
-
- }
-
- /// <summary>
- /// Gets or sets the value of the Table Caption (Alternate Text Title) of this table.
- /// </summary>
- public string TableCaption
- {
- get
- {
- var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- var caption = tblPr?.Element( XName.Get( "tblCaption", DocX.w.NamespaceName ) );
-
- if( caption != null )
- return caption.GetAttribute( XName.Get( "val", DocX.w.NamespaceName ) );
-
- return null;
- }
- set
- {
- var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- if( tblPr != null )
- {
- var caption = tblPr.Descendants( XName.Get( "tblCaption", DocX.w.NamespaceName ) ).FirstOrDefault();
- if( caption != null )
- {
- caption.Remove();
- }
- caption = new XElement( XName.Get( "tblCaption", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), value ) );
- tblPr.Add( caption );
- }
- }
- }
-
- /// <summary>
- /// Gets or sets the value of the Table Description (Alternate Text Description) of this table.
- /// </summary>
- public string TableDescription
- {
- get
- {
- var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- var description = tblPr?.Element( XName.Get( "tblDescription", DocX.w.NamespaceName ) );
-
- if( description != null )
- return description.GetAttribute( XName.Get( "val", DocX.w.NamespaceName ) );
-
- return null;
- }
- set
- {
- var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- if( tblPr != null )
- {
- var description = tblPr.Descendants( XName.Get( "tblDescription", DocX.w.NamespaceName ) ).FirstOrDefault();
- description?.Remove();
- description = new XElement( XName.Get( "tblDescription", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), value ) );
- tblPr.Add( description );
- }
- }
- }
-
-
- public TableLook TableLook
- {
- get;
- set;
- }
-
- public List<Double> ColumnWidths
- {
- get
- {
- var columnWidths = new List<Double>();
-
- // get the table grid property
- XElement grid = Xml.Element( XName.Get( "tblGrid", DocX.w.NamespaceName ) );
-
- // get the columns properties
- var columns = grid?.Elements( XName.Get( "gridCol", DocX.w.NamespaceName ) );
- if( columns == null )
- return null;
-
- foreach( var column in columns )
- {
- string value = column.GetAttribute( XName.Get( "w", DocX.w.NamespaceName ) );
- columnWidths.Add( Convert.ToDouble( value, new CultureInfo( "en-US" ) ) );
- }
-
- return columnWidths;
- }
- }
-
- #endregion
-
- #region Constructors
-
- internal Table( DocX document, XElement xml )
- : base( document, xml )
- {
- _autofit = AutoFit.ColumnWidth;
- this.Xml = xml;
- this.PackagePart = document.PackagePart;
-
- var properties = xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
-
- var alignment = properties.Element( XName.Get( "jc", DocX.w.NamespaceName ) );
- if( alignment != null )
- {
- var val = alignment.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
- if( val != null )
- {
- _alignment = (Alignment)Enum.Parse( typeof( Alignment ), val.Value );
- }
- }
-
- var style = properties?.Element( XName.Get( "tblStyle", DocX.w.NamespaceName ) );
- if( style != null )
- {
- var val = style.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
-
- if( val != null )
- {
- String cleanValue = val.Value.Replace( "-", string.Empty );
-
- if( Enum.IsDefined( typeof( TableDesign ), cleanValue ) )
- {
- _design = ( TableDesign )Enum.Parse( typeof( TableDesign ), cleanValue );
- }
-
- else
- {
- _design = TableDesign.Custom;
- }
- }
- else
- {
- _design = TableDesign.None;
- }
- }
- else
- {
- _design = TableDesign.None;
- }
-
- var tableLook = properties?.Element( XName.Get( "tblLook", DocX.w.NamespaceName ) );
- if( tableLook != null )
- {
- this.TableLook = new TableLook( tableLook.GetAttribute( XName.Get( "firstRow", DocX.w.NamespaceName ) ) == "1",
- tableLook.GetAttribute( XName.Get( "lastRow", DocX.w.NamespaceName ) ) == "1",
- tableLook.GetAttribute( XName.Get( "firstColumn", DocX.w.NamespaceName ) ) == "1",
- tableLook.GetAttribute( XName.Get( "lastColumn", DocX.w.NamespaceName ) ) == "1",
- tableLook.GetAttribute( XName.Get( "noHBand", DocX.w.NamespaceName ) ) == "1",
- tableLook.GetAttribute( XName.Get( "noVBand", DocX.w.NamespaceName ) ) == "1" );
- }
- }
-
- #endregion
-
- #region Public Methods
-
- /// <summary>
- /// Merge cells in given column starting with startRow and ending with endRow.
- /// </summary>
- public void MergeCellsInColumn( int columnIndex, int startRow, int endRow )
- {
- // Check for valid start and end indexes.
- if( columnIndex < 0 || columnIndex >= ColumnCount )
- throw new IndexOutOfRangeException();
-
- if( startRow < 0 || endRow <= startRow || endRow >= Rows.Count )
- throw new IndexOutOfRangeException();
- // Foreach each Cell between startIndex and endIndex inclusive.
- foreach( Row row in Rows.Where( ( z, i ) => i > startRow && i <= endRow ) )
- {
- var c = row.Cells[ columnIndex ];
- var tcPr = c.Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- c.Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- tcPr = c.Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- var vMerge = tcPr.Element( XName.Get( "vMerge", DocX.w.NamespaceName ) );
- if( vMerge == null )
- {
- tcPr.SetElementValue( XName.Get( "vMerge", DocX.w.NamespaceName ), string.Empty );
- vMerge = tcPr.Element( XName.Get( "vMerge", DocX.w.NamespaceName ) );
- }
- }
-
- /*
- * Get the tcPr (table cell properties) element for the first cell in this merge,
- * null will be returned if no such element exists.
- */
- var startRowCellsCount = this.Rows[ startRow ].Cells.Count;
- var start_tcPr = ( columnIndex > startRowCellsCount )
- ? this.Rows[ startRow ].Cells[ startRowCellsCount - 1 ].Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) )
- : this.Rows[ startRow ].Cells[ columnIndex ].Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( start_tcPr == null )
- {
- this.Rows[ startRow ].Cells[ columnIndex ].Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- start_tcPr = this.Rows[ startRow ].Cells[ columnIndex ].Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the gridSpan element of this row,
- * null will be returned if no such element exists.
- */
- var start_vMerge = start_tcPr.Element( XName.Get( "vMerge", DocX.w.NamespaceName ) );
- if( start_vMerge == null )
- {
- start_tcPr.SetElementValue( XName.Get( "vMerge", DocX.w.NamespaceName ), string.Empty );
- start_vMerge = start_tcPr.Element( XName.Get( "vMerge", DocX.w.NamespaceName ) );
- }
-
- start_vMerge.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), "restart" );
- }
-
- /// <summary>
- /// Set the direction of all content in this Table.
- /// </summary>
- /// <param name="direction">(Left to Right) or (Right to Left)</param>
- /// <example>
- /// Set the content direction for all content in a table to RightToLeft.
- /// <code>
- /// // Load a document.
- /// using (DocX document = DocX.Load(@"Test.docx"))
- /// {
- /// // Get the first table in a document.
- /// Table table = document.Tables[0];
- ///
- /// // Set the content direction for all content in this table to RightToLeft.
- /// table.SetDirection(Direction.RightToLeft);
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }
- /// </code>
- /// </example>
- public void SetDirection( Direction direction )
- {
- var tblPr = GetOrCreate_tblPr();
- tblPr.Add( new XElement( DocX.w + "bidiVisual" ) );
-
- foreach( Row r in Rows )
- {
- r.SetDirection( direction );
- }
- }
-
- /// <summary>
- /// Remove this Table from this document.
- /// </summary>
- /// <example>
- /// Remove the first Table from this document.
- /// <code>
- /// // Load a document into memory.
- /// using (DocX document = DocX.Load(@"Test.docx"))
- /// {
- /// // Get the first Table in this document.
- /// Table t = d.Tables[0];
- ///
- /// // Remove this Table.
- /// t.Remove();
- ///
- /// // Save all changes made to the document.
- /// document.Save();
- /// } // Release this document from memory.
- /// </code>
- /// </example>
- public void Remove()
- {
- this.Xml.Remove();
- }
-
- /// <summary>
- /// Insert a row at the end of this table.
- /// </summary>
- /// <example>
- /// <code>
- /// // Load a document.
- /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
- /// {
- /// // Get the first table in this document.
- /// Table table = document.Tables[0];
- ///
- /// // Insert a new row at the end of this table.
- /// Row row = table.InsertRow();
- ///
- /// // Loop through each cell in this new row.
- /// foreach (Cell c in row.Cells)
- /// {
- /// // Set the text of each new cell to "Hello".
- /// c.Paragraphs[0].InsertText("Hello", false);
- /// }
- ///
- /// // Save the document to a new file.
- /// document.SaveAs(@"C:\Example\Test2.docx");
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- /// <returns>A new row.</returns>
- public Row InsertRow()
- {
- return this.InsertRow( this.RowCount );
- }
-
- /// <summary>
- /// Insert a copy of a row at the end of this table.
- /// </summary>
- /// <returns>A new row.</returns>
- public Row InsertRow( Row row, bool keepFormatting = false )
- {
- return this.InsertRow( row, this.RowCount, keepFormatting );
- }
-
- /// <summary>
- /// Insert a column to the right of a Table.
- /// </summary>
- /// <example>
- /// <code>
- /// // Load a document.
- /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
- /// {
- /// // Get the first Table in this document.
- /// Table table = document.Tables[0];
- ///
- /// // Insert a new column to this right of this table.
- /// table.InsertColumn();
- ///
- /// // Set the new columns text to "Row no."
- /// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
- ///
- /// // Loop through each row in the table.
- /// for (int i = 1; i < table.Rows.Count; i++)
- /// {
- /// // The current row.
- /// Row row = table.Rows[i];
- ///
- /// // The cell in this row that belongs to the new column.
- /// Cell cell = row.Cells[table.ColumnCount - 1];
- ///
- /// // The first Paragraph that this cell houses.
- /// Paragraph p = cell.Paragraphs[0];
- ///
- /// // Insert this rows index.
- /// p.InsertText(i.ToString(), false);
- /// }
- ///
- /// document.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public void InsertColumn()
- {
- this.InsertColumn( this.ColumnCount, true );
- }
-
- /// <summary>
- /// Remove the last row from this Table.
- /// </summary>
- /// <example>
- /// Remove the last row from a Table.
- /// <code>
- /// // Load a document.
- /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
- /// {
- /// // Get the first table in this document.
- /// Table table = document.Tables[0];
- ///
- /// // Remove the last row from this table.
- /// table.RemoveRow();
- ///
- /// // Save the document.
- /// document.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public void RemoveRow()
- {
- this.RemoveRow( RowCount - 1 );
- }
-
- /// <summary>
- /// Remove a row from this Table.
- /// </summary>
- /// <param name="index">The row to remove.</param>
- /// <example>
- /// Remove the first row from a Table.
- /// <code>
- /// // Load a document.
- /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
- /// {
- /// // Get the first table in this document.
- /// Table table = document.Tables[0];
- ///
- /// // Remove the first row from this table.
- /// table.RemoveRow(0);
- ///
- /// // Save the document.
- /// document.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public void RemoveRow( int index )
- {
- if( index < 0 || index > RowCount - 1 )
- throw new IndexOutOfRangeException();
-
- this.Rows[ index ].Xml.Remove();
- if( this.Rows.Count == 0 )
- {
- this.Remove();
- }
- }
-
- /// <summary>
- /// Remove the last column for this Table.
- /// </summary>
- /// <example>
- /// Remove the last column from a Table.
- /// <code>
- /// // Load a document.
- /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
- /// {
- /// // Get the first table in this document.
- /// Table table = document.Tables[0];
- ///
- /// // Remove the last column from this table.
- /// table.RemoveColumn();
- ///
- /// // Save the document.
- /// document.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public void RemoveColumn()
- {
- this.RemoveColumn( this.ColumnCount - 1 );
- }
-
- /// <summary>
- /// Remove a column from this Table.
- /// </summary>
- /// <param name="index">The column to remove.</param>
- /// <example>
- /// Remove the first column from a Table.
- /// <code>
- /// // Load a document.
- /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
- /// {
- /// // Get the first table in this document.
- /// Table table = document.Tables[0];
- ///
- /// // Remove the first column from this table.
- /// table.RemoveColumn(0);
- ///
- /// // Save the document.
- /// document.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public void RemoveColumn( int index )
- {
- if( ( index < 0 ) || ( index > this.ColumnCount - 1 ) )
- throw new IndexOutOfRangeException();
-
- foreach( Row r in Rows )
- {
- if( r.Cells.Count < this.ColumnCount )
- {
- int gridAfterValue = r.GridAfter;
- int posIndex = 0;
- int currentPos = 0;
-
- for( int i = 0; i < r.Cells.Count; ++i )
- {
- var rowCell = r.Cells[ i ];
-
- int gridSpanValue = ( rowCell.GridSpan != 0 ) ? rowCell.GridSpan - 1 : 0;
-
- // checks to see if index is between the lowest and highest cell value.
- if( ( ( index - gridAfterValue ) >= currentPos )
- && ( ( index - gridAfterValue ) <= ( currentPos + gridSpanValue ) ) )
- {
- r.Cells[ posIndex ].Xml.Remove();
- break;
- }
-
- ++posIndex;
- currentPos += ( gridSpanValue + 1 );
- }
- }
- else
- {
- r.Cells[ index ].Xml.Remove();
- }
- }
- _cachedColumnCount = -1;
- }
-
- /// <summary>
- /// Insert a row into this table.
- /// </summary>
- /// <example>
- /// <code>
- /// // Load a document.
- /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
- /// {
- /// // Get the first table in this document.
- /// Table table = document.Tables[0];
- ///
- /// // Insert a new row at index 1 in this table.
- /// Row row = table.InsertRow(1);
- ///
- /// // Loop through each cell in this new row.
- /// foreach (Cell c in row.Cells)
- /// {
- /// // Set the text of each new cell to "Hello".
- /// c.Paragraphs[0].InsertText("Hello", false);
- /// }
- ///
- /// // Save the document to a new file.
- /// document.SaveAs(@"C:\Example\Test2.docx");
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- /// <param name="index">Index to insert row at.</param>
- /// <returns>A new Row</returns>
- public Row InsertRow( int index )
- {
- if( ( index < 0 ) || ( index > this.RowCount ) )
- throw new IndexOutOfRangeException();
-
- var content = new List<XElement>();
- for( int i = 0; i < ColumnCount; i++ )
- {
- var cell = ( ( _columnWidths != null ) && ( _columnWidths.Length > i ) )
- ? HelperFunctions.CreateTableCell( _columnWidths[ i ] * 20 )
- : HelperFunctions.CreateTableCell();
- content.Add( cell );
- }
-
- return this.InsertRow( content, index );
- }
-
- /// <summary>
- /// Insert a copy of a row into this table.
- /// </summary>
- /// <param name="row">Row to copy and insert.</param>
- /// <param name="index">Index to insert row at.</param>
- /// <param name="keepFormatting">True to clone everithing, False to clone cell structure only.</param>
- /// <returns>A new Row</returns>
- public Row InsertRow( Row row, int index, bool keepFormatting = false )
- {
- if( row == null )
- throw new ArgumentNullException( "row" );
-
- if( index < 0 || index > RowCount )
- throw new IndexOutOfRangeException();
-
- List<XElement> content;
- if( keepFormatting )
- content = row.Xml.Elements().Select( element => HelperFunctions.CloneElement( element ) ).ToList();
- else
- content = row.Xml.Elements( XName.Get( "tc", DocX.w.NamespaceName ) ).Select( element => HelperFunctions.CloneElement( element ) ).ToList();
-
- return InsertRow( content, index );
- }
-
- /// <summary>
- /// Insert a column into a table.
- /// </summary>
- /// <param name="index">The index to insert the column at.</param>
- /// <param name="direction">The side in which you wish to place the colum : True for right, false for left.</param>
- /// <example>
- /// Insert a column to the left of a table.
- /// <code>
- /// // Load a document.
- /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
- /// {
- /// // Get the first Table in this document.
- /// Table table = document.Tables[0];
- ///
- /// // Insert a new column to this left of this table.
- /// table.InsertColumn(0, false);
- ///
- /// // Set the new columns text to "Row no."
- /// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
- ///
- /// // Loop through each row in the table.
- /// for (int i = 1; i < table.Rows.Count; i++)
- /// {
- /// // The current row.
- /// Row row = table.Rows[i];
- ///
- /// // The cell in this row that belongs to the new column.
- /// Cell cell = row.Cells[table.ColumnCount - 1];
- ///
- /// // The first Paragraph that this cell houses.
- /// Paragraph p = cell.Paragraphs[0];
- ///
- /// // Insert this rows index.
- /// p.InsertText(i.ToString(), false);
- /// }
- ///
- /// document.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public void InsertColumn( int index, bool direction )
- {
- var colCount = this.ColumnCount;
-
- if( (index <= 0) && (index > colCount ) )
- throw new NullReferenceException( "index should be greater than 0 and smaller or equals to this.ColumnCount." );
-
- if( this.RowCount > 0 )
- {
- _cachedColumnCount = -1;
- foreach( Row r in this.Rows )
- {
- // create cell
- var cell = HelperFunctions.CreateTableCell();
-
- // insert cell
- if( r.Cells.Count < colCount )
- {
- if( index >= colCount )
- {
- this.AddCellToRow( r, cell, r.Cells.Count - 1, direction );
- }
- else
- {
- int gridAfterValue = r.GridAfter;
- int currentPosition = 1;
- int posIndex = 1;
-
- foreach( var rowCell in r.Cells )
- {
- int gridSpanValue = ( rowCell.GridSpan != 0 ) ? rowCell.GridSpan - 1 : 0;
- var tcPr = rowCell.Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
-
- // Check if the cell have a gridSpan and if the index is between the lowest and highest cell value
- if( ( ( index - gridAfterValue ) >= currentPosition )
- && ( ( index - gridAfterValue ) <= ( currentPosition + gridSpanValue ) ) )
- {
- var dir = ( direction && ( index == ( currentPosition + gridSpanValue ) ) );
- this.AddCellToRow( r, cell, posIndex - 1, dir );
- break;
- }
-
- ++posIndex;
- currentPosition += (gridSpanValue + 1);
- }
- }
- }
- else
- {
- this.AddCellToRow( r, cell, index - 1, direction );
- }
- }
- }
- }
-
- /// <summary>
- /// Insert a page break before a Table.
- /// </summary>
- /// <example>
- /// Insert a Table and a Paragraph into a document with a page break between them.
- /// <code>
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a new Paragraph.
- /// Paragraph p1 = document.InsertParagraph("Paragraph", false);
- ///
- /// // Insert a new Table.
- /// Table t1 = document.InsertTable(2, 2);
- /// t1.Design = TableDesign.LightShadingAccent1;
- ///
- /// // Insert a page break before this Table.
- /// t1.InsertPageBreakBeforeSelf();
- ///
- /// // Save this document.
- /// document.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public override void InsertPageBreakBeforeSelf()
- {
- base.InsertPageBreakBeforeSelf();
- }
-
- public void SetWidths( float[] widths )
- {
- _columnWidths = widths;
-
- for( int i = 0; i < this.Rows.Count; ++i )
- {
- var row = this.Rows[ i ];
-
- for( int j = 0; j < widths.Length; ++j )
- {
- if( row.Cells.Count > j )
- {
- row.Cells[ j ].Width = widths[ j ];
- }
- }
- }
- }
-
- public void SetWidthsPercentage( float[] widthsPercentage, float? totalWidth )
- {
- if( totalWidth == null )
- totalWidth = this.Document.PageWidth - this.Document.MarginLeft - this.Document.MarginRight;
-
- List<float> widths = new List<float>( widthsPercentage.Length );
- widthsPercentage.ToList().ForEach( pWidth => { widths.Add( ( pWidth * totalWidth.Value / 100 ) * ( 96 / 72 ) ); } );
- SetWidths( widths.ToArray() );
- }
-
- /// <summary>
- /// Insert a page break after a Table.
- /// </summary>
- /// <example>
- /// Insert a Table and a Paragraph into a document with a page break between them.
- /// <code>
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a new Table.
- /// Table t1 = document.InsertTable(2, 2);
- /// t1.Design = TableDesign.LightShadingAccent1;
- ///
- /// // Insert a page break after this Table.
- /// t1.InsertPageBreakAfterSelf();
- ///
- /// // Insert a new Paragraph.
- /// Paragraph p1 = document.InsertParagraph("Paragraph", false);
- ///
- /// // Save this document.
- /// document.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public override void InsertPageBreakAfterSelf()
- {
- base.InsertPageBreakAfterSelf();
- }
-
- /// <summary>
- /// Insert a new Table before this Table, this Table can be from this document or another document.
- /// </summary>
- /// <param name="t">The Table t to be inserted</param>
- /// <returns>A new Table inserted before this Table.</returns>
- /// <example>
- /// Insert a new Table before this Table.
- /// <code>
- /// // Place holder for a Table.
- /// Table t;
- ///
- /// // Load document a.
- /// using (DocX documentA = DocX.Load(@"a.docx"))
- /// {
- /// // Get the first Table from this document.
- /// t = documentA.Tables[0];
- /// }
- ///
- /// // Load document b.
- /// using (DocX documentB = DocX.Load(@"b.docx"))
- /// {
- /// // Get the first Table in document b.
- /// Table t2 = documentB.Tables[0];
- ///
- /// // Insert the Table from document a before this Table.
- /// Table newTable = t2.InsertTableBeforeSelf(t);
- ///
- /// // Save all changes made to document b.
- /// documentB.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public override Table InsertTableBeforeSelf( Table t )
- {
- return base.InsertTableBeforeSelf( t );
- }
-
- /// <summary>
- /// Insert a new Table into this document before this Table.
- /// </summary>
- /// <param name="rowCount">The number of rows this Table should have.</param>
- /// <param name="columnCount">The number of columns this Table should have.</param>
- /// <returns>A new Table inserted before this Table.</returns>
- /// <example>
- /// <code>
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// //Insert a Table into this document.
- /// Table t = document.InsertTable(2, 2);
- /// t.Design = TableDesign.LightShadingAccent1;
- /// t.Alignment = Alignment.center;
- ///
- /// // Insert a new Table before this Table.
- /// Table newTable = t.InsertTableBeforeSelf(2, 2);
- /// newTable.Design = TableDesign.LightShadingAccent2;
- /// newTable.Alignment = Alignment.center;
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public override Table InsertTableBeforeSelf( int rowCount, int columnCount )
- {
- return base.InsertTableBeforeSelf( rowCount, columnCount );
- }
-
- /// <summary>
- /// Insert a new Table after this Table, this Table can be from this document or another document.
- /// </summary>
- /// <param name="t">The Table t to be inserted</param>
- /// <returns>A new Table inserted after this Table.</returns>
- /// <example>
- /// Insert a new Table after this Table.
- /// <code>
- /// // Place holder for a Table.
- /// Table t;
- ///
- /// // Load document a.
- /// using (DocX documentA = DocX.Load(@"a.docx"))
- /// {
- /// // Get the first Table from this document.
- /// t = documentA.Tables[0];
- /// }
- ///
- /// // Load document b.
- /// using (DocX documentB = DocX.Load(@"b.docx"))
- /// {
- /// // Get the first Table in document b.
- /// Table t2 = documentB.Tables[0];
- ///
- /// // Insert the Table from document a after this Table.
- /// Table newTable = t2.InsertTableAfterSelf(t);
- ///
- /// // Save all changes made to document b.
- /// documentB.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public override Table InsertTableAfterSelf( Table t )
- {
- return base.InsertTableAfterSelf( t );
- }
-
- /// <summary>
- /// Insert a new Table into this document after this Table.
- /// </summary>
- /// <param name="rowCount">The number of rows this Table should have.</param>
- /// <param name="columnCount">The number of columns this Table should have.</param>
- /// <returns>A new Table inserted before this Table.</returns>
- /// <example>
- /// <code>
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// //Insert a Table into this document.
- /// Table t = document.InsertTable(2, 2);
- /// t.Design = TableDesign.LightShadingAccent1;
- /// t.Alignment = Alignment.center;
- ///
- /// // Insert a new Table after this Table.
- /// Table newTable = t.InsertTableAfterSelf(2, 2);
- /// newTable.Design = TableDesign.LightShadingAccent2;
- /// newTable.Alignment = Alignment.center;
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public override Table InsertTableAfterSelf( int rowCount, int columnCount )
- {
- return base.InsertTableAfterSelf( rowCount, columnCount );
- }
-
- /// <summary>
- /// Insert a Paragraph before this Table, this Paragraph may have come from the same or another document.
- /// </summary>
- /// <param name="p">The Paragraph to insert.</param>
- /// <returns>The Paragraph now associated with this document.</returns>
- /// <example>
- /// Take a Paragraph from document a, and insert it into document b before this Table.
- /// <code>
- /// // Place holder for a Paragraph.
- /// Paragraph p;
- ///
- /// // Load document a.
- /// using (DocX documentA = DocX.Load(@"a.docx"))
- /// {
- /// // Get the first paragraph from this document.
- /// p = documentA.Paragraphs[0];
- /// }
- ///
- /// // Load document b.
- /// using (DocX documentB = DocX.Load(@"b.docx"))
- /// {
- /// // Get the first Table in document b.
- /// Table t = documentB.Tables[0];
- ///
- /// // Insert the Paragraph from document a before this Table.
- /// Paragraph newParagraph = t.InsertParagraphBeforeSelf(p);
- ///
- /// // Save all changes made to document b.
- /// documentB.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public override Paragraph InsertParagraphBeforeSelf( Paragraph p )
- {
- return base.InsertParagraphBeforeSelf( p );
- }
-
- /// <summary>
- /// Insert a new Paragraph before this Table.
- /// </summary>
- /// <param name="text">The initial text for this new Paragraph.</param>
- /// <returns>A new Paragraph inserted before this Table.</returns>
- /// <example>
- /// Insert a new Paragraph before the first Table in this document.
- /// <code>
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Table into this document.
- /// Table t = document.InsertTable(2, 2);
- ///
- /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.");
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- /// </code>
- /// </example>
- public override Paragraph InsertParagraphBeforeSelf( string text )
- {
- return base.InsertParagraphBeforeSelf( text );
- }
-
- /// <summary>
- /// Insert a new Paragraph before this Table.
- /// </summary>
- /// <param name="text">The initial text for this new Paragraph.</param>
- /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
- /// <returns>A new Paragraph inserted before this Table.</returns>
- /// <example>
- /// Insert a new paragraph before the first Table in this document.
- /// <code>
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Table into this document.
- /// Table t = document.InsertTable(2, 2);
- ///
- /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false);
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- /// </code>
- /// </example>
- public override Paragraph InsertParagraphBeforeSelf( string text, bool trackChanges )
- {
- return base.InsertParagraphBeforeSelf( text, trackChanges );
- }
-
- /// <summary>
- /// Insert a new Paragraph before this Table.
- /// </summary>
- /// <param name="text">The initial text for this new Paragraph.</param>
- /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
- /// <param name="formatting">The formatting to apply to this insertion.</param>
- /// <returns>A new Paragraph inserted before this Table.</returns>
- /// <example>
- /// Insert a new paragraph before the first Table in this document.
- /// <code>
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Table into this document.
- /// Table t = document.InsertTable(2, 2);
- ///
- /// Formatting boldFormatting = new Formatting();
- /// boldFormatting.Bold = true;
- ///
- /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false, boldFormatting);
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- /// </code>
- /// </example>
- public override Paragraph InsertParagraphBeforeSelf( string text, bool trackChanges, Formatting formatting )
- {
- return base.InsertParagraphBeforeSelf( text, trackChanges, formatting );
- }
-
- /// <summary>
- /// Insert a Paragraph after this Table, this Paragraph may have come from the same or another document.
- /// </summary>
- /// <param name="p">The Paragraph to insert.</param>
- /// <returns>The Paragraph now associated with this document.</returns>
- /// <example>
- /// Take a Paragraph from document a, and insert it into document b after this Table.
- /// <code>
- /// // Place holder for a Paragraph.
- /// Paragraph p;
- ///
- /// // Load document a.
- /// using (DocX documentA = DocX.Load(@"a.docx"))
- /// {
- /// // Get the first paragraph from this document.
- /// p = documentA.Paragraphs[0];
- /// }
- ///
- /// // Load document b.
- /// using (DocX documentB = DocX.Load(@"b.docx"))
- /// {
- /// // Get the first Table in document b.
- /// Table t = documentB.Tables[0];
- ///
- /// // Insert the Paragraph from document a after this Table.
- /// Paragraph newParagraph = t.InsertParagraphAfterSelf(p);
- ///
- /// // Save all changes made to document b.
- /// documentB.Save();
- /// }// Release this document from memory.
- /// </code>
- /// </example>
- public override Paragraph InsertParagraphAfterSelf( Paragraph p )
- {
- return base.InsertParagraphAfterSelf( p );
- }
-
- /// <summary>
- /// Insert a new Paragraph after this Table.
- /// </summary>
- /// <param name="text">The initial text for this new Paragraph.</param>
- /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
- /// <param name="formatting">The formatting to apply to this insertion.</param>
- /// <returns>A new Paragraph inserted after this Table.</returns>
- /// <example>
- /// Insert a new paragraph after the first Table in this document.
- /// <code>
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Table into this document.
- /// Table t = document.InsertTable(2, 2);
- ///
- /// Formatting boldFormatting = new Formatting();
- /// boldFormatting.Bold = true;
- ///
- /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false, boldFormatting);
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- /// </code>
- /// </example>
- public override Paragraph InsertParagraphAfterSelf( string text, bool trackChanges, Formatting formatting )
- {
- return base.InsertParagraphAfterSelf( text, trackChanges, formatting );
- }
-
- /// <summary>
- /// Insert a new Paragraph after this Table.
- /// </summary>
- /// <param name="text">The initial text for this new Paragraph.</param>
- /// <param name="trackChanges">Should this insertion be tracked as a change?</param>
- /// <returns>A new Paragraph inserted after this Table.</returns>
- /// <example>
- /// Insert a new paragraph after the first Table in this document.
- /// <code>
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Table into this document.
- /// Table t = document.InsertTable(2, 2);
- ///
- /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false);
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- /// </code>
- /// </example>
- public override Paragraph InsertParagraphAfterSelf( string text, bool trackChanges )
- {
- return base.InsertParagraphAfterSelf( text, trackChanges );
- }
-
- /// <summary>
- /// Insert a new Paragraph after this Table.
- /// </summary>
- /// <param name="text">The initial text for this new Paragraph.</param>
- /// <returns>A new Paragraph inserted after this Table.</returns>
- /// <example>
- /// Insert a new Paragraph after the first Table in this document.
- /// <code>
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Table into this document.
- /// Table t = document.InsertTable(2, 2);
- ///
- /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.");
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- /// </code>
- /// </example>
- public override Paragraph InsertParagraphAfterSelf( string text )
- {
- return base.InsertParagraphAfterSelf( text );
- }
-
- /// <summary>
- /// Set a table border
- /// </summary>
- /// <example>
- /// <code>
- /// // Create a new document.
- ///using (DocX document = DocX.Create("Test.docx"))
- ///{
- /// // Insert a table into this document.
- /// Table t = document.InsertTable(3, 3);
- ///
- /// // Create a large blue border.
- /// Border b = new Border(BorderStyle.Tcbs_single, BorderSize.seven, 0, Color.Blue);
- ///
- /// // Set the tables Top, Bottom, Left and Right Borders to b.
- /// t.SetBorder(TableBorderType.Top, b);
- /// t.SetBorder(TableBorderType.Bottom, b);
- /// t.SetBorder(TableBorderType.Left, b);
- /// t.SetBorder(TableBorderType.Right, b);
- ///
- /// // Save the document.
- /// document.Save();
- ///}
- /// </code>
- /// </example>
- /// <param name="borderType">The table border to set</param>
- /// <param name="border">Border object to set the table border</param>
- public void SetBorder( TableBorderType borderType, Border border )
- {
- /*
- * Get the tblPr (table properties) element for this Table,
- * null will be return if no such element exists.
- */
- var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- if( tblPr == null )
- {
- this.Xml.SetElementValue( XName.Get( "tblPr", DocX.w.NamespaceName ), string.Empty );
- tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the tblBorders (table borders) element for this Table,
- * null will be return if no such element exists.
- */
- var tblBorders = tblPr.Element( XName.Get( "tblBorders", DocX.w.NamespaceName ) );
- if( tblBorders == null )
- {
- tblPr.SetElementValue( XName.Get( "tblBorders", DocX.w.NamespaceName ), string.Empty );
- tblBorders = tblPr.Element( XName.Get( "tblBorders", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the 'borderType' (table border) element for this Table,
- * null will be return if no such element exists.
- */
- string tbordertype;
- tbordertype = borderType.ToString();
- // only lower the first char of string (because of insideH and insideV)
- tbordertype = tbordertype.Substring( 0, 1 ).ToLower() + tbordertype.Substring( 1 );
-
- var tblBorderType = tblBorders.Element( XName.Get( borderType.ToString(), DocX.w.NamespaceName ) );
- if( tblBorderType == null )
- {
- tblBorders.SetElementValue( XName.Get( tbordertype, DocX.w.NamespaceName ), string.Empty );
- tblBorderType = tblBorders.Element( XName.Get( tbordertype, DocX.w.NamespaceName ) );
- }
-
- // get string value of border style
- var borderstyle = border.Tcbs.ToString().Substring( 5 );
- borderstyle = borderstyle.Substring( 0, 1 ).ToLower() + borderstyle.Substring( 1 );
-
- // The val attribute is used for the border style
- tblBorderType.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), borderstyle );
-
- if( border.Tcbs != BorderStyle.Tcbs_nil )
- {
- int size;
- switch( border.Size )
- {
- case BorderSize.one:
- size = 2;
- break;
- case BorderSize.two:
- size = 4;
- break;
- case BorderSize.three:
- size = 6;
- break;
- case BorderSize.four:
- size = 8;
- break;
- case BorderSize.five:
- size = 12;
- break;
- case BorderSize.six:
- size = 18;
- break;
- case BorderSize.seven:
- size = 24;
- break;
- case BorderSize.eight:
- size = 36;
- break;
- case BorderSize.nine:
- size = 48;
- break;
- default:
- size = 2;
- break;
- }
-
- // The sz attribute is used for the border size
- tblBorderType.SetAttributeValue( XName.Get( "sz", DocX.w.NamespaceName ), ( size ).ToString() );
-
- // The space attribute is used for the cell spacing (probably '0')
- tblBorderType.SetAttributeValue( XName.Get( "space", DocX.w.NamespaceName ), ( border.Space ).ToString() );
-
- // The color attribute is used for the border color
- tblBorderType.SetAttributeValue( XName.Get( "color", DocX.w.NamespaceName ), border.Color.ToHex() );
- }
- }
-
- /// <summary>
- /// Get a table border
- /// </summary>
- /// <param name="borderType">The table border to get</param>
- public Border GetBorder( TableBorderType borderType )
- {
- // instance with default border values
- var b = new Border();
-
- /*
- * Get the tblPr (table properties) element for this Table,
- * null will be return if no such element exists.
- */
- var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- if( tblPr == null )
- {
- // uses default border style
- }
-
- /*
- * Get the tblBorders (table borders) element for this Table,
- * null will be return if no such element exists.
- */
- var tblBorders = tblPr.Element( XName.Get( "tblBorders", DocX.w.NamespaceName ) );
- if( tblBorders == null )
- {
- // uses default border style
- }
-
- /*
- * Get the 'borderType' (table border) element for this Table,
- * null will be return if no such element exists.
- */
- string tbordertype = borderType.ToString();
- // only lower the first char of string (because of insideH and insideV)
- tbordertype = tbordertype.Substring( 0, 1 ).ToLower() + tbordertype.Substring( 1 );
-
- var tblBorderType = tblBorders.Element( XName.Get( tbordertype, DocX.w.NamespaceName ) );
- if( tblBorderType == null )
- {
- // uses default border style
- }
-
- // The val attribute is used for the border style
- var val = tblBorderType.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
- // If val is null, this table contains no border information.
- if( val == null )
- {
- // uses default border style
- }
- else
- {
- try
- {
- var bordertype = "Tcbs_" + val.Value;
- b.Tcbs = ( BorderStyle )Enum.Parse( typeof( BorderStyle ), bordertype );
- }
- catch
- {
- val.Remove();
- // uses default border style
- }
- }
-
- // The sz attribute is used for the border size
- var sz = tblBorderType.Attribute( XName.Get( "sz", DocX.w.NamespaceName ) );
- // If sz is null, this border contains no size information.
- if( sz == null )
- {
- // uses default border style
- }
- else
- {
- // If sz is not an int, something is wrong with this attributes value, so remove it
- int numerical_size;
- if( !int.TryParse( sz.Value, out numerical_size ) )
- {
- sz.Remove();
- }
- else
- {
- switch( numerical_size )
- {
- case 2:
- b.Size = BorderSize.one;
- break;
- case 4:
- b.Size = BorderSize.two;
- break;
- case 6:
- b.Size = BorderSize.three;
- break;
- case 8:
- b.Size = BorderSize.four;
- break;
- case 12:
- b.Size = BorderSize.five;
- break;
- case 18:
- b.Size = BorderSize.six;
- break;
- case 24:
- b.Size = BorderSize.seven;
- break;
- case 36:
- b.Size = BorderSize.eight;
- break;
- case 48:
- b.Size = BorderSize.nine;
- break;
- default:
- b.Size = BorderSize.one;
- break;
- }
- }
- }
-
- // The space attribute is used for the border spacing (probably '0')
- var space = tblBorderType.Attribute( XName.Get( "space", DocX.w.NamespaceName ) );
- // If space is null, this border contains no space information.
- if( space == null )
- {
- // uses default border style
- }
- else
- {
- // If space is not an int, something is wrong with this attributes value, so remove it
- int borderspace;
- if( !int.TryParse( space.Value, out borderspace ) )
- {
- space.Remove();
- // uses default border style
- }
- else
- {
- b.Space = borderspace;
- }
- }
-
- // The color attribute is used for the border color
- var color = tblBorderType.Attribute( XName.Get( "color", DocX.w.NamespaceName ) );
- if( color == null )
- {
- // uses default border style
- }
- else
- {
- // If color is not a Color, something is wrong with this attributes value, so remove it
- try
- {
- b.Color = ColorTranslator.FromHtml( string.Format( "#{0}", color.Value ) );
- }
- catch
- {
- color.Remove();
- // uses default border style
- }
- }
- return b;
- }
-
- public Double GetColumnWidth( Int32 columnIndex )
- {
- List<Double> columnWidths = ColumnWidths;
- if( columnWidths == null || columnIndex > columnWidths.Count - 1 )
- return Double.NaN;
-
- return columnWidths[ columnIndex ];
- }
-
- public void SetColumnWidth( int columnIndex, double width )
- {
- var columnWidths = this.ColumnWidths;
- if( columnWidths == null || ( columnIndex > columnWidths.Count - 1 ) )
- {
- if( this.Rows.Count == 0 )
- throw new Exception( "There is at least one row required to detect the existing columns." );
-
- columnWidths = new List<Double>();
- foreach( Cell c in Rows[ Rows.Count - 1 ].Cells )
- {
- columnWidths.Add( c.Width );
- }
- }
-
- // check if the columnIndex is valid
- if( columnIndex > (columnWidths.Count - 1) )
- throw new Exception( "The index is greather than the available table columns." );
-
- // append a new grid if null
- var grid = Xml.Element( XName.Get( "tblGrid", DocX.w.NamespaceName ) );
- if( grid == null )
- {
- var tblPr = GetOrCreate_tblPr();
- tblPr.AddAfterSelf( new XElement( XName.Get( "tblGrid", DocX.w.NamespaceName ) ) );
- grid = Xml.Element( XName.Get( "tblGrid", DocX.w.NamespaceName ) );
- }
-
- // remove all existing values
- grid?.RemoveAll();
-
- // append new column widths
- int index = 0;
- foreach( var columnWidth in columnWidths )
- {
- double newWidth = columnWidth;
- if( index == columnIndex )
- {
- newWidth = width;
- }
-
- var newColumn = new XElement( XName.Get( "gridCol", DocX.w.NamespaceName ), new XAttribute( XName.Get( "w", DocX.w.NamespaceName ), newWidth ) );
- grid?.Add( newColumn );
- index += 1;
- }
-
- // remove cell widths
- foreach( Row row in this.Rows )
- {
- foreach( Cell cell in row.Cells )
- {
- cell.Width = -1;
- }
- }
-
- // set AutoFit to Fixed
- this.AutoFit = AutoFit.Fixed;
- }
-
- public void SetTableCellMargin( TableCellMarginType type, double margin )
- {
- var tblPr = this.GetOrCreate_tblPr();
- var tblCellMarXName = XName.Get( "tblCellMar", DocX.w.NamespaceName );
- var typeXName = XName.Get( type.ToString(), DocX.w.NamespaceName );
-
- var tblCellMar = tblPr.Element( tblCellMarXName );
- if( tblCellMar == null )
- {
- tblPr.AddFirst( new XElement( tblCellMarXName ) );
- tblCellMar = tblPr.Element( tblCellMarXName );
- }
-
- var side = tblCellMar.Element( typeXName );
- if( side == null )
- {
- tblCellMar.AddFirst( new XElement( typeXName ) );
- side = tblCellMar.Element( typeXName );
- }
-
- side.RemoveAttributes();
- // Set value and side for cell Margin
- side.Add( new XAttribute( XName.Get( "w", DocX.w.NamespaceName ), margin ) );
- side.Add( new XAttribute( XName.Get( "type", DocX.w.NamespaceName ), "dxa" ) );
- }
-
- /// <summary>
- /// Deletes a cell in a row and shift the others to the left.
- /// </summary>
- /// <param name="rowIndex">index of the row where a cell will be removed.</param>
- /// <param name="celIndex">index of the cell to remove in the row.</param>
- public void DeleteAndShiftCellsLeft( int rowIndex, int celIndex )
- {
- var trPr = this.Rows[ rowIndex ].Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
- if( trPr != null )
- {
- var gridAfter = trPr.Element( XName.Get( "gridAfter", DocX.w.NamespaceName ) );
- if( gridAfter != null )
- {
- var gridAfterValAttr = gridAfter.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
- gridAfterValAttr.Value = ( gridAfterValAttr != null ) ? int.Parse( gridAfterValAttr.Value ).ToString() : "1";
- }
- else
- {
- gridAfter.SetAttributeValue( "val", 1 );
- }
- }
- else
- {
- var gridAfterXElement = new XElement( XName.Get( "gridAfter", DocX.w.NamespaceName ) );
- var valXAttribute = new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), 1 );
- gridAfterXElement.Add( valXAttribute );
-
- var trPrXElement = new XElement( XName.Get( "trPr", DocX.w.NamespaceName ) );
- trPrXElement.Add( gridAfterXElement );
-
- this.Rows[ rowIndex ].Xml.AddFirst( trPrXElement );
- }
-
- if( ( celIndex <= this.ColumnCount ) && ( this.Rows[ rowIndex ].ColumnCount <= this.ColumnCount ) )
- {
- this.Rows[ rowIndex ].Cells[ celIndex ].Xml.Remove();
- }
- }
-
- #endregion
-
- #region Internal Methods
-
- /// <summary>
- /// If the tblPr element doesent exist it is created, either way it is returned by this function.
- /// </summary>
- /// <returns>The tblPr element for this Table.</returns>
- internal XElement GetOrCreate_tblPr()
- {
- // Get the element.
- var tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
-
- // If it dosen't exist, create it.
- if( tblPr == null )
- {
- this.Xml.AddFirst( new XElement( XName.Get( "tblPr", DocX.w.NamespaceName ) ) );
- tblPr = Xml.Element( XName.Get( "tblPr", DocX.w.NamespaceName ) );
- }
-
- // Return the pPr element for this Paragraph.
- return tblPr;
- }
-
- #endregion
-
- #region Private Methods
-
- private Row InsertRow( List<XElement> content, Int32 index )
- {
- Row newRow = new Row( this, Document, new XElement( XName.Get( "tr", DocX.w.NamespaceName ), content ) );
-
- XElement rowXml;
- if( index == Rows.Count )
- {
- rowXml = Rows.Last().Xml;
- rowXml.AddAfterSelf( newRow.Xml );
- }
-
- else
- {
- rowXml = Rows[ index ].Xml;
- rowXml.AddBeforeSelf( newRow.Xml );
- }
-
- return newRow;
- }
-
- private void AddCellToRow( Row row, XElement cell, int index, bool direction )
- {
- if( index >= row.Cells.Count )
- throw new IndexOutOfRangeException( "index is greater or equals to row.Cells.Count." );
-
- if( direction )
- {
- row.Cells[ index ].Xml.AddAfterSelf( cell );
- }
- else
- {
- row.Cells[ index ].Xml.AddBeforeSelf( cell );
- }
- }
-
- #endregion
- }
-
- /// <summary>
- /// Represents a single row in a Table.
- /// </summary>
- public class Row : Container
- {
- #region Internal Members
-
- internal Table _table;
-
- #endregion
-
- #region Public Properties
-
- /// <summary>
- /// Calculates columns count in the row, taking spanned cells into account
- /// </summary>
- public Int32 ColumnCount
- {
- get
- {
- int gridSpanSum = this.GridAfter;
-
- // Foreach each Cell between startIndex and endIndex inclusive.
- foreach( Cell c in Cells )
- {
- if( c.GridSpan != 0 )
- {
- gridSpanSum += (c.GridSpan - 1);
- }
- }
-
- // return cells count + count of spanned cells
- return Cells.Count + gridSpanSum;
- }
- }
-
- /// <summary>
- /// Returns the row.GridAfter => The number of deleted cells in a row.
- /// </summary>
- public int GridAfter
- {
- get
- {
- var trPr = this.Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
- if( trPr != null )
- {
- var gridAfter = trPr.Element( XName.Get( "gridAfter", DocX.w.NamespaceName ) );
- var gridAfterAttrVal = gridAfter?.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
- if( gridAfterAttrVal != null )
- {
- return int.Parse( gridAfterAttrVal.Value );
- }
- }
-
- return 0;
- }
- }
-
- /// <summary>
- /// A list of Cells in this Row.
- /// </summary>
- public List<Cell> Cells
- {
- get
- {
- List<Cell> cells =
- (
- from c in Xml.Elements( XName.Get( "tc", DocX.w.NamespaceName ) )
- select new Cell( this, Document, c )
- ).ToList();
-
- return cells;
- }
- }
-
- public override ReadOnlyCollection<Paragraph> Paragraphs
- {
- get
- {
- var paragraphs =
- (
- from p in Xml.Descendants( DocX.w + "p" )
- select new Paragraph( Document, p, 0 )
- ).ToList();
-
- foreach( Paragraph p in paragraphs )
- {
- p.PackagePart = _table.PackagePart;
- }
-
- return paragraphs.AsReadOnly();
- }
- }
-
- /// <summary>
- /// Height in pixels.
- /// </summary>
- public double Height
- {
- get
- {
- /*
- * Get the trPr (table row properties) element for this Row,
- * null will be return if no such element exists.
- */
- XElement trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
-
- // If trPr is null, this row contains no height information.
- // Get the trHeight element for this Row,
- // null will be return if no such element exists.
- XElement trHeight = trPr?.Element( XName.Get( "trHeight", DocX.w.NamespaceName ) );
-
- // If trHeight is null, this row contains no height information.
- // Get the val attribute for this trHeight element.
- XAttribute val = trHeight?.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
-
- // If w is null, this cell contains no width information.
- if( val == null )
- return double.NaN;
-
- // If val is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
- double heightInWordUnits;
- if( !double.TryParse( val.Value, out heightInWordUnits ) )
- {
- val.Remove();
- return double.NaN;
- }
-
- // Using 20 to match DocX._pageSizeMultiplier.
- return ( heightInWordUnits / 20 );
- }
-
- set
- {
- SetHeight( value, true );
- }
- }
-
- /// <summary>
- /// Minimum Height in pixels.
- /// </summary>
- public double MinHeight
- {
- get
- {
- return Height;
- }
- set
- {
- SetHeight( value, false );
- }
- }
-
- public bool TableHeader
- {
- get
- {
- XElement trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
- XElement tblHeader = trPr?.Element( XName.Get( "tblHeader", DocX.w.NamespaceName ) );
- return tblHeader != null;
- }
- set
- {
- XElement trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
- if( trPr == null )
- {
- Xml.SetElementValue( XName.Get( "trPr", DocX.w.NamespaceName ), string.Empty );
- trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
- }
-
- XElement tblHeader = trPr.Element( XName.Get( "tblHeader", DocX.w.NamespaceName ) );
-
- if( tblHeader == null && value )
- trPr.SetElementValue( XName.Get( "tblHeader", DocX.w.NamespaceName ), string.Empty );
-
- if( tblHeader != null && !value )
- tblHeader.Remove();
- }
- }
-
- /// <summary>
- /// Allow row to break across pages.
- /// Default value is True : Word will break the contents of the row across the pages.
- /// When False, the contents of the row will not be split across the pages; it will be entirely moved to the next page.
- /// </summary>
- public bool BreakAcrossPages
- {
- get
- {
- var trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
- var cantSplit = trPr?.Element( XName.Get( "cantSplit", DocX.w.NamespaceName ) );
- return cantSplit == null;
- }
- set
- {
- var trPrXName = XName.Get( "trPr", DocX.w.NamespaceName );
- var cantSplitXName = XName.Get( "cantSplit", DocX.w.NamespaceName );
-
- if( value )
- {
- var trPr = Xml.Element( trPrXName );
- var cantSplit = trPr?.Element( cantSplitXName );
- if( cantSplit != null )
- cantSplit.Remove();
- }
- else
- {
- var trPr = Xml.Element( trPrXName );
- if( trPr == null )
- {
- Xml.SetElementValue( trPrXName, string.Empty );
- trPr = Xml.Element( trPrXName );
- }
- var cantSplit = trPr.Element( cantSplitXName );
- if( cantSplit == null )
- {
- trPr.SetElementValue( cantSplitXName, string.Empty );
- }
- }
- }
- }
-
- #endregion
-
- #region Constructors
-
- internal Row( Table table, DocX document, XElement xml )
- : base( document, xml )
- {
- _table = table;
- this.PackagePart = table.PackagePart;
- }
-
- #endregion
-
- #region Private Methods
-
- private void SetHeight( double height, bool isHeightExact )
- {
- XElement trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
- if( trPr == null )
- {
- Xml.SetElementValue( XName.Get( "trPr", DocX.w.NamespaceName ), string.Empty );
- trPr = Xml.Element( XName.Get( "trPr", DocX.w.NamespaceName ) );
- }
-
- XElement tc = Xml.Element( XName.Get( "tc", DocX.w.NamespaceName ) );
- if( tc != null )
- {
- trPr.Remove();
- tc.AddBeforeSelf( trPr );
- }
-
- XElement trHeight = trPr.Element( XName.Get( "trHeight", DocX.w.NamespaceName ) );
- if( trHeight == null )
- {
- trPr.SetElementValue( XName.Get( "trHeight", DocX.w.NamespaceName ), string.Empty );
- trHeight = trPr.Element( XName.Get( "trHeight", DocX.w.NamespaceName ) );
- }
-
- // The hRule attribute needs to be set.
- trHeight.SetAttributeValue( XName.Get( "hRule", DocX.w.NamespaceName ), isHeightExact ? "exact" : "atLeast" );
-
- // Using 20 to match DocX._pageSizeMultiplier.
- trHeight.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), ( ( int )( Math.Round( height * 20, 0 ) ) ).ToString( CultureInfo.InvariantCulture ) );
- }
-
- #endregion
-
- #region Public Methods
-
- public void Remove()
- {
- XElement table = Xml.Parent;
-
- Xml.Remove();
- if( !table.Elements( XName.Get( "tr", DocX.w.NamespaceName ) ).Any() )
- table.Remove();
- }
-
- /// <summary>
- /// Merge cells starting with startIndex and ending with endIndex.
- /// </summary>
- public void MergeCells( int startIndex, int endIndex )
- {
- // Check for valid start and end indexes.
- if( startIndex < 0 || endIndex <= startIndex || endIndex > Cells.Count + 1 )
- throw new IndexOutOfRangeException();
-
- // The sum of all merged gridSpans.
- int gridSpanSum = 0;
-
- // Foreach each Cell between startIndex and endIndex inclusive.
- foreach( Cell c in Cells.Where( ( z, i ) => i > startIndex && i <= endIndex ) )
- {
- XElement tcPr = c.Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- XElement gridSpan = tcPr?.Element( XName.Get( "gridSpan", DocX.w.NamespaceName ) );
- if( gridSpan != null )
- {
- XAttribute val = gridSpan.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
-
- int value;
- if( val != null && int.TryParse( val.Value, out value ) )
- gridSpanSum += value - 1;
- }
-
- // Add this cells Pragraph to the merge start Cell.
- Cells[ startIndex ].Xml.Add( c.Xml.Elements( XName.Get( "p", DocX.w.NamespaceName ) ) );
-
- // Remove this Cell.
- c.Xml.Remove();
- }
-
- // Trim cell's paragraphs to remove extra blank lines, if any
- int index = 0;
- do
- {
- // If the cell doesn't have multiple paragraphs, leave the loop
- if ( Cells[ startIndex ].Paragraphs.Count < 2 )
- break;
-
- // Remove the last paragraph if it's a blank line, otherwise trimming is done
- index = Cells[ startIndex ].Paragraphs.Count - 1;
- if ( Cells[ startIndex ].Paragraphs[ index ].Text.Trim() == "" )
- Cells[ startIndex ].Paragraphs[ index ].Remove( false );
- else
- break;
- } while( true );
-
- /*
- * Get the tcPr (table cell properties) element for the first cell in this merge,
- * null will be returned if no such element exists.
- */
- XElement start_tcPr = Cells[ startIndex ].Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( start_tcPr == null )
- {
- Cells[ startIndex ].Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- start_tcPr = Cells[ startIndex ].Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the gridSpan element of this row,
- * null will be returned if no such element exists.
- */
- XElement start_gridSpan = start_tcPr.Element( XName.Get( "gridSpan", DocX.w.NamespaceName ) );
- if( start_gridSpan == null )
- {
- start_tcPr.SetElementValue( XName.Get( "gridSpan", DocX.w.NamespaceName ), string.Empty );
- start_gridSpan = start_tcPr.Element( XName.Get( "gridSpan", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the val attribute of this row,
- * null will be returned if no such element exists.
- */
- XAttribute start_val = start_gridSpan.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
-
- int start_value = 0;
- if( start_val != null )
- if( int.TryParse( start_val.Value, out start_value ) )
- gridSpanSum += start_value - 1;
-
- // Set the val attribute to the number of merged cells.
- start_gridSpan.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), ( gridSpanSum + ( endIndex - startIndex + 1 ) ).ToString() );
- }
-
- #endregion
- }
-
- public class Cell : Container
- {
- #region Internal Members
-
- internal Row _row;
-
- #endregion
-
- #region Public Properties
-
- public override ReadOnlyCollection<Paragraph> Paragraphs
- {
- get
- {
- var paragraphs = base.Paragraphs;
-
- foreach( Paragraph p in paragraphs )
- {
- p.PackagePart = _row._table.PackagePart;
- }
-
- return paragraphs;
- }
- }
-
- // <summary>
- // Gets or Sets this Cells vertical alignment.
- // </summary>
- // <example>
- // Creates a table with 3 cells and sets the vertical alignment of each to 1 of the 3 available options.
- // <code>
- // Create a new document.
- //using(DocX document = DocX.Create("Test.docx"))
- //{
- // // Insert a Table into this document.
- // Table t = document.InsertTable(3, 1);
- //
- // // Set the design of the Table such that we can easily identify cell boundaries.
- // t.Design = TableDesign.TableGrid;
- //
- // // Set the height of the row bigger than default.
- // // We need to be able to see the difference in vertical cell alignment options.
- // t.Rows[0].Height = 100;
- //
- // // Set the vertical alignment of cell0 to top.
- // Cell c0 = t.Rows[0].Cells[0];
- // c0.InsertParagraph("VerticalAlignment.Top");
- // c0.VerticalAlignment = VerticalAlignment.Top;
- //
- // // Set the vertical alignment of cell1 to center.
- // Cell c1 = t.Rows[0].Cells[1];
- // c1.InsertParagraph("VerticalAlignment.Center");
- // c1.VerticalAlignment = VerticalAlignment.Center;
- //
- // // Set the vertical alignment of cell2 to bottom.
- // Cell c2 = t.Rows[0].Cells[2];
- // c2.InsertParagraph("VerticalAlignment.Bottom");
- // c2.VerticalAlignment = VerticalAlignment.Bottom;
- //
- // // Save the document.
- // document.Save();
- //}
- // </code>
- // </example>
- public VerticalAlignment VerticalAlignment
- {
- get
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
-
- // If tcPr is null, this cell contains no width information.
- // Get the vAlign (table cell vertical alignment) element for this Cell,
- // null will be return if no such element exists.
- XElement vAlign = tcPr?.Element( XName.Get( "vAlign", DocX.w.NamespaceName ) );
-
- // If vAlign is null, this cell contains no vertical alignment information.
- // Get the val attribute of the vAlign element.
- XAttribute val = vAlign?.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
-
- // If val is null, this cell contains no vAlign information.
- if( val == null )
- return VerticalAlignment.Top;
-
- // If val is not a VerticalAlign enum, something is wrong with this attributes value, so remove it and return VerticalAlignment.Center;
- try
- {
- return ( VerticalAlignment )Enum.Parse( typeof( VerticalAlignment ), val.Value, true );
- }
-
- catch
- {
- val.Remove();
- return VerticalAlignment.Top;
- }
- }
-
- set
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the vAlign (table cell vertical alignment) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement vAlign = tcPr.Element( XName.Get( "vAlign", DocX.w.NamespaceName ) );
- if( vAlign == null )
- {
- tcPr.SetElementValue( XName.Get( "vAlign", DocX.w.NamespaceName ), string.Empty );
- vAlign = tcPr.Element( XName.Get( "vAlign", DocX.w.NamespaceName ) );
- }
-
- // Set the VerticalAlignment in 'val'
- vAlign.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), value.ToString().ToLower() );
- }
- }
-
- public Color Shading
- {
- get
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
-
- // If tcPr is null, this cell contains no Color information.
- // Get the shd (table shade) element for this Cell,
- // null will be return if no such element exists.
- XElement shd = tcPr?.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
-
- // If shd is null, this cell contains no Color information.
- // Get the w attribute of the tcW element.
- XAttribute fill = shd?.Attribute( XName.Get( "fill", DocX.w.NamespaceName ) );
-
- // If fill is null, this cell contains no Color information.
- if( fill == null )
- return Color.White;
-
- return ColorTranslator.FromHtml( string.Format( "#{0}", fill.Value ) );
- }
-
- set
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the shd (table shade) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement shd = tcPr.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
- if( shd == null )
- {
- tcPr.SetElementValue( XName.Get( "shd", DocX.w.NamespaceName ), string.Empty );
- shd = tcPr.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
- }
-
- // The val attribute needs to be set to clear
- shd.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), "clear" );
-
- // The color attribute needs to be set to auto
- shd.SetAttributeValue( XName.Get( "color", DocX.w.NamespaceName ), "auto" );
-
- // The fill attribute needs to be set to the hex for this Color.
- shd.SetAttributeValue( XName.Get( "fill", DocX.w.NamespaceName ), value.ToHex() );
- }
- }
-
- /// <summary>
- /// Width in pixels.
- /// </summary>
- public double Width
- {
- get
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
-
- // If tcPr is null, this cell contains no width information.
- // Get the tcW (table cell width) element for this Cell,
- // null will be return if no such element exists.
- XElement tcW = tcPr?.Element( XName.Get( "tcW", DocX.w.NamespaceName ) );
-
- // If tcW is null, this cell contains no width information.
- // Get the w attribute of the tcW element.
- XAttribute w = tcW?.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
- XAttribute type = tcW?.Attribute( XName.Get( "type", DocX.w.NamespaceName ) );
-
- // If w is null, this cell contains no width information.
- if( w == null )
- return double.NaN;
-
- // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
- double widthInWordUnits;
- if( !double.TryParse( w.Value, out widthInWordUnits ) )
- {
- w.Remove();
- return double.NaN;
- }
-
- if( type != null )
- {
- if( type.Value == "pct" )
- {
- return ( ( widthInWordUnits / 5000d ) * this._row._table.ColumnWidths.Sum() ) / 20;
- }
- else if( type.Value == "auto" )
- {
- var cellIndex = this._row.Cells.FindIndex( x => x.Xml == this.Xml );
- if( cellIndex >= 0 )
- return this._row._table.ColumnWidths[ cellIndex ] / 20;
- }
- }
- // Using 20 to match DocX._pageSizeMultiplier.
- return ( widthInWordUnits / 20 );
- }
-
- set
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the tcW (table cell width) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcW = tcPr.Element( XName.Get( "tcW", DocX.w.NamespaceName ) );
- if( tcW == null )
- {
- tcPr.SetElementValue( XName.Get( "tcW", DocX.w.NamespaceName ), string.Empty );
- tcW = tcPr.Element( XName.Get( "tcW", DocX.w.NamespaceName ) );
- }
-
- if( value == -1 )
- {
- // remove cell width; due to set on table prop.
- tcW.Remove();
- return;
- }
-
- // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
- tcW.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "dxa" );
-
- // Using 20 to match DocX._pageSizeMultiplier.
- tcW.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), ( value * 20 ).ToString() );
- }
- }
-
- /// <summary>
- /// LeftMargin in pixels.
- /// </summary>
- /// <example>
- /// <code>
- /// // Create a new document.
- ///using (DocX document = DocX.Create("Test.docx"))
- ///{
- /// // Insert table into this document.
- /// Table t = document.InsertTable(3, 3);
- /// t.Design = TableDesign.TableGrid;
- ///
- /// // Get the center cell.
- /// Cell center = t.Rows[1].Cells[1];
- ///
- /// // Insert some text so that we can see the effect of the Margins.
- /// center.Paragraphs[0].Append("Center Cell");
- ///
- /// // Set the center cells Left, Margin to 10.
- /// center.MarginLeft = 25;
- ///
- /// // Save the document.
- /// document.Save();
- ///}
- /// </code>
- /// </example>
- public double MarginLeft
- {
- get
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
-
- // If tcPr is null, this cell contains no width information.
- if( tcPr == null )
- return double.NaN;
-
- /*
- * Get the tcMar
- *
- */
- XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
-
- // If tcMar is null, this cell contains no margin information.
- // Get the left (LeftMargin) element
- XElement tcMarLeft = tcMar?.Element( XName.Get( "left", DocX.w.NamespaceName ) );
-
- // If tcMarLeft is null, this cell contains no left margin information.
- // Get the w attribute of the tcMarLeft element.
- XAttribute w = tcMarLeft?.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
-
- // If w is null, this cell contains no width information.
- if( w == null )
- return double.NaN;
-
- // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
- double leftMarginInWordUnits;
- if( !double.TryParse( w.Value, out leftMarginInWordUnits ) )
- {
- w.Remove();
- return double.NaN;
- }
-
- // Using 20 to match DocX._pageSizeMultiplier.
- return ( leftMarginInWordUnits / 20 );
- }
-
- set
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the tcMar (table cell margin) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
- if( tcMar == null )
- {
- tcPr.SetElementValue( XName.Get( "tcMar", DocX.w.NamespaceName ), string.Empty );
- tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the left (table cell left margin) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcMarLeft = tcMar.Element( XName.Get( "left", DocX.w.NamespaceName ) );
- if( tcMarLeft == null )
- {
- tcMar.SetElementValue( XName.Get( "left", DocX.w.NamespaceName ), string.Empty );
- tcMarLeft = tcMar.Element( XName.Get( "left", DocX.w.NamespaceName ) );
- }
-
- // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
- tcMarLeft.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "dxa" );
-
- // Using 20 to match DocX._pageSizeMultiplier.
- tcMarLeft.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), ( value * 20 ).ToString() );
- }
- }
-
- /// <summary>
- /// RightMargin in pixels.
- /// </summary>
- /// <example>
- /// <code>
- /// // Create a new document.
- ///using (DocX document = DocX.Create("Test.docx"))
- ///{
- /// // Insert table into this document.
- /// Table t = document.InsertTable(3, 3);
- /// t.Design = TableDesign.TableGrid;
- ///
- /// // Get the center cell.
- /// Cell center = t.Rows[1].Cells[1];
- ///
- /// // Insert some text so that we can see the effect of the Margins.
- /// center.Paragraphs[0].Append("Center Cell");
- ///
- /// // Set the center cells Right, Margin to 10.
- /// center.MarginRight = 25;
- ///
- /// // Save the document.
- /// document.Save();
- ///}
- /// </code>
- /// </example>
- public double MarginRight
- {
- get
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
-
- // If tcPr is null, this cell contains no width information.
- if( tcPr == null )
- return double.NaN;
-
- /*
- * Get the tcMar
- *
- */
- XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
-
- // If tcMar is null, this cell contains no margin information.
- // Get the right (RightMargin) element
- XElement tcMarRight = tcMar?.Element( XName.Get( "right", DocX.w.NamespaceName ) );
-
- // If tcMarRight is null, this cell contains no right margin information.
- // Get the w attribute of the tcMarRight element.
- XAttribute w = tcMarRight?.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
-
- // If w is null, this cell contains no width information.
- if( w == null )
- return double.NaN;
-
- // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
- double rightMarginInWordUnits;
- if( !double.TryParse( w.Value, out rightMarginInWordUnits ) )
- {
- w.Remove();
- return double.NaN;
- }
-
- // Using 20 to match DocX._pageSizeMultiplier.
- return ( rightMarginInWordUnits / 20 );
- }
-
- set
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the tcMar (table cell margin) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
- if( tcMar == null )
- {
- tcPr.SetElementValue( XName.Get( "tcMar", DocX.w.NamespaceName ), string.Empty );
- tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the right (table cell right margin) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcMarRight = tcMar.Element( XName.Get( "right", DocX.w.NamespaceName ) );
- if( tcMarRight == null )
- {
- tcMar.SetElementValue( XName.Get( "right", DocX.w.NamespaceName ), string.Empty );
- tcMarRight = tcMar.Element( XName.Get( "right", DocX.w.NamespaceName ) );
- }
-
- // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
- tcMarRight.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "dxa" );
-
- // Using 20 to match DocX._pageSizeMultiplier.
- tcMarRight.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), ( value * 20 ).ToString() );
- }
- }
-
- /// <summary>
- /// TopMargin in pixels.
- /// </summary>
- /// <example>
- /// <code>
- /// // Create a new document.
- ///using (DocX document = DocX.Create("Test.docx"))
- ///{
- /// // Insert table into this document.
- /// Table t = document.InsertTable(3, 3);
- /// t.Design = TableDesign.TableGrid;
- ///
- /// // Get the center cell.
- /// Cell center = t.Rows[1].Cells[1];
- ///
- /// // Insert some text so that we can see the effect of the Margins.
- /// center.Paragraphs[0].Append("Center Cell");
- ///
- /// // Set the center cells Top, Margin to 10.
- /// center.MarginTop = 25;
- ///
- /// // Save the document.
- /// document.Save();
- ///}
- /// </code>
- /// </example>
- public double MarginTop
- {
- get
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
-
- // If tcPr is null, this cell contains no width information.
- if( tcPr == null )
- return double.NaN;
-
- /*
- * Get the tcMar
- *
- */
- XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
-
- // If tcMar is null, this cell contains no margin information.
- // Get the top (TopMargin) element
- XElement tcMarTop = tcMar?.Element( XName.Get( "top", DocX.w.NamespaceName ) );
-
- // If tcMarTop is null, this cell contains no top margin information.
- // Get the w attribute of the tcMarTop element.
- XAttribute w = tcMarTop?.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
-
- // If w is null, this cell contains no width information.
- if( w == null )
- return double.NaN;
-
- // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
- double topMarginInWordUnits;
- if( !double.TryParse( w.Value, out topMarginInWordUnits ) )
- {
- w.Remove();
- return double.NaN;
- }
-
- // Using 20 to match DocX._pageSizeMultiplier.
- return ( topMarginInWordUnits / 20 );
- }
-
- set
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the tcMar (table cell margin) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
- if( tcMar == null )
- {
- tcPr.SetElementValue( XName.Get( "tcMar", DocX.w.NamespaceName ), string.Empty );
- tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the top (table cell top margin) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcMarTop = tcMar.Element( XName.Get( "top", DocX.w.NamespaceName ) );
- if( tcMarTop == null )
- {
- tcMar.SetElementValue( XName.Get( "top", DocX.w.NamespaceName ), string.Empty );
- tcMarTop = tcMar.Element( XName.Get( "top", DocX.w.NamespaceName ) );
- }
-
- // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
- tcMarTop.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "dxa" );
-
- // Using 20 to match DocX._pageSizeMultiplier.
- tcMarTop.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), ( value * 20 ).ToString() );
- }
- }
-
- /// <summary>
- /// BottomMargin in pixels.
- /// </summary>
- /// <example>
- /// <code>
- /// // Create a new document.
- ///using (DocX document = DocX.Create("Test.docx"))
- ///{
- /// // Insert table into this document.
- /// Table t = document.InsertTable(3, 3);
- /// t.Design = TableDesign.TableGrid;
- ///
- /// // Get the center cell.
- /// Cell center = t.Rows[1].Cells[1];
- ///
- /// // Insert some text so that we can see the effect of the Margins.
- /// center.Paragraphs[0].Append("Center Cell");
- ///
- /// // Set the center cells Top, Margin to 10.
- /// center.MarginBottom = 25;
- ///
- /// // Save the document.
- /// document.Save();
- ///}
- /// </code>
- /// </example>
- public double MarginBottom
- {
- get
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
-
- // If tcPr is null, this cell contains no width information.
- if( tcPr == null )
- return double.NaN;
-
- /*
- * Get the tcMar
- *
- */
- XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
-
- // If tcMar is null, this cell contains no margin information.
- // Get the bottom (BottomMargin) element
- XElement tcMarBottom = tcMar?.Element( XName.Get( "bottom", DocX.w.NamespaceName ) );
-
- // If tcMarBottom is null, this cell contains no bottom margin information.
- // Get the w attribute of the tcMarBottom element.
- XAttribute w = tcMarBottom?.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
-
- // If w is null, this cell contains no width information.
- if( w == null )
- return double.NaN;
-
- // If w is not a double, something is wrong with this attributes value, so remove it and return double.NaN;
- double bottomMarginInWordUnits;
- if( !double.TryParse( w.Value, out bottomMarginInWordUnits ) )
- {
- w.Remove();
- return double.NaN;
- }
-
- // Using 20 to match DocX._pageSizeMultiplier.
- return ( bottomMarginInWordUnits / 20 );
- }
-
- set
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the tcMar (table cell margin) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
- if( tcMar == null )
- {
- tcPr.SetElementValue( XName.Get( "tcMar", DocX.w.NamespaceName ), string.Empty );
- tcMar = tcPr.Element( XName.Get( "tcMar", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the bottom (table cell bottom margin) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcMarBottom = tcMar.Element( XName.Get( "bottom", DocX.w.NamespaceName ) );
- if( tcMarBottom == null )
- {
- tcMar.SetElementValue( XName.Get( "bottom", DocX.w.NamespaceName ), string.Empty );
- tcMarBottom = tcMar.Element( XName.Get( "bottom", DocX.w.NamespaceName ) );
- }
-
- // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
- tcMarBottom.SetAttributeValue( XName.Get( "type", DocX.w.NamespaceName ), "dxa" );
-
- // Using 20 to match DocX._pageSizeMultiplier.
- tcMarBottom.SetAttributeValue( XName.Get( "w", DocX.w.NamespaceName ), ( value * 20 ).ToString() );
- }
- }
-
- public Color FillColor
- {
- get
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- XElement shd = tcPr?.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
- XAttribute fill = shd?.Attribute( XName.Get( "fill", DocX.w.NamespaceName ) );
- if( fill == null )
- return Color.Empty;
-
- int argb = Int32.Parse( fill.Value.Replace( "#", "" ), NumberStyles.HexNumber );
- return Color.FromArgb( argb );
- }
-
- set
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the tcW (table cell width) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement shd = tcPr.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
- if( shd == null )
- {
- tcPr.SetElementValue( XName.Get( "shd", DocX.w.NamespaceName ), string.Empty );
- shd = tcPr.Element( XName.Get( "shd", DocX.w.NamespaceName ) );
- }
-
- shd.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), "clear" );
- shd.SetAttributeValue( XName.Get( "color", DocX.w.NamespaceName ), "auto" );
- shd.SetAttributeValue( XName.Get( "fill", DocX.w.NamespaceName ), value.ToHex() );
- }
- }
-
- public TextDirection TextDirection
- {
- get
- {
- var tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- var textDirection = tcPr?.Element( XName.Get( "textDirection", DocX.w.NamespaceName ) );
- var val = textDirection?.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
-
- if( val != null )
- {
- TextDirection textDirectionValue;
- var success = Enum.TryParse( val.Value, out textDirectionValue );
- if( success )
- return textDirectionValue;
- else
- {
- val.Remove();
- }
- }
-
- return TextDirection.right;
- }
-
- set
- {
- var tcPrXName = XName.Get( "tcPr", DocX.w.NamespaceName );
- var textDirectionXName = XName.Get( "textDirection", DocX.w.NamespaceName );
-
- var tcPr = Xml.Element( tcPrXName );
- if( tcPr == null )
- {
- Xml.SetElementValue( tcPrXName, string.Empty );
- tcPr = Xml.Element( tcPrXName );
- }
-
- var textDirection = tcPr.Element( textDirectionXName );
- if( textDirection == null )
- {
- tcPr.SetElementValue( textDirectionXName, string.Empty );
- textDirection = tcPr.Element( textDirectionXName );
- }
-
- textDirection.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), value.ToString() );
- }
- }
-
- /// <summary>
- /// Returns the Cell.GridSpan => How many cells are merged.
- /// </summary>
- public int GridSpan
- {
- get
- {
- int gridSpanValue = 0;
-
- var tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- var gridSpan = tcPr?.Element( XName.Get( "gridSpan", DocX.w.NamespaceName ) );
- if( gridSpan != null )
- {
- var gridSpanAttrValue = gridSpan.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
-
- int value;
- if( gridSpanAttrValue != null && int.TryParse( gridSpanAttrValue.Value, out value ) )
- gridSpanValue = value;
- }
- return gridSpanValue;
- }
- }
-
- #endregion
-
- #region Constructors
-
- internal Cell( Row row, DocX document, XElement xml )
- : base( document, xml )
- {
- _row = row;
- this.PackagePart = row.PackagePart;
- }
-
- #endregion
-
- #region Public Methods
-
- // <summary>
- // Set the table cell border
- // </summary>
- // <example>
- // <code>
- // Create a new document.
- //using (DocX document = DocX.Create("Test.docx"))
- //{
- // // Insert a table into this document.
- // Table t = document.InsertTable(3, 3);
- //
- // // Get the center cell.
- // Cell center = t.Rows[1].Cells[1];
- //
- // // Create a large blue border.
- // Border b = new Border(BorderStyle.Tcbs_single, BorderSize.seven, 0, Color.Blue);
- //
- // // Set the center cells Top, Bottom, Left and Right Borders to b.
- // center.SetBorder(TableCellBorderType.Top, b);
- // center.SetBorder(TableCellBorderType.Bottom, b);
- // center.SetBorder(TableCellBorderType.Left, b);
- // center.SetBorder(TableCellBorderType.Right, b);
- //
- // // Save the document.
- // document.Save();
- //}
- // </code>
- // </example>
- // <param name="borderType">Table Cell border to set</param>
- // <param name="border">Border object to set the table cell border</param>
- public void SetBorder( TableCellBorderType borderType, Border border )
- {
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- Xml.SetElementValue( XName.Get( "tcPr", DocX.w.NamespaceName ), string.Empty );
- tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the tblBorders (table cell borders) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcBorders = tcPr.Element( XName.Get( "tcBorders", DocX.w.NamespaceName ) );
- if( tcBorders == null )
- {
- tcPr.SetElementValue( XName.Get( "tcBorders", DocX.w.NamespaceName ), string.Empty );
- tcBorders = tcPr.Element( XName.Get( "tcBorders", DocX.w.NamespaceName ) );
- }
-
- /*
- * Get the 'borderType' (table cell border) element for this Cell,
- * null will be return if no such element exists.
- */
- var tcbordertype = borderType.ToString();
- switch( borderType )
- {
- case TableCellBorderType.TopLeftToBottomRight:
- tcbordertype = "tl2br";
- break;
- case TableCellBorderType.TopRightToBottomLeft:
- tcbordertype = "tr2bl";
- break;
- default:
- // only lower the first char of string (because of insideH and insideV)
- tcbordertype = tcbordertype.Substring( 0, 1 ).ToLower() + tcbordertype.Substring( 1 );
- break;
- }
-
- XElement tcBorderType = tcBorders.Element( XName.Get( borderType.ToString(), DocX.w.NamespaceName ) );
- if( tcBorderType == null )
- {
- tcBorders.SetElementValue( XName.Get( tcbordertype, DocX.w.NamespaceName ), string.Empty );
- tcBorderType = tcBorders.Element( XName.Get( tcbordertype, DocX.w.NamespaceName ) );
- }
-
- // get string value of border style
- string borderstyle = border.Tcbs.ToString().Substring( 5 );
- borderstyle = borderstyle.Substring( 0, 1 ).ToLower() + borderstyle.Substring( 1 );
-
- // The val attribute is used for the border style
- tcBorderType.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), borderstyle );
-
- int size;
- switch( border.Size )
- {
- case BorderSize.one:
- size = 2;
- break;
- case BorderSize.two:
- size = 4;
- break;
- case BorderSize.three:
- size = 6;
- break;
- case BorderSize.four:
- size = 8;
- break;
- case BorderSize.five:
- size = 12;
- break;
- case BorderSize.six:
- size = 18;
- break;
- case BorderSize.seven:
- size = 24;
- break;
- case BorderSize.eight:
- size = 36;
- break;
- case BorderSize.nine:
- size = 48;
- break;
- default:
- size = 2;
- break;
- }
-
- // The sz attribute is used for the border size
- tcBorderType.SetAttributeValue( XName.Get( "sz", DocX.w.NamespaceName ), ( size ).ToString() );
-
- // The space attribute is used for the cell spacing (probably '0')
- tcBorderType.SetAttributeValue( XName.Get( "space", DocX.w.NamespaceName ), ( border.Space ).ToString() );
-
- // The color attribute is used for the border color
- tcBorderType.SetAttributeValue( XName.Get( "color", DocX.w.NamespaceName ), border.Color.ToHex() );
- }
-
- /// <summary>
- /// Get a table cell border
- /// </summary>
- /// <param name="borderType">The table cell border to get</param>
- public Border GetBorder( TableCellBorderType borderType )
- {
- // instance with default border values
- var b = new Border();
-
- /*
- * Get the tcPr (table cell properties) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcPr = Xml.Element( XName.Get( "tcPr", DocX.w.NamespaceName ) );
- if( tcPr == null )
- {
- // uses default border style
- }
-
- /*
- * Get the tcBorders (table cell borders) element for this Cell,
- * null will be return if no such element exists.
- */
- XElement tcBorders = tcPr.Element( XName.Get( "tcBorders", DocX.w.NamespaceName ) );
- if( tcBorders == null )
- {
- // uses default border style
- }
-
- /*
- * Get the 'borderType' (cell border) element for this Cell,
- * null will be return if no such element exists.
- */
- var tcbordertype = borderType.ToString();
- switch( tcbordertype )
- {
- case "TopLeftToBottomRight":
- tcbordertype = "tl2br";
- break;
- case "TopRightToBottomLeft":
- tcbordertype = "tr2bl";
- break;
- default:
- // only lower the first char of string (because of insideH and insideV)
- tcbordertype = tcbordertype.Substring( 0, 1 ).ToLower() + tcbordertype.Substring( 1 );
- break;
- }
-
- XElement tcBorderType = tcBorders.Element( XName.Get( tcbordertype, DocX.w.NamespaceName ) );
- if( tcBorderType == null )
- {
- // uses default border style
- }
-
- // The val attribute is used for the border style
- XAttribute val = tcBorderType.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
- // If val is null, this cell contains no border information.
- if( val == null )
- {
- // uses default border style
- }
- else
- {
- try
- {
- string bordertype = "Tcbs_" + val.Value;
- b.Tcbs = ( BorderStyle )Enum.Parse( typeof( BorderStyle ), bordertype );
- }
-
- catch
- {
- val.Remove();
- // uses default border style
- }
- }
-
- // The sz attribute is used for the border size
- XAttribute sz = tcBorderType.Attribute( XName.Get( "sz", DocX.w.NamespaceName ) );
- // If sz is null, this border contains no size information.
- if( sz == null )
- {
- // uses default border style
- }
- else
- {
- // If sz is not an int, something is wrong with this attributes value, so remove it
- int numerical_size;
- if( !int.TryParse( sz.Value, out numerical_size ) )
- sz.Remove();
- else
- {
- switch( numerical_size )
- {
- case 2:
- b.Size = BorderSize.one;
- break;
- case 4:
- b.Size = BorderSize.two;
- break;
- case 6:
- b.Size = BorderSize.three;
- break;
- case 8:
- b.Size = BorderSize.four;
- break;
- case 12:
- b.Size = BorderSize.five;
- break;
- case 18:
- b.Size = BorderSize.six;
- break;
- case 24:
- b.Size = BorderSize.seven;
- break;
- case 36:
- b.Size = BorderSize.eight;
- break;
- case 48:
- b.Size = BorderSize.nine;
- break;
- default:
- b.Size = BorderSize.one;
- break;
- }
- }
- }
-
- // The space attribute is used for the border spacing (probably '0')
- XAttribute space = tcBorderType.Attribute( XName.Get( "space", DocX.w.NamespaceName ) );
- // If space is null, this border contains no space information.
- if( space == null )
- {
- // uses default border style
- }
- else
- {
- // If space is not an int, something is wrong with this attributes value, so remove it
- int borderspace;
- if( !int.TryParse( space.Value, out borderspace ) )
- {
- space.Remove();
- // uses default border style
- }
- else
- {
- b.Space = borderspace;
- }
- }
-
- // The color attribute is used for the border color
- XAttribute color = tcBorderType.Attribute( XName.Get( "color", DocX.w.NamespaceName ) );
- if( color == null )
- {
- // uses default border style
- }
- else
- {
- // If color is not a Color, something is wrong with this attributes value, so remove it
- try
- {
- b.Color = ColorTranslator.FromHtml( string.Format( "#{0}", color.Value ) );
- }
- catch
- {
- color.Remove();
- // uses default border style
- }
- }
- return b;
- }
-
- public override Table InsertTable( int rowCount, int columnCount )
- {
- var table = base.InsertTable( rowCount, columnCount );
- table.PackagePart = this.PackagePart;
- this.InsertParagraph(); //It is necessary to put paragraph in the end of the cell, without it MS-Word will say that the document is corrupted
- //IMPORTANT: It will be better to check all methods that work with adding anything to cells
- return table;
- }
-
- #endregion
-
- // <summary>
- // Gets or Sets the fill color of this Cell.
- // </summary>
- // <example>
- // <code>
- // // Create a new document.
- // using (DocX document = DocX.Create("Test.docx"))
- // {
- // // Insert a table into this document.
- // Table t = document.InsertTable(3, 3);
- //
- // // Fill the first cell as Blue.
- // t.Rows[0].Cells[0].FillColor = Color.Blue;
- // // Fill the middle cell as Red.
- // t.Rows[1].Cells[1].FillColor = Color.Red;
- // // Fill the last cell as Green.
- // t.Rows[2].Cells[2].FillColor = Color.Green;
- //
- // // Save the document.
- // document.Save();
- // }
- // </code>
- // </example>
- }
-
- public class TableLook
- {
- #region Public Properties
-
- public bool FirstRow
- {
- get;
- set;
- }
-
- public bool LastRow
- {
- get;
- set;
- }
-
- public bool FirstColumn
- {
- get;
- set;
- }
-
- public bool LastColumn
- {
- get;
- set;
- }
-
- public bool NoHorizontalBanding
- {
- get;
- set;
- }
-
- public bool NoVerticalBanding
- {
- get;
- set;
- }
-
- #endregion
-
- #region Constructors
-
- public TableLook()
- {
- }
-
- public TableLook( bool firstRow, bool lastRow, bool firstColumn, bool lastColumn, bool noHorizontalBanding, bool noVerticalBanding )
- {
- this.FirstRow = firstRow;
- this.LastRow = lastRow;
- this.FirstColumn = firstColumn;
- this.LastColumn = lastColumn;
- this.NoHorizontalBanding = noHorizontalBanding;
- this.NoVerticalBanding = noVerticalBanding;
- }
-
- #endregion
- }
- }
|