Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DocX.cs 164KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446
  1. /*************************************************************************************
  2. DocX – DocX is the community edition of Xceed Words for .NET
  3. Copyright (C) 2009-2016 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features and fast professional support,
  7. pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
  8. ***********************************************************************************/
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Xml.Linq;
  14. using System.Xml;
  15. using System.IO;
  16. using System.Text.RegularExpressions;
  17. using System.IO.Packaging;
  18. using System.Security.Cryptography;
  19. using System.Drawing;
  20. using System.Collections.ObjectModel;
  21. namespace Xceed.Words.NET
  22. {
  23. /// <summary>
  24. /// Represents a document.
  25. /// </summary>
  26. public class DocX : Container, IDisposable
  27. {
  28. #region Namespaces
  29. static internal XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  30. static internal XNamespace rel = "http://schemas.openxmlformats.org/package/2006/relationships";
  31. static internal XNamespace r = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
  32. static internal XNamespace m = "http://schemas.openxmlformats.org/officeDocument/2006/math";
  33. static internal XNamespace customPropertiesSchema = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties";
  34. static internal XNamespace customVTypesSchema = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes";
  35. static internal XNamespace wp = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing";
  36. static internal XNamespace a = "http://schemas.openxmlformats.org/drawingml/2006/main";
  37. static internal XNamespace c = "http://schemas.openxmlformats.org/drawingml/2006/chart";
  38. internal static XNamespace n = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering";
  39. static internal XNamespace v = "urn:schemas-microsoft-com:vml";
  40. #endregion
  41. #region Private Members
  42. private Headers _headers;
  43. private Footers _footers;
  44. private float _pageSizeMultiplier = 20.0f;
  45. #endregion
  46. #region Internal Members
  47. // Get the word\settings.xml part
  48. internal PackagePart _settingsPart;
  49. internal PackagePart _endnotesPart;
  50. internal PackagePart _footnotesPart;
  51. internal PackagePart _stylesPart;
  52. internal PackagePart _stylesWithEffectsPart;
  53. internal PackagePart _numberingPart;
  54. internal PackagePart _fontTablePart;
  55. #region Internal variables defined foreach DocX object
  56. // Object representation of the .docx
  57. internal Package _package;
  58. // The mainDocument is loaded into a XDocument object for easy querying and editing
  59. internal XDocument _mainDoc;
  60. internal XDocument _settings;
  61. internal XDocument _endnotes;
  62. internal XDocument _footnotes;
  63. internal XDocument _styles;
  64. internal XDocument _stylesWithEffects;
  65. internal XDocument _numbering;
  66. internal XDocument _fontTable;
  67. // A lookup for the Paragraphs in this document.
  68. internal Dictionary<int, Paragraph> _paragraphLookup = new Dictionary<int, Paragraph>();
  69. // Every document is stored in a MemoryStream, all edits made to a document are done in memory.
  70. internal MemoryStream _memoryStream;
  71. // The filename that this document was loaded from
  72. internal string _filename;
  73. // The stream that this document was loaded from
  74. internal Stream _stream;
  75. #endregion
  76. #endregion
  77. #region Public Properties
  78. /// <summary>
  79. /// Top margin in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point.
  80. /// </summary>
  81. public float MarginTop
  82. {
  83. get
  84. {
  85. return getMarginAttribute( XName.Get( "top", w.NamespaceName ) );
  86. }
  87. set
  88. {
  89. setMarginAttribute( XName.Get( "top", w.NamespaceName ), value );
  90. }
  91. }
  92. /// <summary>
  93. /// Bottom margin in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point.
  94. /// </summary>
  95. public float MarginBottom
  96. {
  97. get
  98. {
  99. return getMarginAttribute( XName.Get( "bottom", w.NamespaceName ) );
  100. }
  101. set
  102. {
  103. setMarginAttribute( XName.Get( "bottom", w.NamespaceName ), value );
  104. }
  105. }
  106. /// <summary>
  107. /// Left margin in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point.
  108. /// </summary>
  109. public float MarginLeft
  110. {
  111. get
  112. {
  113. return getMarginAttribute( XName.Get( "left", w.NamespaceName ) );
  114. }
  115. set
  116. {
  117. setMarginAttribute( XName.Get( "left", w.NamespaceName ), value );
  118. }
  119. }
  120. /// <summary>
  121. /// Right margin in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point.
  122. /// </summary>
  123. public float MarginRight
  124. {
  125. get
  126. {
  127. return getMarginAttribute( XName.Get( "right", w.NamespaceName ) );
  128. }
  129. set
  130. {
  131. setMarginAttribute( XName.Get( "right", w.NamespaceName ), value );
  132. }
  133. }
  134. /// <summary>
  135. /// Header margin value in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point.
  136. /// </summary>
  137. public float MarginHeader
  138. {
  139. get
  140. {
  141. return getMarginAttribute(XName.Get("header", w.NamespaceName));
  142. }
  143. set
  144. {
  145. setMarginAttribute(XName.Get("header", w.NamespaceName), value);
  146. }
  147. }
  148. /// <summary>
  149. /// Footer margin value in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point.
  150. /// </summary>
  151. public float MarginFooter
  152. {
  153. get
  154. {
  155. return getMarginAttribute(XName.Get("footer", w.NamespaceName));
  156. }
  157. set
  158. {
  159. setMarginAttribute(XName.Get("footer", w.NamespaceName), value);
  160. }
  161. }
  162. public bool MirrorMargins
  163. {
  164. get
  165. {
  166. return getMirrorMargins(XName.Get("mirrorMargins", DocX.w.NamespaceName));
  167. }
  168. set
  169. {
  170. setMirrorMargins(XName.Get("mirrorMargins", DocX.w.NamespaceName), value);
  171. }
  172. }
  173. /// <summary>
  174. /// Page width in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point.
  175. /// </summary>
  176. public float PageWidth
  177. {
  178. get
  179. {
  180. var body = _mainDoc.Root.Element( XName.Get( "body", w.NamespaceName ) );
  181. var sectPr = body.Element( XName.Get( "sectPr", w.NamespaceName ) );
  182. var pgSz = sectPr?.Element( XName.Get( "pgSz", w.NamespaceName ) );
  183. if( pgSz != null )
  184. {
  185. var w = pgSz.Attribute( XName.Get( "w", DocX.w.NamespaceName ) );
  186. if( w != null )
  187. {
  188. float f;
  189. if( float.TryParse( w.Value, out f ) )
  190. return ( int )( f / _pageSizeMultiplier );
  191. }
  192. }
  193. return ( 12240.0f / _pageSizeMultiplier );
  194. }
  195. set
  196. {
  197. var body = _mainDoc.Root.Element( XName.Get( "body", w.NamespaceName ) );
  198. var sectPr = body.Element( XName.Get( "sectPr", w.NamespaceName ) );
  199. var pgSz = sectPr.Element( XName.Get( "pgSz", w.NamespaceName ) );
  200. pgSz?.SetAttributeValue( XName.Get( "w", w.NamespaceName ), value * Convert.ToInt32( _pageSizeMultiplier ) );
  201. }
  202. }
  203. /// <summary>
  204. /// Page height in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point.
  205. /// </summary>
  206. public float PageHeight
  207. {
  208. get
  209. {
  210. var body = _mainDoc.Root.Element( XName.Get( "body", w.NamespaceName ) );
  211. var sectPr = body.Element( XName.Get( "sectPr", w.NamespaceName ) );
  212. if( sectPr != null )
  213. {
  214. var pgSz = sectPr.Element( XName.Get( "pgSz", w.NamespaceName ) );
  215. if( pgSz != null )
  216. {
  217. var w = pgSz.Attribute( XName.Get( "h", DocX.w.NamespaceName ) );
  218. if( w != null )
  219. {
  220. float f;
  221. if( float.TryParse( w.Value, out f ) )
  222. return ( int )( f / _pageSizeMultiplier );
  223. }
  224. }
  225. }
  226. return ( 15840.0f / _pageSizeMultiplier );
  227. }
  228. set
  229. {
  230. var body = _mainDoc.Root.Element( XName.Get( "body", w.NamespaceName ) );
  231. if( body != null )
  232. {
  233. var sectPr = body.Element( XName.Get( "sectPr", w.NamespaceName ) );
  234. if( sectPr != null )
  235. {
  236. var pgSz = sectPr.Element( XName.Get( "pgSz", w.NamespaceName ) );
  237. if( pgSz != null )
  238. {
  239. pgSz.SetAttributeValue( XName.Get( "h", w.NamespaceName ), value * Convert.ToInt32( _pageSizeMultiplier ) );
  240. }
  241. }
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// Returns true if any editing restrictions are imposed on this document.
  247. /// </summary>
  248. /// <example>
  249. /// <code>
  250. /// // Create a new document.
  251. /// using (DocX document = DocX.Create(@"Test.docx"))
  252. /// {
  253. /// if(document.isProtected)
  254. /// Console.WriteLine("Protected");
  255. /// else
  256. /// Console.WriteLine("Not protected");
  257. ///
  258. /// // Save the document.
  259. /// document.Save();
  260. /// }
  261. /// </code>
  262. /// </example>
  263. /// <seealso cref="AddProtection(EditRestrictions)"/>
  264. /// <seealso cref="RemoveProtection"/>
  265. /// <seealso cref="GetProtectionType"/>
  266. public bool isProtected
  267. {
  268. get
  269. {
  270. return _settings.Descendants( XName.Get( "documentProtection", w.NamespaceName ) ).Count() > 0;
  271. }
  272. }
  273. public PageLayout PageLayout
  274. {
  275. get
  276. {
  277. XElement sectPr = Xml.Element( XName.Get( "sectPr", w.NamespaceName ) );
  278. if( sectPr == null )
  279. {
  280. Xml.SetElementValue( XName.Get( "sectPr", w.NamespaceName ), string.Empty );
  281. sectPr = Xml.Element( XName.Get( "sectPr", w.NamespaceName ) );
  282. }
  283. return new PageLayout( this, sectPr );
  284. }
  285. }
  286. /// <summary>
  287. /// Returns a collection of Headers in this Document.
  288. /// A document typically contains three Headers.
  289. /// A default one (odd), one for the first page and one for even pages.
  290. /// </summary>
  291. /// <example>
  292. /// <code>
  293. /// // Create a document.
  294. /// using (DocX document = DocX.Create(@"Test.docx"))
  295. /// {
  296. /// // Add header support to this document.
  297. /// document.AddHeaders();
  298. ///
  299. /// // Get a collection of all headers in this document.
  300. /// Headers headers = document.Headers;
  301. ///
  302. /// // The header used for the first page of this document.
  303. /// Header first = headers.first;
  304. ///
  305. /// // The header used for odd pages of this document.
  306. /// Header odd = headers.odd;
  307. ///
  308. /// // The header used for even pages of this document.
  309. /// Header even = headers.even;
  310. /// }
  311. /// </code>
  312. /// </example>
  313. public Headers Headers
  314. {
  315. get
  316. {
  317. return _headers;
  318. }
  319. }
  320. /// <summary>
  321. /// Returns a collection of Footers in this Document.
  322. /// A document typically contains three Footers.
  323. /// A default one (odd), one for the first page and one for even pages.
  324. /// </summary>
  325. /// <example>
  326. /// <code>
  327. /// // Create a document.
  328. /// using (DocX document = DocX.Create(@"Test.docx"))
  329. /// {
  330. /// // Add footer support to this document.
  331. /// document.AddFooters();
  332. ///
  333. /// // Get a collection of all footers in this document.
  334. /// Footers footers = document.Footers;
  335. ///
  336. /// // The footer used for the first page of this document.
  337. /// Footer first = footers.first;
  338. ///
  339. /// // The footer used for odd pages of this document.
  340. /// Footer odd = footers.odd;
  341. ///
  342. /// // The footer used for even pages of this document.
  343. /// Footer even = footers.even;
  344. /// }
  345. /// </code>
  346. /// </example>
  347. public Footers Footers
  348. {
  349. get
  350. {
  351. return _footers;
  352. }
  353. }
  354. /// <summary>
  355. /// Should the Document use different Headers and Footers for odd and even pages?
  356. /// </summary>
  357. /// <example>
  358. /// <code>
  359. /// // Create a document.
  360. /// using (DocX document = DocX.Create(@"Test.docx"))
  361. /// {
  362. /// // Add header support to this document.
  363. /// document.AddHeaders();
  364. ///
  365. /// // Get a collection of all headers in this document.
  366. /// Headers headers = document.Headers;
  367. ///
  368. /// // The header used for odd pages of this document.
  369. /// Header odd = headers.odd;
  370. ///
  371. /// // The header used for even pages of this document.
  372. /// Header even = headers.even;
  373. ///
  374. /// // Force the document to use a different header for odd and even pages.
  375. /// document.DifferentOddAndEvenPages = true;
  376. ///
  377. /// // Content can be added to the Headers in the same manor that it would be added to the main document.
  378. /// Paragraph p1 = odd.InsertParagraph();
  379. /// p1.Append("This is the odd pages header.");
  380. ///
  381. /// Paragraph p2 = even.InsertParagraph();
  382. /// p2.Append("This is the even pages header.");
  383. ///
  384. /// // Save all changes to this document.
  385. /// document.Save();
  386. /// }// Release this document from memory.
  387. /// </code>
  388. /// </example>
  389. public bool DifferentOddAndEvenPages
  390. {
  391. get
  392. {
  393. XDocument settings;
  394. using( TextReader tr = new StreamReader( _settingsPart.GetStream() ) )
  395. {
  396. settings = XDocument.Load( tr );
  397. }
  398. var evenAndOddHeaders = settings.Root.Element( w + "evenAndOddHeaders" );
  399. return ( evenAndOddHeaders != null );
  400. }
  401. set
  402. {
  403. XDocument settings;
  404. using( TextReader tr = new StreamReader( _settingsPart.GetStream() ) )
  405. {
  406. settings = XDocument.Load( tr );
  407. }
  408. var evenAndOddHeaders = settings.Root.Element( w + "evenAndOddHeaders" );
  409. if( evenAndOddHeaders == null )
  410. {
  411. if( value )
  412. {
  413. settings.Root.AddFirst( new XElement( w + "evenAndOddHeaders" ) );
  414. }
  415. }
  416. else
  417. {
  418. if( !value )
  419. {
  420. evenAndOddHeaders.Remove();
  421. }
  422. }
  423. using( TextWriter tw = new StreamWriter( new PackagePartStream( _settingsPart.GetStream() ) ) )
  424. {
  425. settings.Save( tw );
  426. }
  427. }
  428. }
  429. /// <summary>
  430. /// Should the Document use an independent Header and Footer for the first page?
  431. /// </summary>
  432. /// <example>
  433. /// // Create a document.
  434. /// using (DocX document = DocX.Create(@"Test.docx"))
  435. /// {
  436. /// // Add header support to this document.
  437. /// document.AddHeaders();
  438. ///
  439. /// // The header used for the first page of this document.
  440. /// Header first = document.Headers.first;
  441. ///
  442. /// // Force the document to use a different header for first page.
  443. /// document.DifferentFirstPage = true;
  444. ///
  445. /// // Content can be added to the Headers in the same manor that it would be added to the main document.
  446. /// Paragraph p = first.InsertParagraph();
  447. /// p.Append("This is the first pages header.");
  448. ///
  449. /// // Save all changes to this document.
  450. /// document.Save();
  451. /// }// Release this document from memory.
  452. /// </example>
  453. public bool DifferentFirstPage
  454. {
  455. get
  456. {
  457. var body = _mainDoc.Root.Element( w + "body" );
  458. var sectPr = body.Element( w + "sectPr" );
  459. var titlePg = sectPr?.Element( w + "titlePg" );
  460. return titlePg != null;
  461. }
  462. set
  463. {
  464. var body = _mainDoc.Root.Element( w + "body" );
  465. body.Add( new XElement( w + "sectPr", string.Empty ) );
  466. var sectPr = body.Element( w + "sectPr" );
  467. var titlePg = sectPr.Element( w + "titlePg" );
  468. if( titlePg == null )
  469. {
  470. if( value )
  471. {
  472. sectPr.Add( new XElement( w + "titlePg", string.Empty ) );
  473. }
  474. }
  475. else
  476. {
  477. if( !value )
  478. {
  479. titlePg.Remove();
  480. }
  481. }
  482. }
  483. }
  484. /// <summary>
  485. /// Returns a list of Images in this document.
  486. /// </summary>
  487. /// <example>
  488. /// Get the unique Id of every Image in this document.
  489. /// <code>
  490. /// // Load a document.
  491. /// DocX document = DocX.Load(@"C:\Example\Test.docx");
  492. ///
  493. /// // Loop through each Image in this document.
  494. /// foreach (Xceed.Words.NET.Image i in document.Images)
  495. /// {
  496. /// // Get the unique Id which identifies this Image.
  497. /// string uniqueId = i.Id;
  498. /// }
  499. ///
  500. /// </code>
  501. /// </example>
  502. /// <seealso cref="AddImage(string)"/>
  503. /// <seealso cref="AddImage(Stream)"/>
  504. /// <seealso cref="Paragraph.Pictures"/>
  505. /// <seealso cref="Paragraph.InsertPicture"/>
  506. public List<Image> Images
  507. {
  508. get
  509. {
  510. var imageRelationships = this.PackagePart.GetRelationshipsByType( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" );
  511. if( imageRelationships.Any() )
  512. {
  513. return
  514. (
  515. from i in imageRelationships
  516. select new Image( this, i )
  517. ).ToList();
  518. }
  519. return new List<Image>();
  520. }
  521. }
  522. /// <summary>
  523. /// Returns a list of custom properties in this document.
  524. /// </summary>
  525. /// <example>
  526. /// Method 1: Get the name, type and value of each CustomProperty in this document.
  527. /// <code>
  528. /// // Load Example.docx
  529. /// DocX document = DocX.Load(@"C:\Example\Test.docx");
  530. ///
  531. /// /*
  532. /// * No two custom properties can have the same name,
  533. /// * so a Dictionary is the perfect data structure to store them in.
  534. /// * Each custom property can be accessed using its name.
  535. /// */
  536. /// foreach (string name in document.CustomProperties.Keys)
  537. /// {
  538. /// // Grab a custom property using its name.
  539. /// CustomProperty cp = document.CustomProperties[name];
  540. ///
  541. /// // Write this custom properties details to Console.
  542. /// Console.WriteLine(string.Format("Name: '{0}', Value: {1}", cp.Name, cp.Value));
  543. /// }
  544. ///
  545. /// Console.WriteLine("Press any key...");
  546. ///
  547. /// // Wait for the user to press a key before closing the Console.
  548. /// Console.ReadKey();
  549. /// </code>
  550. /// </example>
  551. /// <example>
  552. /// Method 2: Get the name, type and value of each CustomProperty in this document.
  553. /// <code>
  554. /// // Load Example.docx
  555. /// DocX document = DocX.Load(@"C:\Example\Test.docx");
  556. ///
  557. /// /*
  558. /// * No two custom properties can have the same name,
  559. /// * so a Dictionary is the perfect data structure to store them in.
  560. /// * The values of this Dictionary are CustomProperties.
  561. /// */
  562. /// foreach (CustomProperty cp in document.CustomProperties.Values)
  563. /// {
  564. /// // Write this custom properties details to Console.
  565. /// Console.WriteLine(string.Format("Name: '{0}', Value: {1}", cp.Name, cp.Value));
  566. /// }
  567. ///
  568. /// Console.WriteLine("Press any key...");
  569. ///
  570. /// // Wait for the user to press a key before closing the Console.
  571. /// Console.ReadKey();
  572. /// </code>
  573. /// </example>
  574. /// <seealso cref="AddCustomProperty"/>
  575. public Dictionary<string, CustomProperty> CustomProperties
  576. {
  577. get
  578. {
  579. if( _package.PartExists( new Uri( "/docProps/custom.xml", UriKind.Relative ) ) )
  580. {
  581. PackagePart docProps_custom = _package.GetPart( new Uri( "/docProps/custom.xml", UriKind.Relative ) );
  582. XDocument customPropDoc;
  583. using( TextReader tr = new StreamReader( docProps_custom.GetStream( FileMode.Open, FileAccess.Read ) ) )
  584. customPropDoc = XDocument.Load( tr, LoadOptions.PreserveWhitespace );
  585. // Get all of the custom properties in this document
  586. return
  587. (
  588. from p in customPropDoc.Descendants( XName.Get( "property", customPropertiesSchema.NamespaceName ) )
  589. let Name = p.Attribute( XName.Get( "name" ) ).Value
  590. let Type = p.Descendants().Single().Name.LocalName
  591. let Value = p.Descendants().Single().Value
  592. select new CustomProperty( Name, Type, Value )
  593. ).ToDictionary( p => p.Name, StringComparer.CurrentCultureIgnoreCase );
  594. }
  595. return new Dictionary<string, CustomProperty>();
  596. }
  597. }
  598. ///<summary>
  599. /// Returns the list of document core properties with corresponding values.
  600. ///</summary>
  601. public Dictionary<string, string> CoreProperties
  602. {
  603. get
  604. {
  605. if( _package.PartExists( new Uri( "/docProps/core.xml", UriKind.Relative ) ) )
  606. {
  607. PackagePart docProps_Core = _package.GetPart( new Uri( "/docProps/core.xml", UriKind.Relative ) );
  608. XDocument corePropDoc;
  609. using( TextReader tr = new StreamReader( docProps_Core.GetStream( FileMode.Open, FileAccess.Read ) ) )
  610. corePropDoc = XDocument.Load( tr, LoadOptions.PreserveWhitespace );
  611. // Get all of the core properties in this document
  612. return ( from docProperty in corePropDoc.Root.Elements()
  613. select
  614. new KeyValuePair<string, string>(
  615. string.Format(
  616. "{0}:{1}",
  617. corePropDoc.Root.GetPrefixOfNamespace( docProperty.Name.Namespace ),
  618. docProperty.Name.LocalName ),
  619. docProperty.Value ) ).ToDictionary( p => p.Key, v => v.Value );
  620. }
  621. return new Dictionary<string, string>();
  622. }
  623. }
  624. /// <summary>
  625. /// Get the Text of this document.
  626. /// </summary>
  627. /// <example>
  628. /// Write to Console the Text from this document.
  629. /// <code>
  630. /// // Load a document
  631. /// DocX document = DocX.Load(@"C:\Example\Test.docx");
  632. ///
  633. /// // Get the text of this document.
  634. /// string text = document.Text;
  635. ///
  636. /// // Write the text of this document to Console.
  637. /// Console.Write(text);
  638. ///
  639. /// // Wait for the user to press a key before closing the console window.
  640. /// Console.ReadKey();
  641. /// </code>
  642. /// </example>
  643. public string Text
  644. {
  645. get
  646. {
  647. return HelperFunctions.GetText( Xml );
  648. }
  649. }
  650. public override ReadOnlyCollection<Paragraph> Paragraphs
  651. {
  652. get
  653. {
  654. var paragraphs = base.Paragraphs;
  655. foreach( var paragraph in paragraphs )
  656. {
  657. paragraph.PackagePart = this.PackagePart;
  658. }
  659. return paragraphs;
  660. }
  661. }
  662. public override List<List> Lists
  663. {
  664. get
  665. {
  666. var l = base.Lists;
  667. l.ForEach( x => x.Items.ForEach( i => i.PackagePart = this.PackagePart ) );
  668. return l;
  669. }
  670. }
  671. public override List<Table> Tables
  672. {
  673. get
  674. {
  675. var l = base.Tables;
  676. l.ForEach( x => x.PackagePart = this.PackagePart );
  677. return l;
  678. }
  679. }
  680. /// <summary>
  681. /// Get the Footnotes of this document
  682. /// </summary>
  683. public IEnumerable<string> FootnotesText
  684. {
  685. get
  686. {
  687. foreach( var note in _footnotes.Root.Elements( w + "footnote" ) )
  688. yield return HelperFunctions.GetText( note );
  689. }
  690. }
  691. /// <summary>
  692. /// Get the Endnotes of this document
  693. /// </summary>
  694. public IEnumerable<string> EndnotesText
  695. {
  696. get
  697. {
  698. foreach( var note in _endnotes.Root.Elements( w + "endnote" ) )
  699. yield return HelperFunctions.GetText( note );
  700. }
  701. }
  702. public BookmarkCollection Bookmarks
  703. {
  704. get
  705. {
  706. var bookmarks = new BookmarkCollection();
  707. for( int i = 0; i < this.Paragraphs.Count; ++i )
  708. {
  709. bookmarks.AddRange( this.Paragraphs[ i ].GetBookmarks() );
  710. }
  711. return bookmarks;
  712. }
  713. }
  714. #endregion
  715. #region Public Methods
  716. /// <summary>
  717. /// Returns the type of editing protection imposed on this document.
  718. /// </summary>
  719. /// <returns>The type of editing protection imposed on this document.</returns>
  720. /// <example>
  721. /// <code>
  722. /// Create a new document.
  723. /// using (DocX document = DocX.Create(@"Test.docx"))
  724. /// {
  725. /// // Make sure the document is protected before checking the protection type.
  726. /// if (document.isProtected)
  727. /// {
  728. /// EditRestrictions protection = document.GetProtectionType();
  729. /// Console.WriteLine("Document is protected using " + protection.ToString());
  730. /// }
  731. ///
  732. /// else
  733. /// Console.WriteLine("Document is not protected.");
  734. ///
  735. /// // Save the document.
  736. /// document.Save();
  737. /// }
  738. /// </code>
  739. /// </example>
  740. /// <seealso cref="AddProtection"/>
  741. /// <seealso cref="RemoveProtection"/>
  742. /// <seealso cref="isProtected"/>
  743. public EditRestrictions GetProtectionType()
  744. {
  745. if( isProtected )
  746. {
  747. XElement documentProtection = _settings.Descendants( XName.Get( "documentProtection", w.NamespaceName ) ).FirstOrDefault();
  748. string edit_type = documentProtection.Attribute( XName.Get( "edit", w.NamespaceName ) ).Value;
  749. return ( EditRestrictions )Enum.Parse( typeof( EditRestrictions ), edit_type );
  750. }
  751. return EditRestrictions.none;
  752. }
  753. /// <summary>
  754. /// Add editing protection to this document.
  755. /// </summary>
  756. /// <param name="er">The type of protection to add to this document.</param>
  757. /// <example>
  758. /// <code>
  759. /// // Create a new document.
  760. /// using (DocX document = DocX.Create(@"Test.docx"))
  761. /// {
  762. /// // Allow no editing, only the adding of comment.
  763. /// document.AddProtection(EditRestrictions.comments);
  764. ///
  765. /// // Save the document.
  766. /// document.Save();
  767. /// }
  768. /// </code>
  769. /// </example>
  770. /// <seealso cref="RemoveProtection"/>
  771. /// <seealso cref="GetProtectionType"/>
  772. /// <seealso cref="isProtected"/>
  773. public void AddProtection( EditRestrictions er )
  774. {
  775. // Call remove protection before adding a new protection element.
  776. RemoveProtection();
  777. if( er == EditRestrictions.none )
  778. return;
  779. var documentProtection = new XElement( XName.Get( "documentProtection", w.NamespaceName ) );
  780. documentProtection.Add( new XAttribute( XName.Get( "edit", w.NamespaceName ), er.ToString() ) );
  781. documentProtection.Add( new XAttribute( XName.Get( "enforcement", w.NamespaceName ), "1" ) );
  782. _settings.Root.AddFirst( documentProtection );
  783. }
  784. /// <summary>
  785. /// Remove editing protection from this document.
  786. /// </summary>
  787. /// <example>
  788. /// <code>
  789. /// // Create a new document.
  790. /// using (DocX document = DocX.Create(@"Test.docx"))
  791. /// {
  792. /// // Remove any editing restrictions that are imposed on this document.
  793. /// document.RemoveProtection();
  794. ///
  795. /// // Save the document.
  796. /// document.Save();
  797. /// }
  798. /// </code>
  799. /// </example>
  800. /// <seealso cref="AddProtection(EditRestrictions)"/>
  801. /// <seealso cref="GetProtectionType"/>
  802. /// <seealso cref="isProtected"/>
  803. public void RemoveProtection()
  804. {
  805. // Remove every node of type documentProtection.
  806. _settings.Descendants( XName.Get( "documentProtection", w.NamespaceName ) ).Remove();
  807. }
  808. /// <summary>
  809. /// Insert the contents of another document at the end of this document.
  810. /// </summary>
  811. /// <param name="remote_document">The document to insert at the end of this document.</param>
  812. /// <param name="append">When true, document is added at the end. If False, document is added at the beginning.</param>
  813. /// <example>
  814. /// Create a new document and insert an old document into it.
  815. /// <code>
  816. /// // Create a new document.
  817. /// using (DocX newDocument = DocX.Create(@"NewDocument.docx"))
  818. /// {
  819. /// // Load an old document.
  820. /// using (DocX oldDocument = DocX.Load(@"OldDocument.docx"))
  821. /// {
  822. /// // Insert the old document into the new document.
  823. /// newDocument.InsertDocument(oldDocument);
  824. ///
  825. /// // Save the new document.
  826. /// newDocument.Save();
  827. /// }// Release the old document from memory.
  828. /// }// Release the new document from memory.
  829. /// </code>
  830. /// <remarks>
  831. /// If the document being inserted contains Images, CustomProperties and or custom styles, these will be correctly inserted into the new document. In the case of Images, new ID's are generated for the Images being inserted to avoid ID conflicts. CustomProperties with the same name will be ignored not replaced.
  832. /// </remarks>
  833. /// </example>
  834. public void InsertDocument( DocX remote_document, bool append = true )
  835. {
  836. // We don't want to effect the origional XDocument, so create a new one from the old one.
  837. var remote_mainDoc = new XDocument( remote_document._mainDoc );
  838. XDocument remote_footnotes = null;
  839. if( remote_document._footnotes != null )
  840. {
  841. remote_footnotes = new XDocument( remote_document._footnotes );
  842. }
  843. XDocument remote_endnotes = null;
  844. if( remote_document._endnotes != null )
  845. {
  846. remote_endnotes = new XDocument( remote_document._endnotes );
  847. }
  848. // Remove all header and footer references.
  849. remote_mainDoc.Descendants( XName.Get( "headerReference", w.NamespaceName ) ).Remove();
  850. remote_mainDoc.Descendants( XName.Get( "footerReference", w.NamespaceName ) ).Remove();
  851. // Get the body of the remote document.
  852. var remote_body = remote_mainDoc.Root.Element( XName.Get( "body", w.NamespaceName ) );
  853. // Every file that is missing from the local document will have to be copied, every file that already exists will have to be merged.
  854. var ppc = remote_document._package.GetParts();
  855. var ignoreContentTypes = new List<string>
  856. {
  857. "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
  858. "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml",
  859. "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml",
  860. "application/vnd.openxmlformats-package.core-properties+xml",
  861. "application/vnd.openxmlformats-officedocument.extended-properties+xml",
  862. "application/vnd.openxmlformats-package.relationships+xml"
  863. };
  864. var imageContentTypes = new List<string>
  865. {
  866. "image/jpeg",
  867. "image/jpg",
  868. "image/png",
  869. "image/bmp",
  870. "image/gif",
  871. "image/tiff",
  872. "image/icon",
  873. "image/pcx",
  874. "image/emf",
  875. "image/wmf"
  876. };
  877. // Check if each PackagePart pp exists in this document.
  878. foreach( PackagePart remote_pp in ppc )
  879. {
  880. if( ignoreContentTypes.Contains( remote_pp.ContentType ) || imageContentTypes.Contains( remote_pp.ContentType ) )
  881. continue;
  882. // If this external PackagePart already exits then we must merge them.
  883. if( _package.PartExists( remote_pp.Uri ) )
  884. {
  885. var local_pp = _package.GetPart( remote_pp.Uri );
  886. switch( remote_pp.ContentType )
  887. {
  888. case "application/vnd.openxmlformats-officedocument.custom-properties+xml":
  889. merge_customs( remote_pp, local_pp, remote_mainDoc );
  890. break;
  891. // Merge footnotes/endnotes before merging styles, then set the remote_footnotes to the just updated footnotes
  892. case "application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml":
  893. remote_footnotes = _footnotes;
  894. break;
  895. case "application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml":
  896. remote_endnotes = _endnotes;
  897. break;
  898. case "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml":
  899. merge_styles( remote_pp, local_pp, remote_mainDoc, remote_document, remote_footnotes, remote_endnotes );
  900. break;
  901. // Merges Styles after merging the footnotes, so the changes will be applied to the correct document/footnotes.
  902. case "application/vnd.ms-word.stylesWithEffects+xml":
  903. merge_styles( remote_pp, local_pp, remote_mainDoc, remote_document, remote_footnotes, remote_endnotes );
  904. break;
  905. case "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml":
  906. merge_fonts( remote_pp, local_pp, remote_mainDoc, remote_document );
  907. break;
  908. case "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml":
  909. merge_numbering( remote_pp, local_pp, remote_mainDoc, remote_document );
  910. break;
  911. }
  912. }
  913. // If this external PackagePart does not exits in the internal document then we can simply copy it.
  914. else
  915. {
  916. var packagePart = clonePackagePart( remote_pp );
  917. switch( remote_pp.ContentType )
  918. {
  919. case "application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml":
  920. _endnotesPart = packagePart;
  921. _endnotes = remote_endnotes;
  922. break;
  923. case "application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml":
  924. _footnotesPart = packagePart;
  925. _footnotes = remote_footnotes;
  926. break;
  927. case "application/vnd.openxmlformats-officedocument.custom-properties+xml":
  928. break;
  929. case "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml":
  930. _stylesPart = packagePart;
  931. using( TextReader tr = new StreamReader( _stylesPart.GetStream() ) )
  932. _styles = XDocument.Load( tr );
  933. break;
  934. case "application/vnd.ms-word.stylesWithEffects+xml":
  935. _stylesWithEffectsPart = packagePart;
  936. using( TextReader tr = new StreamReader( _stylesWithEffectsPart.GetStream() ) )
  937. _stylesWithEffects = XDocument.Load( tr );
  938. break;
  939. case "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml":
  940. _fontTablePart = packagePart;
  941. using( TextReader tr = new StreamReader( _fontTablePart.GetStream() ) )
  942. _fontTable = XDocument.Load( tr );
  943. break;
  944. case "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml":
  945. _numberingPart = packagePart;
  946. using( TextReader tr = new StreamReader( _numberingPart.GetStream() ) )
  947. _numbering = XDocument.Load( tr );
  948. break;
  949. }
  950. clonePackageRelationship( remote_document, remote_pp, remote_mainDoc );
  951. }
  952. }
  953. foreach( var hyperlink_rel in remote_document.PackagePart.GetRelationshipsByType( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" ) )
  954. {
  955. var old_rel_Id = hyperlink_rel.Id;
  956. var new_rel_Id = this.PackagePart.CreateRelationship( hyperlink_rel.TargetUri, hyperlink_rel.TargetMode, hyperlink_rel.RelationshipType ).Id;
  957. var hyperlink_refs = remote_mainDoc.Descendants( XName.Get( "hyperlink", w.NamespaceName ) );
  958. foreach( var hyperlink_ref in hyperlink_refs )
  959. {
  960. var a0 = hyperlink_ref.Attribute( XName.Get( "id", r.NamespaceName ) );
  961. if( a0 != null && a0.Value == old_rel_Id )
  962. {
  963. a0.SetValue( new_rel_Id );
  964. }
  965. }
  966. }
  967. foreach( var oleObject_rel in remote_document.PackagePart.GetRelationshipsByType( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject" ) )
  968. {
  969. var oldRelationshipID = oleObject_rel.Id;
  970. var newRelationshipID = this.PackagePart.CreateRelationship( oleObject_rel.TargetUri, oleObject_rel.TargetMode, oleObject_rel.RelationshipType ).Id;
  971. var references = remote_mainDoc.Descendants( XName.Get( "OLEObject", "urn:schemas-microsoft-com:office:office" ) );
  972. foreach( var reference in references )
  973. {
  974. var attribute = reference.Attribute( XName.Get( "id", r.NamespaceName ) );
  975. if( attribute != null && attribute.Value == oldRelationshipID )
  976. attribute.SetValue( newRelationshipID );
  977. }
  978. }
  979. foreach( PackagePart remote_pp in ppc )
  980. {
  981. if( imageContentTypes.Contains( remote_pp.ContentType ) )
  982. {
  983. merge_images( remote_pp, remote_document, remote_mainDoc, remote_pp.ContentType );
  984. }
  985. }
  986. int id = 0;
  987. var local_docPrs = _mainDoc.Root.Descendants( XName.Get( "docPr", wp.NamespaceName ) );
  988. foreach( var local_docPr in local_docPrs )
  989. {
  990. var a_id = local_docPr.Attribute( XName.Get( "id" ) );
  991. int a_id_value;
  992. if( a_id != null && int.TryParse( a_id.Value, out a_id_value ) )
  993. {
  994. if( a_id_value > id )
  995. {
  996. id = a_id_value;
  997. }
  998. }
  999. }
  1000. id++;
  1001. // docPr must be sequential
  1002. var docPrs = remote_body.Descendants( XName.Get( "docPr", wp.NamespaceName ) );
  1003. foreach( var docPr in docPrs )
  1004. {
  1005. docPr.SetAttributeValue( XName.Get( "id" ), id );
  1006. id++;
  1007. }
  1008. // Add the remote documents contents to this document.
  1009. var local_body = _mainDoc.Root.Element( XName.Get( "body", w.NamespaceName ) );
  1010. if( append )
  1011. {
  1012. local_body.Add( remote_body.Elements() );
  1013. }
  1014. else
  1015. {
  1016. local_body.AddFirst( remote_body.Elements() );
  1017. }
  1018. // Copy any missing root attributes to the local document.
  1019. foreach( XAttribute a in remote_mainDoc.Root.Attributes() )
  1020. {
  1021. if( _mainDoc.Root.Attribute( a.Name ) == null )
  1022. {
  1023. _mainDoc.Root.SetAttributeValue( a.Name, a.Value );
  1024. }
  1025. }
  1026. }
  1027. /// <summary>
  1028. /// Insert a new Table at the end of this document.
  1029. /// </summary>
  1030. /// <param name="columnCount">The number of columns to create.</param>
  1031. /// <param name="rowCount">The number of rows to create.</param>
  1032. /// <returns>A new Table.</returns>
  1033. /// <example>
  1034. /// Insert a new Table with 2 columns and 3 rows, at the end of a document.
  1035. /// <code>
  1036. /// // Create a document.
  1037. /// using (DocX document = DocX.Create(@"C:\Example\Test.docx"))
  1038. /// {
  1039. /// // Create a new Table with 2 columns and 3 rows.
  1040. /// Table newTable = document.InsertTable(2, 3);
  1041. ///
  1042. /// // Set the design of this Table.
  1043. /// newTable.Design = TableDesign.LightShadingAccent2;
  1044. ///
  1045. /// // Set the column names.
  1046. /// newTable.Rows[0].Cells[0].Paragraph.InsertText("Ice Cream", false);
  1047. /// newTable.Rows[0].Cells[1].Paragraph.InsertText("Price", false);
  1048. ///
  1049. /// // Fill row 1
  1050. /// newTable.Rows[1].Cells[0].Paragraph.InsertText("Chocolate", false);
  1051. /// newTable.Rows[1].Cells[1].Paragraph.InsertText("€3:50", false);
  1052. ///
  1053. /// // Fill row 2
  1054. /// newTable.Rows[2].Cells[0].Paragraph.InsertText("Vanilla", false);
  1055. /// newTable.Rows[2].Cells[1].Paragraph.InsertText("€3:00", false);
  1056. ///
  1057. /// // Save all changes made to document b.
  1058. /// document.Save();
  1059. /// }// Release this document from memory.
  1060. /// </code>
  1061. /// </example>
  1062. public new Table InsertTable( int rowCount, int columnCount )
  1063. {
  1064. if( rowCount < 1 || columnCount < 1 )
  1065. throw new ArgumentOutOfRangeException( "Row and Column count must be greater than zero." );
  1066. var t = base.InsertTable( rowCount, columnCount );
  1067. t.PackagePart = this.PackagePart;
  1068. return t;
  1069. }
  1070. public Table AddTable( int rowCount, int columnCount )
  1071. {
  1072. if( rowCount < 1 || columnCount < 1 )
  1073. throw new ArgumentOutOfRangeException( "Row and Column count must be greater than zero." );
  1074. var t = new Table( this, HelperFunctions.CreateTable( rowCount, columnCount ) );
  1075. t.PackagePart = this.PackagePart;
  1076. return t;
  1077. }
  1078. /// <summary>
  1079. /// Insert a Table into this document. The Table's source can be a completely different document.
  1080. /// </summary>
  1081. /// <param name="t">The Table to insert.</param>
  1082. /// <param name="index">The index to insert this Table at.</param>
  1083. /// <returns>The Table now associated with this document.</returns>
  1084. /// <example>
  1085. /// Extract a Table from document a and insert it into document b, at index 10.
  1086. /// <code>
  1087. /// // Place holder for a Table.
  1088. /// Table t;
  1089. ///
  1090. /// // Load document a.
  1091. /// using (DocX documentA = DocX.Load(@"C:\Example\a.docx"))
  1092. /// {
  1093. /// // Get the first Table from this document.
  1094. /// t = documentA.Tables[0];
  1095. /// }
  1096. ///
  1097. /// // Load document b.
  1098. /// using (DocX documentB = DocX.Load(@"C:\Example\b.docx"))
  1099. /// {
  1100. /// /*
  1101. /// * Insert the Table that was extracted from document a, into document b.
  1102. /// * This creates a new Table that is now associated with document b.
  1103. /// */
  1104. /// Table newTable = documentB.InsertTable(10, t);
  1105. ///
  1106. /// // Save all changes made to document b.
  1107. /// documentB.Save();
  1108. /// }// Release this document from memory.
  1109. /// </code>
  1110. /// </example>
  1111. public new Table InsertTable( int index, Table t )
  1112. {
  1113. var t2 = base.InsertTable( index, t );
  1114. t2.PackagePart = this.PackagePart;
  1115. return t2;
  1116. }
  1117. /// <summary>
  1118. /// Insert a Table into this document. The Table's source can be a completely different document.
  1119. /// </summary>
  1120. /// <param name="t">The Table to insert.</param>
  1121. /// <returns>The Table now associated with this document.</returns>
  1122. /// <example>
  1123. /// Extract a Table from document a and insert it at the end of document b.
  1124. /// <code>
  1125. /// // Place holder for a Table.
  1126. /// Table t;
  1127. ///
  1128. /// // Load document a.
  1129. /// using (DocX documentA = DocX.Load(@"C:\Example\a.docx"))
  1130. /// {
  1131. /// // Get the first Table from this document.
  1132. /// t = documentA.Tables[0];
  1133. /// }
  1134. ///
  1135. /// // Load document b.
  1136. /// using (DocX documentB = DocX.Load(@"C:\Example\b.docx"))
  1137. /// {
  1138. /// /*
  1139. /// * Insert the Table that was extracted from document a, into document b.
  1140. /// * This creates a new Table that is now associated with document b.
  1141. /// */
  1142. /// Table newTable = documentB.InsertTable(t);
  1143. ///
  1144. /// // Save all changes made to document b.
  1145. /// documentB.Save();
  1146. /// }// Release this document from memory.
  1147. /// </code>
  1148. /// </example>
  1149. public new Table InsertTable( Table t )
  1150. {
  1151. t = base.InsertTable( t );
  1152. t.PackagePart = this.PackagePart;
  1153. return t;
  1154. }
  1155. /// <summary>
  1156. /// Insert a new Table at the end of this document.
  1157. /// </summary>
  1158. /// <param name="columnCount">The number of columns to create.</param>
  1159. /// <param name="rowCount">The number of rows to create.</param>
  1160. /// <param name="index">The index to insert this Table at.</param>
  1161. /// <returns>A new Table.</returns>
  1162. /// <example>
  1163. /// Insert a new Table with 2 columns and 3 rows, at index 37 in this document.
  1164. /// <code>
  1165. /// // Create a document.
  1166. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1167. /// {
  1168. /// // Create a new Table with 3 rows and 2 columns. Insert this Table at index 37.
  1169. /// Table newTable = document.InsertTable(37, 3, 2);
  1170. ///
  1171. /// // Set the design of this Table.
  1172. /// newTable.Design = TableDesign.LightShadingAccent3;
  1173. ///
  1174. /// // Set the column names.
  1175. /// newTable.Rows[0].Cells[0].Paragraph.InsertText("Ice Cream", false);
  1176. /// newTable.Rows[0].Cells[1].Paragraph.InsertText("Price", false);
  1177. ///
  1178. /// // Fill row 1
  1179. /// newTable.Rows[1].Cells[0].Paragraph.InsertText("Chocolate", false);
  1180. /// newTable.Rows[1].Cells[1].Paragraph.InsertText("€3:50", false);
  1181. ///
  1182. /// // Fill row 2
  1183. /// newTable.Rows[2].Cells[0].Paragraph.InsertText("Vanilla", false);
  1184. /// newTable.Rows[2].Cells[1].Paragraph.InsertText("€3:00", false);
  1185. ///
  1186. /// // Save all changes made to document b.
  1187. /// document.Save();
  1188. /// }// Release this document from memory.
  1189. /// </code>
  1190. /// </example>
  1191. public new Table InsertTable( int index, int rowCount, int columnCount )
  1192. {
  1193. if( rowCount < 1 || columnCount < 1 )
  1194. throw new ArgumentOutOfRangeException( "Row and Column count must be greater than zero." );
  1195. var t = base.InsertTable( index, rowCount, columnCount );
  1196. t.PackagePart = this.PackagePart;
  1197. return t;
  1198. }
  1199. /// <summary>
  1200. /// Creates a document using a Stream.
  1201. /// </summary>
  1202. /// <param name="stream">The Stream to create the document from.</param>
  1203. /// <param name="documentType"></param>
  1204. /// <returns>Returns a DocX object which represents the document.</returns>
  1205. /// <example>
  1206. /// Creating a document from a FileStream.
  1207. /// <code>
  1208. /// // Use a FileStream fs to create a new document.
  1209. /// using(FileStream fs = new FileStream(@"C:\Example\Test.docx", FileMode.Create))
  1210. /// {
  1211. /// // Load the document using fs
  1212. /// using (DocX document = DocX.Create(fs))
  1213. /// {
  1214. /// // Do something with the document here.
  1215. ///
  1216. /// // Save all changes made to this document.
  1217. /// document.Save();
  1218. /// }// Release this document from memory.
  1219. /// }
  1220. /// </code>
  1221. /// </example>
  1222. /// <example>
  1223. /// Creating a document in a SharePoint site.
  1224. /// <code>
  1225. /// using(SPSite mySite = new SPSite("http://server/sites/site"))
  1226. /// {
  1227. /// // Open a connection to the SharePoint site
  1228. /// using(SPWeb myWeb = mySite.OpenWeb())
  1229. /// {
  1230. /// // Create a MemoryStream ms.
  1231. /// using (MemoryStream ms = new MemoryStream())
  1232. /// {
  1233. /// // Create a document using ms.
  1234. /// using (DocX document = DocX.Create(ms))
  1235. /// {
  1236. /// // Do something with the document here.
  1237. ///
  1238. /// // Save all changes made to this document.
  1239. /// document.Save();
  1240. /// }// Release this document from memory
  1241. ///
  1242. /// // Add the document to the SharePoint site
  1243. /// web.Files.Add("filename", ms.ToArray(), true);
  1244. /// }
  1245. /// }
  1246. /// }
  1247. /// </code>
  1248. /// </example>
  1249. /// <seealso cref="DocX.Load(System.IO.Stream)"/>
  1250. /// <seealso cref="DocX.Load(string)"/>
  1251. /// <seealso cref="DocX.Save()"/>
  1252. public static DocX Create( Stream stream, DocumentTypes documentType = DocumentTypes.Document )
  1253. {
  1254. var document = CreateDocument( documentType );
  1255. document._stream = stream;
  1256. return document;
  1257. }
  1258. /// <summary>
  1259. /// Creates a document using a fully qualified or relative filename.
  1260. /// </summary>
  1261. /// <param name="filename">The fully qualified or relative filename.</param>
  1262. /// <param name="documentType"></param>
  1263. /// <returns>Returns a DocX object which represents the document.</returns>
  1264. /// <example>
  1265. /// <code>
  1266. /// // Create a document using a relative filename.
  1267. /// using (DocX document = DocX.Create(@"..\Test.docx"))
  1268. /// {
  1269. /// // Do something with the document here.
  1270. ///
  1271. /// // Save all changes made to this document.
  1272. /// document.Save();
  1273. /// }// Release this document from memory
  1274. /// </code>
  1275. /// <code>
  1276. /// // Create a document using a relative filename.
  1277. /// using (DocX document = DocX.Create(@"..\Test.docx"))
  1278. /// {
  1279. /// // Do something with the document here.
  1280. ///
  1281. /// // Save all changes made to this document.
  1282. /// document.Save();
  1283. /// }// Release this document from memory
  1284. /// </code>
  1285. /// <seealso cref="DocX.Load(System.IO.Stream)"/>
  1286. /// <seealso cref="DocX.Load(string)"/>
  1287. /// <seealso cref="DocX.Save()"/>
  1288. /// </example>
  1289. public static DocX Create( string filename, DocumentTypes documentType = DocumentTypes.Document )
  1290. {
  1291. var document = CreateDocument( documentType );
  1292. document._filename = filename;
  1293. return document;
  1294. }
  1295. /// <summary>
  1296. /// Loads a document into a DocX object using a Stream.
  1297. /// </summary>
  1298. /// <param name="stream">The Stream to load the document from.</param>
  1299. /// <returns>
  1300. /// Returns a DocX object which represents the document.
  1301. /// </returns>
  1302. /// <example>
  1303. /// Loading a document from a FileStream.
  1304. /// <code>
  1305. /// // Open a FileStream fs to a document.
  1306. /// using (FileStream fs = new FileStream(@"C:\Example\Test.docx", FileMode.Open))
  1307. /// {
  1308. /// // Load the document using fs.
  1309. /// using (DocX document = DocX.Load(fs))
  1310. /// {
  1311. /// // Do something with the document here.
  1312. ///
  1313. /// // Save all changes made to the document.
  1314. /// document.Save();
  1315. /// }// Release this document from memory.
  1316. /// }
  1317. /// </code>
  1318. /// </example>
  1319. /// <example>
  1320. /// Loading a document from a SharePoint site.
  1321. /// <code>
  1322. /// // Get the SharePoint site that you want to access.
  1323. /// using (SPSite mySite = new SPSite("http://server/sites/site"))
  1324. /// {
  1325. /// // Open a connection to the SharePoint site
  1326. /// using (SPWeb myWeb = mySite.OpenWeb())
  1327. /// {
  1328. /// // Grab a document stored on this site.
  1329. /// SPFile file = web.GetFile("Source_Folder_Name/Source_File");
  1330. ///
  1331. /// // DocX.Load requires a Stream, so open a Stream to this document.
  1332. /// Stream str = new MemoryStream(file.OpenBinary());
  1333. ///
  1334. /// // Load the file using the Stream str.
  1335. /// using (DocX document = DocX.Load(str))
  1336. /// {
  1337. /// // Do something with the document here.
  1338. ///
  1339. /// // Save all changes made to the document.
  1340. /// document.Save();
  1341. /// }// Release this document from memory.
  1342. /// }
  1343. /// }
  1344. /// </code>
  1345. /// </example>
  1346. /// <seealso cref="DocX.Load(string)"/>
  1347. /// <seealso cref="DocX.Save()"/>
  1348. public static DocX Load( Stream stream )
  1349. {
  1350. var ms = new MemoryStream();
  1351. stream.Position = 0;
  1352. var data = new byte[ stream.Length ];
  1353. stream.Read( data, 0, ( int )stream.Length );
  1354. ms.Write( data, 0, ( int )stream.Length );
  1355. // Open the docx package
  1356. var package = Package.Open( ms, FileMode.Open, FileAccess.ReadWrite );
  1357. var document = DocX.PostLoad( ref package );
  1358. document._package = package;
  1359. document._memoryStream = ms;
  1360. document._stream = stream;
  1361. return document;
  1362. }
  1363. /// <summary>
  1364. /// Loads a document into a DocX object using a fully qualified or relative filename.
  1365. /// </summary>
  1366. /// <param name="filename">The fully qualified or relative filename.</param>
  1367. /// <returns>
  1368. /// Returns a DocX object which represents the document.
  1369. /// </returns>
  1370. /// <example>
  1371. /// <code>
  1372. /// // Load a document using its fully qualified filename
  1373. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1374. /// {
  1375. /// // Do something with the document here
  1376. ///
  1377. /// // Save all changes made to document.
  1378. /// document.Save();
  1379. /// }// Release this document from memory.
  1380. /// </code>
  1381. /// <code>
  1382. /// // Load a document using its relative filename.
  1383. /// using(DocX document = DocX.Load(@"..\..\Test.docx"))
  1384. /// {
  1385. /// // Do something with the document here.
  1386. ///
  1387. /// // Save all changes made to document.
  1388. /// document.Save();
  1389. /// }// Release this document from memory.
  1390. /// </code>
  1391. /// <seealso cref="DocX.Load(System.IO.Stream)"/>
  1392. /// <seealso cref="DocX.Save()"/>
  1393. /// </example>
  1394. public static DocX Load( string filename )
  1395. {
  1396. if( !File.Exists( filename ) )
  1397. throw new FileNotFoundException( string.Format( "File could not be found {0}", filename ) );
  1398. var ms = new MemoryStream();
  1399. using( FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read, FileShare.Read ) )
  1400. {
  1401. var data = new byte[ fs.Length ];
  1402. fs.Read( data, 0, ( int )fs.Length );
  1403. ms.Write( data, 0, ( int )fs.Length );
  1404. }
  1405. // Open the docx package
  1406. var package = Package.Open( ms, FileMode.Open, FileAccess.ReadWrite );
  1407. var document = PostLoad( ref package );
  1408. document._package = package;
  1409. document._filename = filename;
  1410. document._memoryStream = ms;
  1411. return document;
  1412. }
  1413. ///<summary>
  1414. /// Applies document template to the document. Document template may include styles, headers, footers, properties, etc. as well as text content.
  1415. ///</summary>
  1416. ///<param name="templateFilePath">The path to the document template file.</param>
  1417. ///<exception cref="FileNotFoundException">The document template file not found.</exception>
  1418. public void ApplyTemplate( string templateFilePath )
  1419. {
  1420. ApplyTemplate( templateFilePath, true );
  1421. }
  1422. ///<summary>
  1423. /// Applies document template to the document. Document template may include styles, headers, footers, properties, etc. as well as text content.
  1424. ///</summary>
  1425. ///<param name="templateFilePath">The path to the document template file.</param>
  1426. ///<param name="includeContent">Whether to copy the document template text content to document.</param>
  1427. ///<exception cref="FileNotFoundException">The document template file not found.</exception>
  1428. public void ApplyTemplate( string templateFilePath, bool includeContent )
  1429. {
  1430. if( !File.Exists( templateFilePath ) )
  1431. {
  1432. throw new FileNotFoundException( string.Format( "File could not be found {0}", templateFilePath ) );
  1433. }
  1434. using( FileStream packageStream = new FileStream( templateFilePath, FileMode.Open, FileAccess.Read ) )
  1435. {
  1436. ApplyTemplate( packageStream, includeContent );
  1437. }
  1438. }
  1439. ///<summary>
  1440. /// Applies document template to the document. Document template may include styles, headers, footers, properties, etc. as well as text content.
  1441. ///</summary>
  1442. ///<param name="templateStream">The stream of the document template file.</param>
  1443. public void ApplyTemplate( Stream templateStream )
  1444. {
  1445. ApplyTemplate( templateStream, true );
  1446. }
  1447. ///<summary>
  1448. /// Applies document template to the document. Document template may include styles, headers, footers, properties, etc. as well as text content.
  1449. ///</summary>
  1450. ///<param name="templateStream">The stream of the document template file.</param>
  1451. ///<param name="includeContent">Whether to copy the document template text content to document.</param>
  1452. public void ApplyTemplate( Stream templateStream, bool includeContent )
  1453. {
  1454. var templatePackage = Package.Open( templateStream );
  1455. try
  1456. {
  1457. PackagePart documentPart = null;
  1458. XDocument documentDoc = null;
  1459. foreach( PackagePart packagePart in templatePackage.GetParts() )
  1460. {
  1461. switch( packagePart.Uri.ToString() )
  1462. {
  1463. case "/word/document.xml":
  1464. documentPart = packagePart;
  1465. using( XmlReader xr = XmlReader.Create( packagePart.GetStream( FileMode.Open, FileAccess.Read ) ) )
  1466. {
  1467. documentDoc = XDocument.Load( xr );
  1468. }
  1469. break;
  1470. case "/_rels/.rels":
  1471. if( !_package.PartExists( packagePart.Uri ) )
  1472. {
  1473. _package.CreatePart( packagePart.Uri, packagePart.ContentType, packagePart.CompressionOption );
  1474. }
  1475. var globalRelsPart = _package.GetPart( packagePart.Uri );
  1476. using(
  1477. var tr = new StreamReader(
  1478. packagePart.GetStream( FileMode.Open, FileAccess.Read ), Encoding.UTF8 ) )
  1479. {
  1480. using(
  1481. var tw = new StreamWriter(
  1482. new PackagePartStream( globalRelsPart.GetStream( FileMode.Create, FileAccess.Write ) ), Encoding.UTF8 ) )
  1483. {
  1484. tw.Write( tr.ReadToEnd() );
  1485. }
  1486. }
  1487. break;
  1488. case "/word/_rels/document.xml.rels":
  1489. break;
  1490. default:
  1491. if( !_package.PartExists( packagePart.Uri ) )
  1492. {
  1493. _package.CreatePart( packagePart.Uri, packagePart.ContentType, packagePart.CompressionOption );
  1494. }
  1495. var packagePartEncoding = Encoding.Default;
  1496. if( packagePart.Uri.ToString().EndsWith( ".xml" ) || packagePart.Uri.ToString().EndsWith( ".rels" ) )
  1497. {
  1498. packagePartEncoding = Encoding.UTF8;
  1499. }
  1500. var nativePart = _package.GetPart( packagePart.Uri );
  1501. using(
  1502. var tr = new StreamReader(
  1503. packagePart.GetStream( FileMode.Open, FileAccess.Read ), packagePartEncoding ) )
  1504. {
  1505. using(
  1506. var tw = new StreamWriter(
  1507. new PackagePartStream( nativePart.GetStream( FileMode.Create, FileAccess.Write ) ), tr.CurrentEncoding ) )
  1508. {
  1509. tw.Write( tr.ReadToEnd() );
  1510. }
  1511. }
  1512. break;
  1513. }
  1514. }
  1515. if( documentPart != null )
  1516. {
  1517. string mainContentType = documentPart.ContentType.Replace( "template.main", "document.main" );
  1518. if( _package.PartExists( documentPart.Uri ) )
  1519. {
  1520. _package.DeletePart( documentPart.Uri );
  1521. }
  1522. var documentNewPart = _package.CreatePart(
  1523. documentPart.Uri, mainContentType, documentPart.CompressionOption );
  1524. using( XmlWriter xw = XmlWriter.Create( new PackagePartStream( documentNewPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1525. {
  1526. documentDoc.WriteTo( xw );
  1527. }
  1528. foreach( PackageRelationship documentPartRel in documentPart.GetRelationships() )
  1529. {
  1530. documentNewPart.CreateRelationship(
  1531. documentPartRel.TargetUri,
  1532. documentPartRel.TargetMode,
  1533. documentPartRel.RelationshipType,
  1534. documentPartRel.Id );
  1535. }
  1536. this.PackagePart = documentNewPart;
  1537. _mainDoc = documentDoc;
  1538. PopulateDocument( this, templatePackage );
  1539. // DragonFire: I added next line and recovered ApplyTemplate method.
  1540. // I do it, becouse PopulateDocument(...) writes into field "settingsPart" the part of Template's package
  1541. // and after line "templatePackage.Close();" in finally, field "settingsPart" becomes not available and method "Save" throw an exception...
  1542. // That's why I recreated settingsParts and unlinked it from Template's package =)
  1543. _settingsPart = HelperFunctions.CreateOrGetSettingsPart( _package );
  1544. }
  1545. if( !includeContent )
  1546. {
  1547. foreach( Paragraph paragraph in this.Paragraphs )
  1548. {
  1549. paragraph.Remove( false );
  1550. }
  1551. }
  1552. }
  1553. finally
  1554. {
  1555. _package.Flush();
  1556. var documentRelsPart = _package.GetPart( new Uri( "/word/_rels/document.xml.rels", UriKind.Relative ) );
  1557. using( TextReader tr = new StreamReader( documentRelsPart.GetStream( FileMode.Open, FileAccess.Read ) ) )
  1558. {
  1559. tr.Read();
  1560. }
  1561. templatePackage.Close();
  1562. PopulateDocument( Document, _package );
  1563. }
  1564. }
  1565. /// <summary>
  1566. /// Add an Image into this document from a fully qualified or relative filename.
  1567. /// </summary>
  1568. /// <param name="filename">The fully qualified or relative filename.</param>
  1569. /// <returns>An Image file.</returns>
  1570. /// <example>
  1571. /// Add an Image into this document from a fully qualified filename.
  1572. /// <code>
  1573. /// // Load a document.
  1574. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1575. /// {
  1576. /// // Add an Image from a file.
  1577. /// document.AddImage(@"C:\Example\Image.png");
  1578. ///
  1579. /// // Save all changes made to this document.
  1580. /// document.Save();
  1581. /// }// Release this document from memory.
  1582. /// </code>
  1583. /// </example>
  1584. /// <seealso cref="AddImage(System.IO.Stream)"/>
  1585. /// <seealso cref="Paragraph.InsertPicture"/>
  1586. public Image AddImage( string filename )
  1587. {
  1588. string contentType = "";
  1589. // The extension this file has will be taken to be its format.
  1590. switch( Path.GetExtension( filename ) )
  1591. {
  1592. case ".tiff":
  1593. contentType = "image/tif";
  1594. break;
  1595. case ".tif":
  1596. contentType = "image/tif";
  1597. break;
  1598. case ".png":
  1599. contentType = "image/png";
  1600. break;
  1601. case ".bmp":
  1602. contentType = "image/png";
  1603. break;
  1604. case ".gif":
  1605. contentType = "image/gif";
  1606. break;
  1607. case ".jpg":
  1608. contentType = "image/jpg";
  1609. break;
  1610. case ".jpeg":
  1611. contentType = "image/jpeg";
  1612. break;
  1613. default:
  1614. contentType = "image/jpg";
  1615. break;
  1616. }
  1617. return AddImage( filename, contentType );
  1618. }
  1619. /// <summary>
  1620. /// Add an Image into this document from a Stream.
  1621. /// </summary>
  1622. /// <param name="stream">A Stream stream.</param>
  1623. /// <returns>An Image file.</returns>
  1624. /// <example>
  1625. /// Add an Image into a document using a Stream.
  1626. /// <code>
  1627. /// // Open a FileStream fs to an Image.
  1628. /// using (FileStream fs = new FileStream(@"C:\Example\Image.jpg", FileMode.Open))
  1629. /// {
  1630. /// // Load a document.
  1631. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1632. /// {
  1633. /// // Add an Image from a filestream fs.
  1634. /// document.AddImage(fs);
  1635. ///
  1636. /// // Save all changes made to this document.
  1637. /// document.Save();
  1638. /// }// Release this document from memory.
  1639. /// }
  1640. /// </code>
  1641. /// </example>
  1642. /// <seealso cref="AddImage(string)"/>
  1643. /// <seealso cref="Paragraph.InsertPicture"/>
  1644. public Image AddImage( Stream stream )
  1645. {
  1646. return AddImage( stream as object );
  1647. }
  1648. /// <summary>
  1649. /// Adds a hyperlink to a document and creates a Paragraph which uses it.
  1650. /// </summary>
  1651. /// <param name="text">The text as displayed by the hyperlink.</param>
  1652. /// <param name="uri">The hyperlink itself.</param>
  1653. /// <returns>Returns a hyperlink that can be inserted into a Paragraph.</returns>
  1654. /// <example>
  1655. /// Adds a hyperlink to a document and creates a Paragraph which uses it.
  1656. /// <code>
  1657. /// // Create a document.
  1658. /// using (DocX document = DocX.Create(@"Test.docx"))
  1659. /// {
  1660. /// // Add a hyperlink to this document.
  1661. /// Hyperlink h = document.AddHyperlink("Google", new Uri("http://www.google.com"));
  1662. ///
  1663. /// // Add a new Paragraph to this document.
  1664. /// Paragraph p = document.InsertParagraph();
  1665. /// p.Append("My favourite search engine is ");
  1666. /// p.AppendHyperlink(h);
  1667. /// p.Append(", I think it's great.");
  1668. ///
  1669. /// // Save all changes made to this document.
  1670. /// document.Save();
  1671. /// }
  1672. /// </code>
  1673. /// </example>
  1674. public Hyperlink AddHyperlink( string text, Uri uri )
  1675. {
  1676. var i = new XElement
  1677. (
  1678. XName.Get( "hyperlink", w.NamespaceName ),
  1679. new XAttribute( r + "id", string.Empty ),
  1680. new XAttribute( w + "history", "1" ),
  1681. new XElement( XName.Get( "r", w.NamespaceName ),
  1682. new XElement( XName.Get( "rPr", w.NamespaceName ),
  1683. new XElement( XName.Get( "rStyle", w.NamespaceName ),
  1684. new XAttribute( w + "val", "Hyperlink" ) ) ),
  1685. new XElement( XName.Get( "t", w.NamespaceName ), text ) )
  1686. );
  1687. var h = new Hyperlink( this, this.PackagePart, i );
  1688. h.text = text;
  1689. h.uri = uri;
  1690. this.AddHyperlinkStyleIfNotPresent();
  1691. return h;
  1692. }
  1693. /// <summary>
  1694. /// Adds three new Headers to this document. One for the first page, one for odd pages and one for even pages.
  1695. /// </summary>
  1696. /// <example>
  1697. /// // Create a document.
  1698. /// using (DocX document = DocX.Create(@"Test.docx"))
  1699. /// {
  1700. /// // Add header support to this document.
  1701. /// document.AddHeaders();
  1702. ///
  1703. /// // Get a collection of all headers in this document.
  1704. /// Headers headers = document.Headers;
  1705. ///
  1706. /// // The header used for the first page of this document.
  1707. /// Header first = headers.first;
  1708. ///
  1709. /// // The header used for odd pages of this document.
  1710. /// Header odd = headers.odd;
  1711. ///
  1712. /// // The header used for even pages of this document.
  1713. /// Header even = headers.even;
  1714. ///
  1715. /// // Force the document to use a different header for first, odd and even pages.
  1716. /// document.DifferentFirstPage = true;
  1717. /// document.DifferentOddAndEvenPages = true;
  1718. ///
  1719. /// // Content can be added to the Headers in the same manor that it would be added to the main document.
  1720. /// Paragraph p = first.InsertParagraph();
  1721. /// p.Append("This is the first pages header.");
  1722. ///
  1723. /// // Save all changes to this document.
  1724. /// document.Save();
  1725. /// }// Release this document from memory.
  1726. /// </example>
  1727. public void AddHeaders()
  1728. {
  1729. this.AddHeadersOrFooters( true );
  1730. _headers.Odd = Document.GetHeaderByType( "default" );
  1731. _headers.Even = Document.GetHeaderByType( "even" );
  1732. _headers.First = Document.GetHeaderByType( "first" );
  1733. }
  1734. /// <summary>
  1735. /// Adds three new Footers to this document. One for the first page, one for odd pages and one for even pages.
  1736. /// </summary>
  1737. /// <example>
  1738. /// // Create a document.
  1739. /// using (DocX document = DocX.Create(@"Test.docx"))
  1740. /// {
  1741. /// // Add footer support to this document.
  1742. /// document.AddFooters();
  1743. ///
  1744. /// // Get a collection of all footers in this document.
  1745. /// Footers footers = document.Footers;
  1746. ///
  1747. /// // The footer used for the first page of this document.
  1748. /// Footer first = footers.first;
  1749. ///
  1750. /// // The footer used for odd pages of this document.
  1751. /// Footer odd = footers.odd;
  1752. ///
  1753. /// // The footer used for even pages of this document.
  1754. /// Footer even = footers.even;
  1755. ///
  1756. /// // Force the document to use a different footer for first, odd and even pages.
  1757. /// document.DifferentFirstPage = true;
  1758. /// document.DifferentOddAndEvenPages = true;
  1759. ///
  1760. /// // Content can be added to the Footers in the same manor that it would be added to the main document.
  1761. /// Paragraph p = first.InsertParagraph();
  1762. /// p.Append("This is the first pages footer.");
  1763. ///
  1764. /// // Save all changes to this document.
  1765. /// document.Save();
  1766. /// }// Release this document from memory.
  1767. /// </example>
  1768. public void AddFooters()
  1769. {
  1770. AddHeadersOrFooters( false );
  1771. _footers.Odd = Document.GetFooterByType( "default" );
  1772. _footers.Even = Document.GetFooterByType( "even" );
  1773. _footers.First = Document.GetFooterByType( "first" );
  1774. }
  1775. /// <summary>
  1776. /// Save this document back to the location it was loaded from.
  1777. /// </summary>
  1778. /// <example>
  1779. /// <code>
  1780. /// // Load a document.
  1781. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  1782. /// {
  1783. /// // Add an Image from a file.
  1784. /// document.AddImage(@"C:\Example\Image.jpg");
  1785. ///
  1786. /// // Save all changes made to this document.
  1787. /// document.Save();
  1788. /// }// Release this document from memory.
  1789. /// </code>
  1790. /// </example>
  1791. /// <seealso cref="DocX.SaveAs(string)"/>
  1792. /// <seealso cref="DocX.Load(System.IO.Stream)"/>
  1793. /// <seealso cref="DocX.Load(string)"/>
  1794. /// <!--
  1795. /// Bug found and fixed by krugs525 on August 12 2009.
  1796. /// Use TFS compare to see exact code change.
  1797. /// -->
  1798. public void Save()
  1799. {
  1800. var headers = Headers;
  1801. // Save the main document
  1802. using( TextWriter tw = new StreamWriter( new PackagePartStream( this.PackagePart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1803. {
  1804. _mainDoc.Save( tw, SaveOptions.None );
  1805. }
  1806. if( (_settings == null) || !this.isProtected )
  1807. {
  1808. using( TextReader textReader = new StreamReader( _settingsPart.GetStream() ) )
  1809. {
  1810. _settings = XDocument.Load( textReader );
  1811. }
  1812. }
  1813. var body = _mainDoc.Root.Element( w + "body" );
  1814. var sectPr = body.Descendants( w + "sectPr" ).FirstOrDefault();
  1815. if( sectPr != null )
  1816. {
  1817. var evenHeaderRef =
  1818. (
  1819. from e in _mainDoc.Descendants( w + "headerReference" )
  1820. let type = e.Attribute( w + "type" )
  1821. where type != null && type.Value.Equals( "even", StringComparison.CurrentCultureIgnoreCase )
  1822. select e.Attribute( r + "id" ).Value
  1823. ).LastOrDefault();
  1824. if( evenHeaderRef != null )
  1825. {
  1826. var even = headers.Even.Xml;
  1827. var target = PackUriHelper.ResolvePartUri
  1828. (
  1829. this.PackagePart.Uri,
  1830. this.PackagePart.GetRelationship( evenHeaderRef ).TargetUri
  1831. );
  1832. using( TextWriter tw = new StreamWriter( new PackagePartStream( _package.GetPart( target ).GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1833. {
  1834. new XDocument
  1835. (
  1836. new XDeclaration( "1.0", "UTF-8", "yes" ),
  1837. even
  1838. ).Save( tw, SaveOptions.None );
  1839. }
  1840. }
  1841. var oddHeaderRef =
  1842. (
  1843. from e in _mainDoc.Descendants( w + "headerReference" )
  1844. let type = e.Attribute( w + "type" )
  1845. where type != null && type.Value.Equals( "default", StringComparison.CurrentCultureIgnoreCase )
  1846. select e.Attribute( r + "id" ).Value
  1847. ).LastOrDefault();
  1848. if( oddHeaderRef != null )
  1849. {
  1850. var odd = headers.Odd.Xml;
  1851. var target = PackUriHelper.ResolvePartUri
  1852. (
  1853. this.PackagePart.Uri,
  1854. this.PackagePart.GetRelationship( oddHeaderRef ).TargetUri
  1855. );
  1856. // Save header1
  1857. using( TextWriter tw = new StreamWriter( new PackagePartStream( _package.GetPart( target ).GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1858. {
  1859. new XDocument
  1860. (
  1861. new XDeclaration( "1.0", "UTF-8", "yes" ),
  1862. odd
  1863. ).Save( tw, SaveOptions.None );
  1864. }
  1865. }
  1866. var firstHeaderRef =
  1867. (
  1868. from e in _mainDoc.Descendants( w + "headerReference" )
  1869. let type = e.Attribute( w + "type" )
  1870. where type != null && type.Value.Equals( "first", StringComparison.CurrentCultureIgnoreCase )
  1871. select e.Attribute( r + "id" ).Value
  1872. ).LastOrDefault();
  1873. if( firstHeaderRef != null )
  1874. {
  1875. var first = headers.First.Xml;
  1876. var target = PackUriHelper.ResolvePartUri
  1877. (
  1878. this.PackagePart.Uri,
  1879. this.PackagePart.GetRelationship( firstHeaderRef ).TargetUri
  1880. );
  1881. // Save header3
  1882. using( TextWriter tw = new StreamWriter( new PackagePartStream( _package.GetPart( target ).GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1883. {
  1884. new XDocument
  1885. (
  1886. new XDeclaration( "1.0", "UTF-8", "yes" ),
  1887. first
  1888. ).Save( tw, SaveOptions.None );
  1889. }
  1890. }
  1891. var oddFooterRef =
  1892. (
  1893. from e in _mainDoc.Descendants( w + "footerReference" )
  1894. let type = e.Attribute( w + "type" )
  1895. where type != null && type.Value.Equals( "default", StringComparison.CurrentCultureIgnoreCase )
  1896. select e.Attribute( r + "id" ).Value
  1897. ).LastOrDefault();
  1898. if( oddFooterRef != null )
  1899. {
  1900. var odd = _footers.Odd.Xml;
  1901. var target = PackUriHelper.ResolvePartUri
  1902. (
  1903. this.PackagePart.Uri,
  1904. this.PackagePart.GetRelationship( oddFooterRef ).TargetUri
  1905. );
  1906. // Save header1
  1907. using( TextWriter tw = new StreamWriter( new PackagePartStream( _package.GetPart( target ).GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1908. {
  1909. new XDocument
  1910. (
  1911. new XDeclaration( "1.0", "UTF-8", "yes" ),
  1912. odd
  1913. ).Save( tw, SaveOptions.None );
  1914. }
  1915. }
  1916. var evenFooterRef =
  1917. (
  1918. from e in _mainDoc.Descendants( w + "footerReference" )
  1919. let type = e.Attribute( w + "type" )
  1920. where type != null && type.Value.Equals( "even", StringComparison.CurrentCultureIgnoreCase )
  1921. select e.Attribute( r + "id" ).Value
  1922. ).LastOrDefault();
  1923. if( evenFooterRef != null )
  1924. {
  1925. var even = _footers.Even.Xml;
  1926. var target = PackUriHelper.ResolvePartUri
  1927. (
  1928. this.PackagePart.Uri,
  1929. this.PackagePart.GetRelationship( evenFooterRef ).TargetUri
  1930. );
  1931. // Save header2
  1932. using( TextWriter tw = new StreamWriter( new PackagePartStream( _package.GetPart( target ).GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1933. {
  1934. new XDocument
  1935. (
  1936. new XDeclaration( "1.0", "UTF-8", "yes" ),
  1937. even
  1938. ).Save( tw, SaveOptions.None );
  1939. }
  1940. }
  1941. var firstFooterRef =
  1942. (
  1943. from e in _mainDoc.Descendants( w + "footerReference" )
  1944. let type = e.Attribute( w + "type" )
  1945. where type != null && type.Value.Equals( "first", StringComparison.CurrentCultureIgnoreCase )
  1946. select e.Attribute( r + "id" ).Value
  1947. ).LastOrDefault();
  1948. if( firstFooterRef != null )
  1949. {
  1950. var first = _footers.First.Xml;
  1951. var target = PackUriHelper.ResolvePartUri
  1952. (
  1953. this.PackagePart.Uri,
  1954. this.PackagePart.GetRelationship( firstFooterRef ).TargetUri
  1955. );
  1956. // Save header3
  1957. using( TextWriter tw = new StreamWriter( new PackagePartStream( _package.GetPart( target ).GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1958. {
  1959. new XDocument
  1960. (
  1961. new XDeclaration( "1.0", "UTF-8", "yes" ),
  1962. first
  1963. ).Save( tw, SaveOptions.None );
  1964. }
  1965. }
  1966. // Save the settings document.
  1967. using( TextWriter tw = new StreamWriter( new PackagePartStream( _settingsPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1968. {
  1969. _settings.Save( tw, SaveOptions.None );
  1970. }
  1971. if( _endnotesPart != null )
  1972. {
  1973. using( TextWriter tw = new StreamWriter( new PackagePartStream( _endnotesPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1974. {
  1975. _endnotes.Save( tw, SaveOptions.None );
  1976. }
  1977. }
  1978. if( _footnotesPart != null )
  1979. {
  1980. using( TextWriter tw = new StreamWriter( new PackagePartStream( _footnotesPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1981. {
  1982. _footnotes.Save( tw, SaveOptions.None );
  1983. }
  1984. }
  1985. if( _stylesPart != null )
  1986. {
  1987. using( TextWriter tw = new StreamWriter( new PackagePartStream( _stylesPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1988. {
  1989. _styles.Save( tw, SaveOptions.None );
  1990. }
  1991. }
  1992. if( _stylesWithEffectsPart != null )
  1993. {
  1994. using( TextWriter tw = new StreamWriter( new PackagePartStream( _stylesWithEffectsPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  1995. {
  1996. _stylesWithEffects.Save( tw, SaveOptions.None );
  1997. }
  1998. }
  1999. if( _numberingPart != null )
  2000. {
  2001. using( TextWriter tw = new StreamWriter( new PackagePartStream( _numberingPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  2002. {
  2003. _numbering.Save( tw, SaveOptions.None );
  2004. }
  2005. }
  2006. if( _fontTablePart != null )
  2007. {
  2008. using( TextWriter tw = new StreamWriter( new PackagePartStream( _fontTablePart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  2009. {
  2010. _fontTable.Save( tw, SaveOptions.None );
  2011. }
  2012. }
  2013. }
  2014. // Close the document so that it can be saved.
  2015. _package.Flush();
  2016. #region Save this document back to a file or stream, that was specified by the user at save time.
  2017. if( _filename != null )
  2018. {
  2019. using( FileStream fs = new FileStream( _filename, FileMode.Create ) )
  2020. {
  2021. fs.Write( _memoryStream.ToArray(), 0, ( int )_memoryStream.Length );
  2022. }
  2023. }
  2024. else if( _stream.CanSeek ) //Check if stream can be seeked to support System.Web.HttpResponseStream.
  2025. {
  2026. // Set the length of this stream to 0
  2027. _stream.SetLength( 0 );
  2028. // Write to the beginning of the stream
  2029. _stream.Position = 0;
  2030. _memoryStream.WriteTo( _stream );
  2031. _memoryStream.Flush();
  2032. }
  2033. #endregion
  2034. }
  2035. /// <summary>
  2036. /// Save this document to a file.
  2037. /// </summary>
  2038. /// <param name="filename">The filename to save this document as.</param>
  2039. /// <example>
  2040. /// Load a document from one file and save it to another.
  2041. /// <code>
  2042. /// // Load a document using its fully qualified filename.
  2043. /// DocX document = DocX.Load(@"C:\Example\Test1.docx");
  2044. ///
  2045. /// // Insert a new Paragraph
  2046. /// document.InsertParagraph("Hello world!", false);
  2047. ///
  2048. /// // Save the document to a new location.
  2049. /// document.SaveAs(@"C:\Example\Test2.docx");
  2050. /// </code>
  2051. /// </example>
  2052. /// <example>
  2053. /// Load a document from a Stream and save it to a file.
  2054. /// <code>
  2055. /// DocX document;
  2056. /// using (FileStream fs1 = new FileStream(@"C:\Example\Test1.docx", FileMode.Open))
  2057. /// {
  2058. /// // Load a document using a stream.
  2059. /// document = DocX.Load(fs1);
  2060. ///
  2061. /// // Insert a new Paragraph
  2062. /// document.InsertParagraph("Hello world again!", false);
  2063. /// }
  2064. ///
  2065. /// // Save the document to a new location.
  2066. /// document.SaveAs(@"C:\Example\Test2.docx");
  2067. /// </code>
  2068. /// </example>
  2069. /// <seealso cref="DocX.Save()"/>
  2070. /// <seealso cref="DocX.Load(System.IO.Stream)"/>
  2071. /// <seealso cref="DocX.Load(string)"/>
  2072. public void SaveAs( string filename )
  2073. {
  2074. _filename = filename;
  2075. _stream = null;
  2076. Save();
  2077. }
  2078. /// <summary>
  2079. /// Save this document to a Stream.
  2080. /// </summary>
  2081. /// <param name="stream">The Stream to save this document to.</param>
  2082. /// <example>
  2083. /// Load a document from a file and save it to a Stream.
  2084. /// <code>
  2085. /// // Place holder for a document.
  2086. /// DocX document;
  2087. ///
  2088. /// using (FileStream fs1 = new FileStream(@"C:\Example\Test1.docx", FileMode.Open))
  2089. /// {
  2090. /// // Load a document using a stream.
  2091. /// document = DocX.Load(fs1);
  2092. ///
  2093. /// // Insert a new Paragraph
  2094. /// document.InsertParagraph("Hello world again!", false);
  2095. /// }
  2096. ///
  2097. /// using (FileStream fs2 = new FileStream(@"C:\Example\Test2.docx", FileMode.Create))
  2098. /// {
  2099. /// // Save the document to a different stream.
  2100. /// document.SaveAs(fs2);
  2101. /// }
  2102. ///
  2103. /// // Release this document from memory.
  2104. /// document.Dispose();
  2105. /// </code>
  2106. /// </example>
  2107. /// <example>
  2108. /// Load a document from one Stream and save it to another.
  2109. /// <code>
  2110. /// DocX document;
  2111. /// using (FileStream fs1 = new FileStream(@"C:\Example\Test1.docx", FileMode.Open))
  2112. /// {
  2113. /// // Load a document using a stream.
  2114. /// document = DocX.Load(fs1);
  2115. ///
  2116. /// // Insert a new Paragraph
  2117. /// document.InsertParagraph("Hello world again!", false);
  2118. /// }
  2119. ///
  2120. /// using (FileStream fs2 = new FileStream(@"C:\Example\Test2.docx", FileMode.Create))
  2121. /// {
  2122. /// // Save the document to a different stream.
  2123. /// document.SaveAs(fs2);
  2124. /// }
  2125. /// </code>
  2126. /// </example>
  2127. /// <seealso cref="DocX.Save()"/>
  2128. /// <seealso cref="DocX.Load(System.IO.Stream)"/>
  2129. /// <seealso cref="DocX.Load(string)"/>
  2130. public void SaveAs( Stream stream )
  2131. {
  2132. _filename = null;
  2133. _stream = stream;
  2134. Save();
  2135. }
  2136. /// <summary>
  2137. /// Add a core property to this document. If a core property already exists with the same name it will be replaced. Core property names are case insensitive.
  2138. /// </summary>
  2139. ///<param name="propertyName">The property name.</param>
  2140. ///<param name="propertyValue">The property value.</param>
  2141. ///<example>
  2142. /// Add a core properties of each type to a document.
  2143. /// <code>
  2144. /// // Load Example.docx
  2145. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  2146. /// {
  2147. /// // If this document does not contain a core property called 'forename', create one.
  2148. /// if (!document.CoreProperties.ContainsKey("forename"))
  2149. /// {
  2150. /// // Create a new core property called 'forename' and set its value.
  2151. /// document.AddCoreProperty("forename", "Cathal");
  2152. /// }
  2153. ///
  2154. /// // Get this documents core property called 'forename'.
  2155. /// string forenameValue = document.CoreProperties["forename"];
  2156. ///
  2157. /// // Print all of the information about this core property to Console.
  2158. /// Console.WriteLine(string.Format("Name: '{0}', Value: '{1}'\nPress any key...", "forename", forenameValue));
  2159. ///
  2160. /// // Save all changes made to this document.
  2161. /// document.Save();
  2162. /// } // Release this document from memory.
  2163. ///
  2164. /// // Wait for the user to press a key before exiting.
  2165. /// Console.ReadKey();
  2166. /// </code>
  2167. /// </example>
  2168. /// <seealso cref="CoreProperties"/>
  2169. /// <seealso cref="CustomProperty"/>
  2170. /// <seealso cref="CustomProperties"/>
  2171. public void AddCoreProperty( string propertyName, string propertyValue )
  2172. {
  2173. var propertyNamespacePrefix = propertyName.Contains(":") ? propertyName.Split(':')[0] : "cp";
  2174. var propertyLocalName = propertyName.Contains(":") ? propertyName.Split(':')[1] : propertyName;
  2175. // If this document does not contain a coreFilePropertyPart create one.)
  2176. if( !_package.PartExists( new Uri( "/docProps/core.xml", UriKind.Relative ) ) )
  2177. throw new Exception( "Core properties part doesn't exist." );
  2178. XDocument corePropDoc;
  2179. var corePropPart = _package.GetPart( new Uri( "/docProps/core.xml", UriKind.Relative ) );
  2180. using( TextReader tr = new StreamReader( corePropPart.GetStream( FileMode.Open, FileAccess.Read ) ) )
  2181. {
  2182. corePropDoc = XDocument.Load( tr );
  2183. }
  2184. var corePropElement =
  2185. ( from propElement in corePropDoc.Root.Elements()
  2186. where ( propElement.Name.LocalName.Equals( propertyLocalName ) )
  2187. select propElement ).SingleOrDefault();
  2188. if( corePropElement != null )
  2189. {
  2190. corePropElement.SetValue( propertyValue );
  2191. }
  2192. else
  2193. {
  2194. var propertyNamespace = corePropDoc.Root.GetNamespaceOfPrefix( propertyNamespacePrefix );
  2195. corePropDoc.Root.Add( new XElement( XName.Get( propertyLocalName, propertyNamespace.NamespaceName ), propertyValue ) );
  2196. }
  2197. using( TextWriter tw = new StreamWriter( new PackagePartStream( corePropPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  2198. {
  2199. corePropDoc.Save( tw );
  2200. }
  2201. DocX.UpdateCorePropertyValue( this, propertyLocalName, propertyValue );
  2202. }
  2203. /// <summary>
  2204. /// Add a custom property to this document. If a custom property already exists with the same name it will be replace. CustomProperty names are case insensitive.
  2205. /// </summary>
  2206. /// <param name="cp">The CustomProperty to add to this document.</param>
  2207. /// <example>
  2208. /// Add a custom properties of each type to a document.
  2209. /// <code>
  2210. /// // Load Example.docx
  2211. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  2212. /// {
  2213. /// // A CustomProperty called forename which stores a string.
  2214. /// CustomProperty forename;
  2215. ///
  2216. /// // If this document does not contain a custom property called 'forename', create one.
  2217. /// if (!document.CustomProperties.ContainsKey("forename"))
  2218. /// {
  2219. /// // Create a new custom property called 'forename' and set its value.
  2220. /// document.AddCustomProperty(new CustomProperty("forename", "Cathal"));
  2221. /// }
  2222. ///
  2223. /// // Get this documents custom property called 'forename'.
  2224. /// forename = document.CustomProperties["forename"];
  2225. ///
  2226. /// // Print all of the information about this CustomProperty to Console.
  2227. /// Console.WriteLine(string.Format("Name: '{0}', Value: '{1}'\nPress any key...", forename.Name, forename.Value));
  2228. ///
  2229. /// // Save all changes made to this document.
  2230. /// document.Save();
  2231. /// } // Release this document from memory.
  2232. ///
  2233. /// // Wait for the user to press a key before exiting.
  2234. /// Console.ReadKey();
  2235. /// </code>
  2236. /// </example>
  2237. /// <seealso cref="CustomProperty"/>
  2238. /// <seealso cref="CustomProperties"/>
  2239. public void AddCustomProperty( CustomProperty cp )
  2240. {
  2241. // If this document does not contain a customFilePropertyPart create one.
  2242. if( !_package.PartExists( new Uri( "/docProps/custom.xml", UriKind.Relative ) ) )
  2243. {
  2244. HelperFunctions.CreateCustomPropertiesPart( this );
  2245. }
  2246. XDocument customPropDoc;
  2247. var customPropPart = _package.GetPart( new Uri( "/docProps/custom.xml", UriKind.Relative ) );
  2248. using( TextReader tr = new StreamReader( customPropPart.GetStream( FileMode.Open, FileAccess.Read ) ) )
  2249. {
  2250. customPropDoc = XDocument.Load( tr, LoadOptions.PreserveWhitespace );
  2251. }
  2252. // Each custom property has a PID, get the highest PID in this document.
  2253. IEnumerable<int> pids =
  2254. (
  2255. from d in customPropDoc.Descendants()
  2256. where d.Name.LocalName == "property"
  2257. select int.Parse( d.Attribute( XName.Get( "pid" ) ).Value )
  2258. );
  2259. int pid = 1;
  2260. if( pids.Count() > 0 )
  2261. {
  2262. pid = pids.Max();
  2263. }
  2264. // Check if a custom property already exists with this name
  2265. var customProperty =
  2266. (
  2267. from d in customPropDoc.Descendants()
  2268. where ( d.Name.LocalName == "property" ) && ( d.Attribute( XName.Get( "name" ) ).Value.Equals( cp.Name, StringComparison.InvariantCultureIgnoreCase ) )
  2269. select d
  2270. ).SingleOrDefault();
  2271. // If a custom property with this name already exists remove it.
  2272. if( customProperty != null )
  2273. {
  2274. customProperty.Remove();
  2275. }
  2276. var propertiesElement = customPropDoc.Element( XName.Get( "Properties", customPropertiesSchema.NamespaceName ) );
  2277. propertiesElement.Add
  2278. (
  2279. new XElement
  2280. (
  2281. XName.Get( "property", customPropertiesSchema.NamespaceName ),
  2282. new XAttribute( "fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" ),
  2283. new XAttribute( "pid", pid + 1 ),
  2284. new XAttribute( "name", cp.Name ),
  2285. new XElement( customVTypesSchema + cp.Type, cp.Value ?? "" )
  2286. )
  2287. );
  2288. // Save the custom properties
  2289. using( TextWriter tw = new StreamWriter( new PackagePartStream( customPropPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  2290. {
  2291. customPropDoc.Save( tw, SaveOptions.None );
  2292. }
  2293. // Refresh all fields in this document which display this custom property.
  2294. DocX.UpdateCustomPropertyValue( this, cp.Name, ( cp.Value ?? "" ).ToString() );
  2295. }
  2296. public override Paragraph InsertParagraph()
  2297. {
  2298. var p = base.InsertParagraph();
  2299. p.PackagePart = this.PackagePart;
  2300. return p;
  2301. }
  2302. public override Paragraph InsertParagraph( int index, string text, bool trackChanges )
  2303. {
  2304. var p = base.InsertParagraph( index, text, trackChanges );
  2305. p.PackagePart = this.PackagePart;
  2306. return p;
  2307. }
  2308. public override Paragraph InsertParagraph( Paragraph p )
  2309. {
  2310. p.PackagePart = this.PackagePart;
  2311. return base.InsertParagraph( p );
  2312. }
  2313. public override Paragraph InsertParagraph( int index, Paragraph p )
  2314. {
  2315. p.PackagePart = this.PackagePart;
  2316. return base.InsertParagraph( index, p );
  2317. }
  2318. public override Paragraph InsertParagraph( int index, string text, bool trackChanges, Formatting formatting )
  2319. {
  2320. var p = base.InsertParagraph( index, text, trackChanges, formatting );
  2321. p.PackagePart = this.PackagePart;
  2322. return p;
  2323. }
  2324. public override Paragraph InsertParagraph( string text )
  2325. {
  2326. var p = base.InsertParagraph( text );
  2327. p.PackagePart = this.PackagePart;
  2328. return p;
  2329. }
  2330. public override Paragraph InsertParagraph( string text, bool trackChanges )
  2331. {
  2332. var p = base.InsertParagraph( text, trackChanges );
  2333. p.PackagePart = this.PackagePart;
  2334. return p;
  2335. }
  2336. public override Paragraph InsertParagraph( string text, bool trackChanges, Formatting formatting )
  2337. {
  2338. var p = base.InsertParagraph( text, trackChanges, formatting );
  2339. p.PackagePart = this.PackagePart;
  2340. return p;
  2341. }
  2342. public Paragraph[] InsertParagraphs( string text )
  2343. {
  2344. var textArray = text.Split( '\n' );
  2345. var paragraphs = new List<Paragraph>();
  2346. foreach( var textForParagraph in textArray )
  2347. {
  2348. var p = base.InsertParagraph( text );
  2349. p.PackagePart = this.PackagePart;
  2350. paragraphs.Add( p );
  2351. }
  2352. return paragraphs.ToArray();
  2353. }
  2354. /// <summary>
  2355. /// Create an equation and insert it in the new paragraph
  2356. /// </summary>
  2357. public override Paragraph InsertEquation( String equation )
  2358. {
  2359. var p = base.InsertEquation( equation );
  2360. p.PackagePart = this.PackagePart;
  2361. return p;
  2362. }
  2363. /// <summary>
  2364. /// Insert a chart in document
  2365. /// </summary>
  2366. public void InsertChart( Chart chart )
  2367. {
  2368. this.InsertChart( chart, null );
  2369. }
  2370. /// <summary>
  2371. /// Insert a chart in document after the specified paragraph
  2372. /// </summary>
  2373. public void InsertChartAfterParagraph( Chart chart, Paragraph paragraph )
  2374. {
  2375. this.InsertChart( chart, paragraph );
  2376. }
  2377. private void InsertChart( Chart chart, Paragraph paragraph )
  2378. {
  2379. Paragraph p;
  2380. // Create a new chart part uri.
  2381. var chartPartUriPath = String.Empty;
  2382. var chartIndex = 1;
  2383. do
  2384. {
  2385. chartPartUriPath = String.Format( "/word/charts/chart{0}.xml", chartIndex );
  2386. chartIndex++;
  2387. } while( _package.PartExists( new Uri( chartPartUriPath, UriKind.Relative ) ) );
  2388. // Create chart part.
  2389. var chartPackagePart = _package.CreatePart( new Uri( chartPartUriPath, UriKind.Relative ), "application/vnd.openxmlformats-officedocument.drawingml.chart+xml", CompressionOption.Normal );
  2390. // Create a new chart relationship
  2391. var relID = this.GetNextFreeRelationshipID();
  2392. var rel = this.PackagePart.CreateRelationship( chartPackagePart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart", relID );
  2393. // Save a chart info the chartPackagePart
  2394. if( paragraph == null )
  2395. {
  2396. using( TextWriter tw = new StreamWriter( new PackagePartStream( chartPackagePart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  2397. {
  2398. chart.Xml.Save( tw );
  2399. }
  2400. p = InsertParagraph();
  2401. }
  2402. else
  2403. {
  2404. using( TextWriter tw = new StreamWriter( chartPackagePart.GetStream( FileMode.Create, FileAccess.Write ) ) )
  2405. {
  2406. chart.Xml.Save( tw );
  2407. }
  2408. p = paragraph;
  2409. }
  2410. // Insert a new chart into a paragraph.
  2411. var chartElement = new XElement( XName.Get( "r", w.NamespaceName ),
  2412. new XElement( XName.Get( "drawing", w.NamespaceName ),
  2413. new XElement( XName.Get( "inline", wp.NamespaceName ),
  2414. new XElement( XName.Get( "extent", wp.NamespaceName ), new XAttribute( "cx", "5486400" ), new XAttribute( "cy", "3200400" ) ),
  2415. new XElement( XName.Get( "effectExtent", wp.NamespaceName ), new XAttribute( "l", "0" ), new XAttribute( "t", "0" ), new XAttribute( "r", "19050" ), new XAttribute( "b", "19050" ) ),
  2416. new XElement( XName.Get( "docPr", wp.NamespaceName ), new XAttribute( "id", "1" ), new XAttribute( "name", "chart" ) ),
  2417. new XElement( XName.Get( "graphic", a.NamespaceName ),
  2418. new XElement( XName.Get( "graphicData", a.NamespaceName ),
  2419. new XAttribute( "uri", c.NamespaceName ),
  2420. new XElement( XName.Get( "chart", c.NamespaceName ),
  2421. new XAttribute( XName.Get( "id", r.NamespaceName ), relID ) ) ) ) ) ) );
  2422. p.Xml.Add( chartElement );
  2423. }
  2424. public List<Section> GetSections()
  2425. {
  2426. var paragraphs = Paragraphs;
  2427. var sections = new List<Section>();
  2428. var sectionParagraphs = new List<Paragraph>();
  2429. foreach( Paragraph paragraph in paragraphs )
  2430. {
  2431. var sectionInPara = paragraph.Xml.Descendants().FirstOrDefault( s => s.Name.LocalName == "sectPr" );
  2432. if( sectionInPara != null )
  2433. {
  2434. sectionParagraphs.Add( paragraph );
  2435. var section = new Section( Document, sectionInPara );
  2436. section.SectionParagraphs = sectionParagraphs;
  2437. sections.Add( section );
  2438. sectionParagraphs = new List<Paragraph>();
  2439. }
  2440. else
  2441. {
  2442. sectionParagraphs.Add( paragraph );
  2443. }
  2444. }
  2445. XElement body = _mainDoc.Root.Element( XName.Get( "body", w.NamespaceName ) );
  2446. XElement baseSectionXml = body.Element( XName.Get( "sectPr", w.NamespaceName ) );
  2447. var baseSection = new Section( Document, baseSectionXml );
  2448. baseSection.SectionParagraphs = sectionParagraphs;
  2449. sections.Add( baseSection );
  2450. return sections;
  2451. }
  2452. /// <summary>
  2453. /// Create a new List
  2454. /// </summary>
  2455. public List AddList( string listText = null, int level = 0, ListItemType listType = ListItemType.Numbered, int? startNumber = null, bool trackChanges = false, bool continueNumbering = false )
  2456. {
  2457. return AddListItem( new List( this, null ), listText, level, listType, startNumber, trackChanges, continueNumbering );
  2458. }
  2459. /// <summary>
  2460. /// Add a list item to an existing list
  2461. /// </summary>
  2462. public List AddListItem( List list, string listText, int level = 0, ListItemType listType = ListItemType.Numbered, int? startNumber = null, bool trackChanges = false, bool continueNumbering = false )
  2463. {
  2464. if( startNumber.HasValue && continueNumbering )
  2465. throw new InvalidOperationException( "Cannot specify a start number and at the same time continue numbering from another list" );
  2466. var result = HelperFunctions.CreateItemInList( list, listText, level, listType, startNumber, trackChanges, continueNumbering );
  2467. var lastItem = result.Items.LastOrDefault();
  2468. if( lastItem != null )
  2469. {
  2470. lastItem.PackagePart = this.PackagePart;
  2471. }
  2472. return result;
  2473. }
  2474. /// <summary>
  2475. /// Insert a list in the document
  2476. /// </summary>
  2477. /// <param name="list">The list to insert into the document.</param>
  2478. /// <returns>The list that was inserted into the document.</returns>
  2479. public override List InsertList( List list )
  2480. {
  2481. base.InsertList( list );
  2482. return list;
  2483. }
  2484. public override List InsertList( List list, Font fontFamily, double fontSize )
  2485. {
  2486. base.InsertList( list, fontFamily, fontSize );
  2487. return list;
  2488. }
  2489. public override List InsertList( List list, double fontSize )
  2490. {
  2491. base.InsertList( list, fontSize );
  2492. return list;
  2493. }
  2494. /// <summary>
  2495. /// Insert a list at an index location in the document
  2496. /// </summary>
  2497. /// <param name="index">Index in document to insert the list.</param>
  2498. /// <param name="list">The list that was inserted into the document.</param>
  2499. /// <returns></returns>
  2500. public new List InsertList( int index, List list )
  2501. {
  2502. base.InsertList( index, list );
  2503. return list;
  2504. }
  2505. /// <summary>
  2506. /// Insert a default Table of Contents in the current document
  2507. /// </summary>
  2508. public TableOfContents InsertDefaultTableOfContents()
  2509. {
  2510. return InsertTableOfContents( "Table of contents", TableOfContentsSwitches.O | TableOfContentsSwitches.H | TableOfContentsSwitches.Z | TableOfContentsSwitches.U );
  2511. }
  2512. /// <summary>
  2513. /// Insert a Table of Contents in the current document
  2514. /// </summary>
  2515. public TableOfContents InsertTableOfContents( string title, TableOfContentsSwitches switches, string headerStyle = null, int maxIncludeLevel = 3, int? rightTabPos = null )
  2516. {
  2517. var toc = TableOfContents.CreateTableOfContents( this, title, switches, headerStyle, maxIncludeLevel, rightTabPos );
  2518. Xml.Add( toc.Xml );
  2519. return toc;
  2520. }
  2521. /// <summary>
  2522. /// Insert a Table of Contents in the current document at a specific location (prior to the referenced paragraph)
  2523. /// </summary>
  2524. public TableOfContents InsertTableOfContents( Paragraph reference, string title, TableOfContentsSwitches switches, string headerStyle = null, int maxIncludeLevel = 3, int? rightTabPos = null )
  2525. {
  2526. var toc = TableOfContents.CreateTableOfContents( this, title, switches, headerStyle, maxIncludeLevel, rightTabPos );
  2527. reference.Xml.AddBeforeSelf( toc.Xml );
  2528. return toc;
  2529. }
  2530. /// <summary>
  2531. /// Copy the Document into a new Document
  2532. /// </summary>
  2533. /// <returns>Returns a copy of a the Document</returns>
  2534. public DocX Copy()
  2535. {
  2536. var memorystream = new MemoryStream();
  2537. this.SaveAs( memorystream );
  2538. memorystream.Seek( 0, SeekOrigin.Begin );
  2539. return Load( memorystream );
  2540. }
  2541. public void AddPasswordProtection( EditRestrictions editRestrictions, string password )
  2542. {
  2543. // Intellectual Property information :
  2544. //
  2545. // The following code handles password protection of Word documents (Open Specifications)
  2546. // and is an implementation of algorithm(s) described in Office Document Cryptography Structure
  2547. // here: https://msdn.microsoft.com/en-us/library/cc313071.aspx.
  2548. //
  2549. // The code’s use is covered under Microsoft’s Open Specification Promise
  2550. // described here: https://msdn.microsoft.com/en-US/openspecifications/dn646765
  2551. // Remove existing password protection
  2552. this.RemoveProtection();
  2553. // If no EditRestrictions, nothing to do
  2554. if( editRestrictions == EditRestrictions.none )
  2555. return;
  2556. // Variables
  2557. int maxPasswordLength = 15;
  2558. var saltArray = new byte[ 16 ];
  2559. var keyValues = new byte[ 14 ];
  2560. // Init DocumentProtection element
  2561. var documentProtection = new XElement( XName.Get( "documentProtection", w.NamespaceName ) );
  2562. documentProtection.Add( new XAttribute( XName.Get( "edit", w.NamespaceName ), editRestrictions.ToString() ) );
  2563. documentProtection.Add( new XAttribute( XName.Get( "enforcement", w.NamespaceName ), "1" ) );
  2564. int[] InitialCodeArray = { 0xE1F0, 0x1D0F, 0xCC9C, 0x84C0, 0x110C, 0x0E10, 0xF1CE, 0x313E, 0x1872, 0xE139, 0xD40F, 0x84F9, 0x280C, 0xA96A, 0x4EC3 };
  2565. int[,] EncryptionMatrix = new int[ 15, 7 ]
  2566. {
  2567. /* char 1 */ { 0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09},
  2568. /* char 2 */ { 0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF},
  2569. /* char 3 */ { 0x4563, 0x8AC6, 0x05AD, 0x0B5A, 0x16B4, 0x2D68, 0x5AD0},
  2570. /* char 4 */ { 0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40},
  2571. /* char 5 */ { 0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5},
  2572. /* char 6 */ { 0x6F45, 0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A},
  2573. /* char 7 */ { 0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9},
  2574. /* char 8 */ { 0x47D3, 0x8FA6, 0x0F6D, 0x1EDA, 0x3DB4, 0x7B68, 0xF6D0},
  2575. /* char 9 */ { 0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC},
  2576. /* char 10 */ { 0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10},
  2577. /* char 11 */ { 0xAA51, 0x4483, 0x8906, 0x022D, 0x045A, 0x08B4, 0x1168},
  2578. /* char 12 */ { 0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C},
  2579. /* char 13 */ { 0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6, 0x1DAD},
  2580. /* char 14 */ { 0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC},
  2581. /* char 15 */ { 0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4}
  2582. };
  2583. // Generate the salt
  2584. var random = new RNGCryptoServiceProvider();
  2585. random.GetNonZeroBytes( saltArray );
  2586. // Validate the provided password
  2587. if( !String.IsNullOrEmpty( password ) )
  2588. {
  2589. password = password.Substring( 0, Math.Min( password.Length, maxPasswordLength ) );
  2590. var byteChars = new byte[ password.Length ];
  2591. for( int i = 0; i < password.Length; i++ )
  2592. {
  2593. var temp = Convert.ToInt32( password[ i ] );
  2594. byteChars[ i ] = Convert.ToByte( temp & 0x00FF );
  2595. if( byteChars[ i ] == 0 )
  2596. {
  2597. byteChars[ i ] = Convert.ToByte( ( temp & 0x00FF ) >> 8 );
  2598. }
  2599. }
  2600. var intHighOrderWord = InitialCodeArray[ byteChars.Length - 1 ];
  2601. for( int i = 0; i < byteChars.Length; i++ )
  2602. {
  2603. int tmp = maxPasswordLength - byteChars.Length + i;
  2604. for( int intBit = 0; intBit < 7; intBit++ )
  2605. {
  2606. if( ( byteChars[ i ] & ( 0x0001 << intBit ) ) != 0 )
  2607. {
  2608. intHighOrderWord ^= EncryptionMatrix[ tmp, intBit ];
  2609. }
  2610. }
  2611. }
  2612. int intLowOrderWord = 0;
  2613. // For each character in the strPassword, going backwards
  2614. for( int i = byteChars.Length - 1; i >= 0; i-- )
  2615. {
  2616. intLowOrderWord = ( ( ( intLowOrderWord >> 14 ) & 0x0001 ) | ( ( intLowOrderWord << 1 ) & 0x7FFF ) ) ^ byteChars[ i ];
  2617. }
  2618. intLowOrderWord = ( ( ( intLowOrderWord >> 14 ) & 0x0001 ) | ( ( intLowOrderWord << 1 ) & 0x7FFF ) ) ^ byteChars.Length ^ 0xCE4B;
  2619. // Combine the Low and High Order Word
  2620. var intCombinedkey = ( intHighOrderWord << 16 ) + intLowOrderWord;
  2621. // The byte order of the result shall be reversed [Example: 0x64CEED7E becomes 7EEDCE64. end example],
  2622. // and that value shall be hashed as defined by the attribute values.
  2623. for( int i = 0; i < 4; i++ )
  2624. {
  2625. keyValues[ i ] = Convert.ToByte( ( ( uint )( intCombinedkey & ( 0x000000FF << ( i * 8 ) ) ) ) >> ( i * 8 ) );
  2626. }
  2627. }
  2628. var sb = new StringBuilder();
  2629. for( int intTemp = 0; intTemp < 4; intTemp++ )
  2630. {
  2631. sb.Append( Convert.ToString( keyValues[ intTemp ], 16 ) );
  2632. }
  2633. keyValues = Encoding.Unicode.GetBytes( sb.ToString().ToUpper() );
  2634. keyValues = MergeArrays( keyValues, saltArray );
  2635. int iterations = 100000;
  2636. var sha1 = new SHA1Managed();
  2637. keyValues = sha1.ComputeHash( keyValues );
  2638. var iterator = new byte[ 4 ];
  2639. for( int i = 0; i < iterations; i++ )
  2640. {
  2641. iterator[ 0 ] = Convert.ToByte( ( i & 0x000000FF ) >> 0 );
  2642. iterator[ 1 ] = Convert.ToByte( ( i & 0x0000FF00 ) >> 8 );
  2643. iterator[ 2 ] = Convert.ToByte( ( i & 0x00FF0000 ) >> 16 );
  2644. iterator[ 3 ] = Convert.ToByte( ( i & 0xFF000000 ) >> 24 );
  2645. keyValues = MergeArrays( iterator, keyValues );
  2646. keyValues = sha1.ComputeHash( keyValues );
  2647. }
  2648. documentProtection.Add( new XAttribute( XName.Get( "cryptProviderType", w.NamespaceName ), "rsaFull" ) );
  2649. documentProtection.Add( new XAttribute( XName.Get( "cryptAlgorithmClass", w.NamespaceName ), "hash" ) );
  2650. documentProtection.Add( new XAttribute( XName.Get( "cryptAlgorithmType", w.NamespaceName ), "typeAny" ) );
  2651. documentProtection.Add( new XAttribute( XName.Get( "cryptAlgorithmSid", w.NamespaceName ), "4" ) );
  2652. documentProtection.Add( new XAttribute( XName.Get( "cryptSpinCount", w.NamespaceName ), iterations.ToString() ) );
  2653. documentProtection.Add( new XAttribute( XName.Get( "hash", w.NamespaceName ), Convert.ToBase64String( keyValues ) ) );
  2654. documentProtection.Add( new XAttribute( XName.Get( "salt", w.NamespaceName ), Convert.ToBase64String( saltArray ) ) );
  2655. _settings.Root.AddFirst( documentProtection );
  2656. }
  2657. #endregion
  2658. #region Internal Methods
  2659. internal float getMarginAttribute( XName name )
  2660. {
  2661. var body = _mainDoc.Root.Element( XName.Get( "body", w.NamespaceName ) );
  2662. var sectPr = body.Element( XName.Get( "sectPr", w.NamespaceName ) );
  2663. var pgMar = sectPr?.Element( XName.Get( "pgMar", w.NamespaceName ) );
  2664. var top = pgMar?.Attribute( name );
  2665. if( top != null )
  2666. {
  2667. float f;
  2668. if( float.TryParse( top.Value, out f ) )
  2669. return ( int )( f / _pageSizeMultiplier );
  2670. }
  2671. return 0;
  2672. }
  2673. internal void setMarginAttribute( XName xName, float value )
  2674. {
  2675. var body = _mainDoc.Root.Element( XName.Get( "body", w.NamespaceName ) );
  2676. var sectPr = body.Element( XName.Get( "sectPr", w.NamespaceName ) );
  2677. var pgMar = sectPr?.Element( XName.Get( "pgMar", w.NamespaceName ) );
  2678. var top = pgMar?.Attribute( xName );
  2679. top?.SetValue( value * Convert.ToInt32( _pageSizeMultiplier ) );
  2680. }
  2681. internal string GetCollectiveText( List<PackagePart> list )
  2682. {
  2683. var text = string.Empty;
  2684. foreach( var hp in list )
  2685. {
  2686. using( TextReader tr = new StreamReader( hp.GetStream() ) )
  2687. {
  2688. var d = XDocument.Load( tr );
  2689. var sb = new StringBuilder();
  2690. // Loop through each text item in this run
  2691. foreach( XElement descendant in d.Descendants() )
  2692. {
  2693. switch( descendant.Name.LocalName )
  2694. {
  2695. case "tab":
  2696. sb.Append( "\t" );
  2697. break;
  2698. case "br":
  2699. sb.Append( "\n" );
  2700. break;
  2701. case "t":
  2702. goto case "delText";
  2703. case "delText":
  2704. sb.Append( descendant.Value );
  2705. break;
  2706. default:
  2707. break;
  2708. }
  2709. }
  2710. text += "\n" + sb;
  2711. }
  2712. }
  2713. return text;
  2714. }
  2715. internal static void PostCreation( Package package, DocumentTypes documentType = DocumentTypes.Document )
  2716. {
  2717. XDocument mainDoc, stylesDoc, numberingDoc;
  2718. #region MainDocumentPart
  2719. // Create the main document part for this package
  2720. var mainDocPart = ( documentType == DocumentTypes.Document )
  2721. ? package.CreatePart( new Uri( "/word/document.xml", UriKind.Relative ), HelperFunctions.DOCUMENT_DOCUMENTTYPE, CompressionOption.Normal )
  2722. : package.CreatePart( new Uri( "/word/document.xml", UriKind.Relative ), HelperFunctions.TEMPLATE_DOCUMENTTYPE, CompressionOption.Normal );
  2723. package.CreateRelationship( mainDocPart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" );
  2724. // Load the document part into a XDocument object
  2725. using( TextReader tr = new StreamReader( mainDocPart.GetStream( FileMode.Create, FileAccess.ReadWrite ) ) )
  2726. {
  2727. mainDoc = XDocument.Parse
  2728. ( @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
  2729. <w:document xmlns:ve=""http://schemas.openxmlformats.org/markup-compatibility/2006"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns:m=""http://schemas.openxmlformats.org/officeDocument/2006/math"" xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:wp=""http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"" xmlns:w10=""urn:schemas-microsoft-com:office:word"" xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"" xmlns:wne=""http://schemas.microsoft.com/office/word/2006/wordml"" xmlns:a=""http://schemas.openxmlformats.org/drawingml/2006/main"" xmlns:c=""http://schemas.openxmlformats.org/drawingml/2006/chart"">
  2730. <w:body>
  2731. <w:sectPr w:rsidR=""003E25F4"" w:rsidSect=""00FC3028"">
  2732. <w:pgSz w:w=""11906"" w:h=""16838""/>
  2733. <w:pgMar w:top=""1440"" w:right=""1440"" w:bottom=""1440"" w:left=""1440"" w:header=""708"" w:footer=""708"" w:gutter=""0""/>
  2734. <w:cols w:space=""708""/>
  2735. <w:docGrid w:linePitch=""360""/>
  2736. </w:sectPr>
  2737. </w:body>
  2738. </w:document>"
  2739. );
  2740. }
  2741. // Save the main document
  2742. using( TextWriter tw = new StreamWriter( new PackagePartStream( mainDocPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  2743. {
  2744. mainDoc.Save( tw, SaveOptions.None );
  2745. }
  2746. #endregion
  2747. #region StylePart
  2748. stylesDoc = HelperFunctions.AddDefaultStylesXml( package );
  2749. #endregion
  2750. #region NumberingPart
  2751. numberingDoc = HelperFunctions.AddDefaultNumberingXml( package );
  2752. #endregion
  2753. package.Close();
  2754. }
  2755. internal static DocX PostLoad( ref Package package )
  2756. {
  2757. var document = new DocX( null, null );
  2758. document._package = package;
  2759. document.Document = document;
  2760. #region MainDocumentPart
  2761. document.PackagePart = package.GetParts().Where
  2762. (
  2763. p => p.ContentType.Equals( HelperFunctions.DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase ) ||
  2764. p.ContentType.Equals( HelperFunctions.TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase )
  2765. ).Single();
  2766. using( TextReader tr = new StreamReader( document.PackagePart.GetStream( FileMode.Open, FileAccess.Read ) ) )
  2767. {
  2768. document._mainDoc = XDocument.Load( tr, LoadOptions.PreserveWhitespace );
  2769. }
  2770. #endregion
  2771. DocX.PopulateDocument( document, package );
  2772. using( TextReader tr = new StreamReader( document._settingsPart.GetStream() ) )
  2773. {
  2774. document._settings = XDocument.Load( tr );
  2775. }
  2776. document._paragraphLookup.Clear();
  2777. var paragraphs = document.Paragraphs;
  2778. foreach( var p in paragraphs )
  2779. {
  2780. if( !document._paragraphLookup.ContainsKey( p._endIndex ) )
  2781. {
  2782. document._paragraphLookup.Add( p._endIndex, p );
  2783. }
  2784. }
  2785. return document;
  2786. }
  2787. internal void AddHyperlinkStyleIfNotPresent()
  2788. {
  2789. var word_styles_Uri = new Uri( "/word/styles.xml", UriKind.Relative );
  2790. // If the internal document contains no /word/styles.xml create one.
  2791. if( !_package.PartExists( word_styles_Uri ) )
  2792. {
  2793. HelperFunctions.AddDefaultStylesXml( _package );
  2794. }
  2795. // Load the styles.xml into memory.
  2796. XDocument word_styles;
  2797. using( TextReader tr = new StreamReader( _package.GetPart( word_styles_Uri ).GetStream() ) )
  2798. {
  2799. word_styles = XDocument.Load( tr );
  2800. }
  2801. bool hyperlinkStyleExists =
  2802. (
  2803. from s in word_styles.Element( w + "styles" ).Elements()
  2804. let styleId = s.Attribute( XName.Get( "styleId", w.NamespaceName ) )
  2805. where ( styleId != null && styleId.Value == "Hyperlink" )
  2806. select s
  2807. ).Count() > 0;
  2808. if( !hyperlinkStyleExists )
  2809. {
  2810. var style = new XElement
  2811. (
  2812. w + "style",
  2813. new XAttribute( w + "type", "character" ),
  2814. new XAttribute( w + "styleId", "Hyperlink" ),
  2815. new XElement( w + "name", new XAttribute( w + "val", "Hyperlink" ) ),
  2816. new XElement( w + "basedOn", new XAttribute( w + "val", "DefaultParagraphFont" ) ),
  2817. new XElement( w + "uiPriority", new XAttribute( w + "val", "99" ) ),
  2818. new XElement( w + "unhideWhenUsed" ),
  2819. new XElement( w + "rsid", new XAttribute( w + "val", "0005416C" ) ),
  2820. new XElement
  2821. (
  2822. w + "rPr",
  2823. new XElement( w + "color", new XAttribute( w + "val", "0000FF" ), new XAttribute( w + "themeColor", "hyperlink" ) ),
  2824. new XElement
  2825. (
  2826. w + "u",
  2827. new XAttribute( w + "val", "single" )
  2828. )
  2829. )
  2830. );
  2831. word_styles.Element( w + "styles" ).Add( style );
  2832. // Save the styles document.
  2833. using( TextWriter tw = new StreamWriter( new PackagePartStream( _package.GetPart( word_styles_Uri ).GetStream() ) ) )
  2834. {
  2835. word_styles.Save( tw );
  2836. }
  2837. }
  2838. }
  2839. /// <summary>
  2840. /// Adds a Header to a document.
  2841. /// If the document already contains a Header it will be replaced.
  2842. /// </summary>
  2843. /// <returns>The Header that was added to the document.</returns>
  2844. internal void AddHeadersOrFooters( bool b )
  2845. {
  2846. var element = b ? "hdr" : "ftr";
  2847. var reference = b ? "header" : "footer";
  2848. this.DeleteHeadersOrFooters( b );
  2849. var sectPr = _mainDoc.Root.Element( w + "body" ).Element( w + "sectPr" );
  2850. for( int i = 1; i < 4; i++ )
  2851. {
  2852. var header_uri = string.Format( "/word/{0}{1}.xml", reference, i );
  2853. var headerPart = _package.CreatePart( new Uri( header_uri, UriKind.Relative ), string.Format( "application/vnd.openxmlformats-officedocument.wordprocessingml.{0}+xml", reference ), CompressionOption.Normal );
  2854. var headerRelationship = this.PackagePart.CreateRelationship( headerPart.Uri, TargetMode.Internal, string.Format( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/{0}", reference ) );
  2855. XDocument header;
  2856. // Load the document part into a XDocument object
  2857. using( TextReader tr = new StreamReader( headerPart.GetStream( FileMode.Create, FileAccess.ReadWrite ) ) )
  2858. {
  2859. header = XDocument.Parse
  2860. ( string.Format( @"<?xml version=""1.0"" encoding=""utf-16"" standalone=""yes""?>
  2861. <w:{0} xmlns:ve=""http://schemas.openxmlformats.org/markup-compatibility/2006"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns:m=""http://schemas.openxmlformats.org/officeDocument/2006/math"" xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:wp=""http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"" xmlns:w10=""urn:schemas-microsoft-com:office:word"" xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"" xmlns:wne=""http://schemas.microsoft.com/office/word/2006/wordml"">
  2862. <w:p w:rsidR=""009D472B"" w:rsidRDefault=""009D472B"">
  2863. <w:pPr>
  2864. <w:pStyle w:val=""{1}"" />
  2865. </w:pPr>
  2866. </w:p>
  2867. </w:{0}>", element, reference )
  2868. );
  2869. }
  2870. // Save the main document
  2871. using( TextWriter tw = new StreamWriter( new PackagePartStream( headerPart.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  2872. {
  2873. header.Save( tw, SaveOptions.None );
  2874. }
  2875. string type;
  2876. switch( i )
  2877. {
  2878. case 1:
  2879. type = "default";
  2880. break;
  2881. case 2:
  2882. type = "even";
  2883. break;
  2884. case 3:
  2885. type = "first";
  2886. break;
  2887. default:
  2888. throw new ArgumentOutOfRangeException();
  2889. }
  2890. sectPr.Add
  2891. (
  2892. new XElement
  2893. (
  2894. w + string.Format( "{0}Reference", reference ),
  2895. new XAttribute( w + "type", type ),
  2896. new XAttribute( r + "id", headerRelationship.Id )
  2897. )
  2898. );
  2899. }
  2900. }
  2901. internal void DeleteHeadersOrFooters( bool b )
  2902. {
  2903. string reference = "footer";
  2904. if( b )
  2905. reference = "header";
  2906. // Get all header Relationships in this document.
  2907. var header_relationships = this.PackagePart.GetRelationshipsByType( string.Format( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/{0}", reference ) );
  2908. foreach( PackageRelationship header_relationship in header_relationships )
  2909. {
  2910. // Get the TargetUri for this Part.
  2911. Uri header_uri = header_relationship.TargetUri;
  2912. // Check to see if the document actually contains the Part.
  2913. if( !header_uri.OriginalString.StartsWith( "/word/" ) )
  2914. header_uri = new Uri( "/word/" + header_uri.OriginalString, UriKind.Relative );
  2915. if( _package.PartExists( header_uri ) )
  2916. {
  2917. // Delete the Part
  2918. _package.DeletePart( header_uri );
  2919. // Get all references to this Relationship in the document.
  2920. var query =
  2921. (
  2922. from e in _mainDoc.Descendants( XName.Get( "body", w.NamespaceName ) ).Descendants()
  2923. where ( e.Name.LocalName == string.Format( "{0}Reference", reference ) ) && ( e.Attribute( r + "id" ).Value == header_relationship.Id )
  2924. select e
  2925. );
  2926. // Remove all references to this Relationship in the document.
  2927. for( int i = 0; i < query.Count(); i++ )
  2928. query.ElementAt( i ).Remove();
  2929. // Delete the Relationship.
  2930. _package.DeleteRelationship( header_relationship.Id );
  2931. }
  2932. }
  2933. }
  2934. internal Image AddImage( object o, string contentType = "image/jpeg" )
  2935. {
  2936. // Open a Stream to the new image being added.
  2937. var newImageStream = ( o is string ) ? new FileStream( o as string, FileMode.Open, FileAccess.Read ) : o as Stream;
  2938. // Get all image parts in word\document.xml
  2939. var ImageRelationships = this.PackagePart.GetRelationshipsByType( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" );
  2940. var imageParts = ImageRelationships.Select( ir => _package.GetParts().FirstOrDefault( p => p.Uri.ToString().EndsWith( ir.TargetUri.ToString() ) ) )
  2941. .Where( x => ( x != null ) )
  2942. .ToList();
  2943. foreach( PackagePart relsPart in _package.GetParts().Where( part => part.Uri.ToString().Contains( "/word/" ) ).Where( part => part.ContentType.Equals( "application/vnd.openxmlformats-package.relationships+xml" ) ) )
  2944. {
  2945. XDocument relsPartContent;
  2946. using( TextReader tr = new StreamReader( relsPart.GetStream( FileMode.Open, FileAccess.Read ) ) )
  2947. {
  2948. relsPartContent = XDocument.Load( tr );
  2949. }
  2950. var imageRelationships =
  2951. relsPartContent.Root.Elements().Where
  2952. (
  2953. imageRel =>
  2954. imageRel.Attribute( XName.Get( "Type" ) ).Value.Equals( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" )
  2955. );
  2956. foreach( XElement imageRelationship in imageRelationships )
  2957. {
  2958. if( imageRelationship.Attribute( XName.Get( "Target" ) ) != null )
  2959. {
  2960. var targetModeAttr = imageRelationship.Attribute( XName.Get( "TargetMode" ) );
  2961. var targetMode = ( targetModeAttr != null ) ? targetModeAttr.Value : string.Empty;
  2962. if( !targetMode.Equals( "External" ) )
  2963. {
  2964. var imagePartUri = Path.Combine( Path.GetDirectoryName( relsPart.Uri.ToString() ), imageRelationship.Attribute( XName.Get( "Target" ) ).Value );
  2965. imagePartUri = Path.GetFullPath( imagePartUri.Replace( "\\_rels", string.Empty ) );
  2966. imagePartUri = imagePartUri.Replace( Path.GetFullPath( "\\" ), string.Empty ).Replace( "\\", "/" );
  2967. if( !imagePartUri.StartsWith( "/" ) )
  2968. {
  2969. imagePartUri = "/" + imagePartUri;
  2970. }
  2971. var imagePart = _package.GetPart( new Uri( imagePartUri, UriKind.Relative ) );
  2972. imageParts.Add( imagePart );
  2973. }
  2974. }
  2975. }
  2976. }
  2977. // Loop through each image part in this document.
  2978. foreach( PackagePart pp in imageParts )
  2979. {
  2980. // Open a tempory Stream to this image part.
  2981. using( Stream tempStream = pp.GetStream( FileMode.Open, FileAccess.Read ) )
  2982. {
  2983. // Compare this image to the new image being added.
  2984. if( HelperFunctions.IsSameFile( tempStream, newImageStream ) )
  2985. {
  2986. // Get the image object for this image part
  2987. var id = this.PackagePart.GetRelationshipsByType( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" )
  2988. .Where( r => r.TargetUri == pp.Uri )
  2989. .Select( r => r.Id ).First();
  2990. // Return the Image object
  2991. return Images.Where( i => i.Id == id ).First();
  2992. }
  2993. }
  2994. }
  2995. var imgPartUriPath = string.Empty;
  2996. var extension = contentType.Substring( contentType.LastIndexOf( "/" ) + 1 );
  2997. do
  2998. {
  2999. // Create a new image part.
  3000. imgPartUriPath = string.Format
  3001. (
  3002. "/word/media/{0}.{1}",
  3003. Guid.NewGuid(), // The unique part.
  3004. extension
  3005. );
  3006. } while( _package.PartExists( new Uri( imgPartUriPath, UriKind.Relative ) ) );
  3007. // We are now guareenteed that imgPartUriPath is unique.
  3008. var img = _package.CreatePart( new Uri( imgPartUriPath, UriKind.Relative ), contentType, CompressionOption.Normal );
  3009. // Create a new image relationship
  3010. var rel = this.PackagePart.CreateRelationship( img.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" );
  3011. // Open a Stream to the newly created Image part.
  3012. using( Stream stream = new PackagePartStream( img.GetStream( FileMode.Create, FileAccess.Write ) ) )
  3013. {
  3014. // Using the Stream to the real image, copy this streams data into the newly create Image part.
  3015. using( newImageStream )
  3016. {
  3017. byte[] bytes = new byte[ newImageStream.Length ];
  3018. newImageStream.Read( bytes, 0, ( int )newImageStream.Length );
  3019. stream.Write( bytes, 0, ( int )newImageStream.Length );
  3020. }// Close the Stream to the new image.
  3021. }// Close the Stream to the new image part.
  3022. return new Image( this, rel );
  3023. }
  3024. internal static void UpdateCorePropertyValue( DocX document, string corePropertyName, string corePropertyValue )
  3025. {
  3026. var matchPattern = string.Format( @"(DOCPROPERTY)?{0}\\\*MERGEFORMAT", corePropertyName ).ToLower();
  3027. foreach( XElement e in document._mainDoc.Descendants( XName.Get( "fldSimple", w.NamespaceName ) ) )
  3028. {
  3029. var attr_value = e.Attribute( XName.Get( "instr", w.NamespaceName ) ).Value.Replace( " ", string.Empty ).Trim().ToLower();
  3030. if( Regex.IsMatch( attr_value, matchPattern ) )
  3031. {
  3032. var firstRun = e.Element( w + "r" );
  3033. var firstText = firstRun.Element( w + "t" );
  3034. var rPr = firstText.Element( w + "rPr" );
  3035. // Delete everything and insert updated text value
  3036. e.RemoveNodes();
  3037. var t = new XElement( w + "t", rPr, corePropertyValue );
  3038. Xceed.Words.NET.Text.PreserveSpace( t );
  3039. e.Add( new XElement( firstRun.Name, firstRun.Attributes(), firstRun.Element( XName.Get( "rPr", w.NamespaceName ) ), t ) );
  3040. }
  3041. }
  3042. #region Headers
  3043. var headerParts = from headerPart in document._package.GetParts()
  3044. where ( Regex.IsMatch( headerPart.Uri.ToString(), @"/word/header\d?.xml" ) )
  3045. select headerPart;
  3046. foreach( PackagePart pp in headerParts )
  3047. {
  3048. var header = XDocument.Load( new StreamReader( pp.GetStream() ) );
  3049. foreach( XElement e in header.Descendants( XName.Get( "fldSimple", w.NamespaceName ) ) )
  3050. {
  3051. string attr_value = e.Attribute( XName.Get( "instr", w.NamespaceName ) ).Value.Replace( " ", string.Empty ).Trim().ToLower();
  3052. if( Regex.IsMatch( attr_value, matchPattern ) )
  3053. {
  3054. var firstRun = e.Element( w + "r" );
  3055. // Delete everything and insert updated text value
  3056. e.RemoveNodes();
  3057. var t = new XElement( w + "t", corePropertyValue );
  3058. Xceed.Words.NET.Text.PreserveSpace( t );
  3059. e.Add( new XElement( firstRun.Name, firstRun.Attributes(), firstRun.Element( XName.Get( "rPr", w.NamespaceName ) ), t ) );
  3060. }
  3061. }
  3062. using( TextWriter tw = new StreamWriter( new PackagePartStream( pp.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  3063. {
  3064. header.Save( tw );
  3065. }
  3066. }
  3067. #endregion
  3068. #region Footers
  3069. var footerParts = from footerPart in document._package.GetParts()
  3070. where ( Regex.IsMatch( footerPart.Uri.ToString(), @"/word/footer\d?.xml" ) )
  3071. select footerPart;
  3072. foreach( PackagePart pp in footerParts )
  3073. {
  3074. var footer = XDocument.Load( new StreamReader( pp.GetStream() ) );
  3075. foreach( XElement e in footer.Descendants( XName.Get( "fldSimple", w.NamespaceName ) ) )
  3076. {
  3077. string attr_value = e.Attribute( XName.Get( "instr", w.NamespaceName ) ).Value.Replace( " ", string.Empty ).Trim().ToLower();
  3078. if( Regex.IsMatch( attr_value, matchPattern ) )
  3079. {
  3080. var firstRun = e.Element( w + "r" );
  3081. // Delete everything and insert updated text value
  3082. e.RemoveNodes();
  3083. var t = new XElement( w + "t", corePropertyValue );
  3084. Xceed.Words.NET.Text.PreserveSpace( t );
  3085. e.Add( new XElement( firstRun.Name, firstRun.Attributes(), firstRun.Element( XName.Get( "rPr", w.NamespaceName ) ), t ) );
  3086. }
  3087. }
  3088. using( TextWriter tw = new StreamWriter( new PackagePartStream( pp.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  3089. {
  3090. footer.Save( tw );
  3091. }
  3092. }
  3093. #endregion
  3094. DocX.PopulateDocument( document, document._package );
  3095. }
  3096. /// <summary>
  3097. /// Update the custom properties inside the document
  3098. /// </summary>
  3099. /// <param name="document">The DocX document</param>
  3100. /// <param name="customPropertyName">The property used inside the document</param>
  3101. /// <param name="customPropertyValue">The new value for the property</param>
  3102. /// <remarks>Different version of Word create different Document XML.</remarks>
  3103. internal static void UpdateCustomPropertyValue( DocX document, string customPropertyName, string customPropertyValue )
  3104. {
  3105. // A list of documents, which will contain, The Main Document and if they exist: header1, header2, header3, footer1, footer2, footer3.
  3106. var documents = new List<XElement> { document._mainDoc.Root };
  3107. // Check if each header exists and add if if so.
  3108. #region Headers
  3109. var headers = document.Headers;
  3110. if( headers.First != null )
  3111. {
  3112. documents.Add( headers.First.Xml );
  3113. }
  3114. if( headers.Odd != null )
  3115. {
  3116. documents.Add( headers.Odd.Xml );
  3117. }
  3118. if( headers.Even != null )
  3119. {
  3120. documents.Add( headers.Even.Xml );
  3121. }
  3122. #endregion
  3123. // Check if each footer exists and add if if so.
  3124. #region Footers
  3125. var footers = document.Footers;
  3126. if( footers.First != null )
  3127. {
  3128. documents.Add( footers.First.Xml );
  3129. }
  3130. if( footers.Odd != null )
  3131. {
  3132. documents.Add( footers.Odd.Xml );
  3133. }
  3134. if( footers.Even != null )
  3135. {
  3136. documents.Add( footers.Even.Xml );
  3137. }
  3138. #endregion
  3139. var match_value = string.Format( @"DOCPROPERTY {0} \* MERGEFORMAT", customPropertyName.Contains( " " ) ? "\"" + customPropertyName + "\"" : customPropertyName )
  3140. .Replace( " ", string.Empty );
  3141. // Process each document in the list.
  3142. foreach( XElement doc in documents )
  3143. {
  3144. #region Word 2010+
  3145. foreach( XElement e in doc.Descendants( XName.Get( "instrText", w.NamespaceName ) ) )
  3146. {
  3147. var attr_value = e.Value.Replace( " ", string.Empty ).Trim();
  3148. if( attr_value.Equals( match_value, StringComparison.CurrentCultureIgnoreCase ) )
  3149. {
  3150. var node = e.Parent.NextNode;
  3151. bool found = false;
  3152. while( true )
  3153. {
  3154. if( node.NodeType == XmlNodeType.Element )
  3155. {
  3156. var ele = node as XElement;
  3157. var match = ele.Descendants( XName.Get( "t", w.NamespaceName ) );
  3158. if( match.Any() )
  3159. {
  3160. if( !found )
  3161. {
  3162. match.First().Value = customPropertyValue;
  3163. found = true;
  3164. }
  3165. else
  3166. {
  3167. ele.RemoveNodes();
  3168. }
  3169. }
  3170. else
  3171. {
  3172. match = ele.Descendants( XName.Get( "fldChar", w.NamespaceName ) );
  3173. if( match.Any() )
  3174. {
  3175. var endMatch = match.First().Attribute( XName.Get( "fldCharType", w.NamespaceName ) );
  3176. if( endMatch != null && endMatch.Value == "end" )
  3177. {
  3178. break;
  3179. }
  3180. }
  3181. }
  3182. }
  3183. node = node.NextNode;
  3184. }
  3185. }
  3186. }
  3187. #endregion
  3188. #region < Word 2010
  3189. foreach( XElement e in doc.Descendants( XName.Get( "fldSimple", w.NamespaceName ) ) )
  3190. {
  3191. var attr_value = e.Attribute( XName.Get( "instr", w.NamespaceName ) ).Value.Replace( " ", string.Empty ).Trim();
  3192. if( attr_value.Equals( match_value, StringComparison.CurrentCultureIgnoreCase ) )
  3193. {
  3194. var firstRun = e.Element( w + "r" );
  3195. var firstText = firstRun.Element( w + "t" );
  3196. var rPr = firstText.Element( w + "rPr" );
  3197. // Delete everything and insert updated text value
  3198. e.RemoveNodes();
  3199. var t = new XElement( w + "t", rPr, customPropertyValue );
  3200. Xceed.Words.NET.Text.PreserveSpace( t );
  3201. e.Add( new XElement( firstRun.Name, firstRun.Attributes(), firstRun.Element( XName.Get( "rPr", w.NamespaceName ) ), t ) );
  3202. }
  3203. }
  3204. #endregion
  3205. }
  3206. }
  3207. internal XDocument AddStylesForList()
  3208. {
  3209. var fileUri = new Uri( "/word/styles.xml", UriKind.Relative );
  3210. // Create the /word/styles.xml if it doesn't already exist
  3211. if( !_package.PartExists( fileUri ) )
  3212. HelperFunctions.AddDefaultStylesXml( _package );
  3213. // Load the xml into memory.
  3214. XDocument stylesDoc;
  3215. using( TextReader tr = new StreamReader( _package.GetPart( fileUri ).GetStream() ) )
  3216. stylesDoc = XDocument.Load( tr );
  3217. bool listStyleExists =
  3218. (
  3219. from s in stylesDoc.Element( w + "styles" ).Elements()
  3220. let styleId = s.Attribute( XName.Get( "styleId", w.NamespaceName ) )
  3221. where ( styleId != null && styleId.Value == "ListParagraph" )
  3222. select s
  3223. ).Any();
  3224. if( !listStyleExists )
  3225. {
  3226. var style = new XElement
  3227. (
  3228. w + "style",
  3229. new XAttribute( w + "type", "paragraph" ),
  3230. new XAttribute( w + "styleId", "ListParagraph" ),
  3231. new XElement( w + "name", new XAttribute( w + "val", "List Paragraph" ) ),
  3232. new XElement( w + "basedOn", new XAttribute( w + "val", "Normal" ) ),
  3233. new XElement( w + "uiPriority", new XAttribute( w + "val", "34" ) ),
  3234. new XElement( w + "qformat" ),
  3235. new XElement( w + "rsid", new XAttribute( w + "val", "00832EE1" ) ),
  3236. new XElement
  3237. (
  3238. w + "rPr",
  3239. new XElement( w + "ind", new XAttribute( w + "left", "720" ) ),
  3240. new XElement
  3241. (
  3242. w + "contextualSpacing"
  3243. )
  3244. )
  3245. );
  3246. stylesDoc.Element( w + "styles" ).Add( style );
  3247. // Save the document
  3248. using( TextWriter tw = new StreamWriter( _package.GetPart( fileUri ).GetStream() ) )
  3249. stylesDoc.Save( tw );
  3250. }
  3251. return stylesDoc;
  3252. }
  3253. internal bool getMirrorMargins(XName name)
  3254. {
  3255. var body = _mainDoc.Root.Element(XName.Get("body", DocX.w.NamespaceName));
  3256. var sectPr = body.Element(XName.Get("sectPr", DocX.w.NamespaceName));
  3257. var mirrorMargins = sectPr?.Element(XName.Get("mirrorMargins", DocX.w.NamespaceName));
  3258. return (mirrorMargins != null);
  3259. }
  3260. internal void setMirrorMargins(XName name, bool value)
  3261. {
  3262. var body = _mainDoc.Root.Element(XName.Get("body", DocX.w.NamespaceName));
  3263. var sectPr = body.Element(XName.Get("sectPr", DocX.w.NamespaceName));
  3264. var mirrorMargins = sectPr?.Element(XName.Get("mirrorMargins", DocX.w.NamespaceName));
  3265. if (mirrorMargins == null)
  3266. sectPr.Add(new XElement(w + "mirrorMargins", string.Empty));
  3267. else
  3268. {
  3269. if (!value)
  3270. mirrorMargins.Remove();
  3271. }
  3272. }
  3273. #endregion
  3274. #region Private Methods
  3275. private static DocX CreateDocument( DocumentTypes documentType )
  3276. {
  3277. // Store this document in memory
  3278. var ms = new MemoryStream();
  3279. // Create the docx package
  3280. var package = Package.Open( ms, FileMode.Create, FileAccess.ReadWrite );
  3281. DocX.PostCreation( package, documentType );
  3282. return DocX.Load( ms );
  3283. }
  3284. private Header GetHeaderByType( string type )
  3285. {
  3286. return ( Header )GetHeaderOrFooterByType( type, true );
  3287. }
  3288. private Footer GetFooterByType( string type )
  3289. {
  3290. return ( Footer )GetHeaderOrFooterByType( type, false );
  3291. }
  3292. private object GetHeaderOrFooterByType( string type, bool isHeader )
  3293. {
  3294. // Switch which handles either case Header\Footer, this just cuts down on code duplication.
  3295. string reference = "footerReference";
  3296. if( isHeader )
  3297. reference = "headerReference";
  3298. // Get the Id of the [default, even or first] [Header or Footer]
  3299. string Id =
  3300. (
  3301. from e in _mainDoc.Descendants( XName.Get( "body", w.NamespaceName ) ).Descendants()
  3302. where ( e.Name.LocalName == reference ) && ( e.Attribute( w + "type" ).Value == type )
  3303. select e.Attribute( r + "id" ).Value
  3304. ).LastOrDefault();
  3305. if( Id != null )
  3306. {
  3307. // Get the Xml file for this Header or Footer.
  3308. var partUri = this.PackagePart.GetRelationship( Id ).TargetUri;
  3309. // Weird problem with PackaePart API.
  3310. if( !partUri.OriginalString.StartsWith( "/word/" ) )
  3311. partUri = new Uri( "/word/" + partUri.OriginalString, UriKind.Relative );
  3312. // Get the Part and open a stream to get the Xml file.
  3313. var part = _package.GetPart( partUri );
  3314. using( TextReader tr = new StreamReader( part.GetStream() ) )
  3315. {
  3316. var doc = XDocument.Load( tr );
  3317. // Header and Footer extend Container.
  3318. Container c;
  3319. if( isHeader )
  3320. {
  3321. c = new Header( this, doc.Element( w + "hdr" ), part );
  3322. }
  3323. else
  3324. {
  3325. c = new Footer( this, doc.Element( w + "ftr" ), part );
  3326. }
  3327. return c;
  3328. }
  3329. }
  3330. // If we got this far something went wrong.
  3331. return null;
  3332. }
  3333. private void merge_images( PackagePart remote_pp, DocX remote_document, XDocument remote_mainDoc, String contentType )
  3334. {
  3335. // Before doing any other work, check to see if this image is actually referenced in the document.
  3336. // In my testing I have found cases of Images inside documents that are not referenced
  3337. var remote_rel = remote_document.PackagePart.GetRelationships().Where( r => r.TargetUri.OriginalString.Equals( remote_pp.Uri.OriginalString.Replace( "/word/", "" ) ) ).FirstOrDefault();
  3338. if( remote_rel == null )
  3339. {
  3340. remote_rel = remote_document.PackagePart.GetRelationships().Where( r => r.TargetUri.OriginalString.Equals( remote_pp.Uri.OriginalString ) ).FirstOrDefault();
  3341. if( remote_rel == null )
  3342. return;
  3343. }
  3344. var remote_Id = remote_rel.Id;
  3345. var remote_hash = this.ComputeMD5HashString( remote_pp.GetStream() );
  3346. var image_parts = _package.GetParts().Where( pp => pp.ContentType.Equals( contentType ) );
  3347. bool found = false;
  3348. foreach( var part in image_parts )
  3349. {
  3350. var local_hash = ComputeMD5HashString( part.GetStream() );
  3351. if( local_hash.Equals( remote_hash ) )
  3352. {
  3353. // This image already exists in this document.
  3354. found = true;
  3355. var local_rel = this.PackagePart.GetRelationships().Where( r => r.TargetUri.OriginalString.Equals( part.Uri.OriginalString.Replace( "/word/", "" ) ) ).FirstOrDefault();
  3356. if( local_rel == null )
  3357. {
  3358. local_rel = this.PackagePart.GetRelationships().Where( r => r.TargetUri.OriginalString.Equals( part.Uri.OriginalString ) ).FirstOrDefault();
  3359. }
  3360. if( local_rel != null )
  3361. {
  3362. var new_Id = local_rel.Id;
  3363. // Replace all instances of remote_Id in the local document with local_Id
  3364. this.ReplaceAllRemoteID( remote_mainDoc, "blip", "embed", a.NamespaceName, remote_Id, new_Id );
  3365. // Replace all instances of remote_Id in the local document with local_Id (for shapes)
  3366. this.ReplaceAllRemoteID( remote_mainDoc, "imagedata", "id", v.NamespaceName, remote_Id, new_Id );
  3367. }
  3368. break;
  3369. }
  3370. }
  3371. // This image does not exist in this document.
  3372. if( !found )
  3373. {
  3374. var new_uri = remote_pp.Uri.OriginalString;
  3375. new_uri = new_uri.Remove( new_uri.LastIndexOf( "/" ) );
  3376. new_uri += "/" + Guid.NewGuid() + contentType.Replace( "image/", "." );
  3377. if( !new_uri.StartsWith( "/" ) )
  3378. {
  3379. new_uri = "/" + new_uri;
  3380. }
  3381. var new_pp = _package.CreatePart( new Uri( new_uri, UriKind.Relative ), remote_pp.ContentType, CompressionOption.Normal );
  3382. using( Stream s_read = remote_pp.GetStream() )
  3383. {
  3384. using( Stream s_write = new PackagePartStream( new_pp.GetStream( FileMode.Create ) ) )
  3385. {
  3386. var buffer = new byte[ 32768 ];
  3387. int read;
  3388. while( ( read = s_read.Read( buffer, 0, buffer.Length ) ) > 0 )
  3389. {
  3390. s_write.Write( buffer, 0, read );
  3391. }
  3392. }
  3393. }
  3394. var pr = this.PackagePart.CreateRelationship( new Uri( new_uri, UriKind.Relative ), TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" );
  3395. var new_Id = pr.Id;
  3396. //Check if the remote relationship id is a default rId from Word
  3397. Match relationshipID = Regex.Match( remote_Id, @"rId\d+", RegexOptions.IgnoreCase );
  3398. // Replace all instances of remote_Id in the local document with local_Id
  3399. this.ReplaceAllRemoteID( remote_mainDoc, "blip", "embed", a.NamespaceName, remote_Id, new_Id );
  3400. if( !relationshipID.Success )
  3401. {
  3402. // Replace all instances of remote_Id in the local document with local_Id
  3403. this.ReplaceAllRemoteID( _mainDoc, "blip", "embed", a.NamespaceName, remote_Id, new_Id );
  3404. // Replace all instances of remote_Id in the local document with local_Id (for shapes)
  3405. this.ReplaceAllRemoteID( _mainDoc, "imagedata", "id", v.NamespaceName, remote_Id, new_Id );
  3406. }
  3407. // Replace all instances of remote_Id in the local document with local_Id (for shapes)
  3408. this.ReplaceAllRemoteID( remote_mainDoc, "imagedata", "id", v.NamespaceName, remote_Id, new_Id );
  3409. }
  3410. }
  3411. private void ReplaceAllRemoteID( XDocument remote_mainDoc, string localName, string localNameAttribute, string namespaceName, string remote_Id, string new_Id )
  3412. {
  3413. // Replace all instances of remote_Id in the local document with local_Id
  3414. var elems = remote_mainDoc.Descendants( XName.Get( localName, namespaceName ) );
  3415. foreach( var elem in elems )
  3416. {
  3417. var attribute = elem.Attribute( XName.Get( localNameAttribute, DocX.r.NamespaceName ) );
  3418. if( attribute != null && attribute.Value == remote_Id )
  3419. {
  3420. attribute.SetValue( new_Id );
  3421. }
  3422. }
  3423. }
  3424. private string ComputeMD5HashString( Stream stream )
  3425. {
  3426. MD5 md5 = MD5.Create();
  3427. byte[] hash = md5.ComputeHash( stream );
  3428. StringBuilder sb = new StringBuilder();
  3429. foreach( byte b in hash )
  3430. sb.Append( b.ToString( "X2" ) );
  3431. return sb.ToString();
  3432. }
  3433. private void merge_endnotes( PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote, XDocument remote_endnotes )
  3434. {
  3435. IEnumerable<int> ids =
  3436. (
  3437. from d in _endnotes.Root.Descendants()
  3438. where d.Name.LocalName == "endnote"
  3439. select int.Parse( d.Attribute( XName.Get( "id", w.NamespaceName ) ).Value )
  3440. );
  3441. int max_id = ids.Max() + 1;
  3442. var endnoteReferences = remote_mainDoc.Descendants( XName.Get( "endnoteReference", w.NamespaceName ) );
  3443. foreach( var endnote in remote_endnotes.Root.Elements().OrderBy( fr => fr.Attribute( XName.Get( "id", r.NamespaceName ) ) ).Reverse() )
  3444. {
  3445. XAttribute id = endnote.Attribute( XName.Get( "id", w.NamespaceName ) );
  3446. int i;
  3447. if( id != null && int.TryParse( id.Value, out i ) )
  3448. {
  3449. if( i > 0 )
  3450. {
  3451. foreach( var endnoteRef in endnoteReferences )
  3452. {
  3453. XAttribute a = endnoteRef.Attribute( XName.Get( "id", w.NamespaceName ) );
  3454. if( a != null && int.Parse( a.Value ).Equals( i ) )
  3455. {
  3456. a.SetValue( max_id );
  3457. }
  3458. }
  3459. // We care about copying this footnote.
  3460. endnote.SetAttributeValue( XName.Get( "id", w.NamespaceName ), max_id );
  3461. _endnotes.Root.Add( endnote );
  3462. max_id++;
  3463. }
  3464. }
  3465. }
  3466. }
  3467. private void merge_footnotes( PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote, XDocument remote_footnotes )
  3468. {
  3469. IEnumerable<int> ids =
  3470. (
  3471. from d in _footnotes.Root.Descendants()
  3472. where d.Name.LocalName == "footnote"
  3473. select int.Parse( d.Attribute( XName.Get( "id", DocX.w.NamespaceName ) ).Value )
  3474. );
  3475. int max_id = ids.Max() + 1;
  3476. var footnoteReferences = remote_mainDoc.Descendants( XName.Get( "footnoteReference", DocX.w.NamespaceName ) );
  3477. foreach( var footnote in remote_footnotes.Root.Elements().OrderBy( fr => fr.Attribute( XName.Get( "id", DocX.r.NamespaceName ) ) ).Reverse() )
  3478. {
  3479. XAttribute id = footnote.Attribute( XName.Get( "id", DocX.w.NamespaceName ) );
  3480. int i;
  3481. if( id != null && int.TryParse( id.Value, out i ) )
  3482. {
  3483. if( i > 0 )
  3484. {
  3485. foreach( var footnoteRef in footnoteReferences )
  3486. {
  3487. XAttribute a = footnoteRef.Attribute( XName.Get( "id", DocX.w.NamespaceName ) );
  3488. if( a != null && int.Parse( a.Value ).Equals( i ) )
  3489. {
  3490. a.SetValue( max_id );
  3491. }
  3492. }
  3493. // We care about copying this footnote.
  3494. footnote.SetAttributeValue( XName.Get( "id", DocX.w.NamespaceName ), max_id );
  3495. _footnotes.Root.Add( footnote );
  3496. max_id++;
  3497. }
  3498. }
  3499. }
  3500. }
  3501. private void merge_customs( PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc )
  3502. {
  3503. // Get the remote documents custom.xml file.
  3504. XDocument remote_custom_document;
  3505. using( TextReader tr = new StreamReader( remote_pp.GetStream() ) )
  3506. {
  3507. remote_custom_document = XDocument.Load( tr );
  3508. }
  3509. // Get the local documents custom.xml file.
  3510. XDocument local_custom_document;
  3511. using( TextReader tr = new StreamReader( local_pp.GetStream() ) )
  3512. {
  3513. local_custom_document = XDocument.Load( tr );
  3514. }
  3515. IEnumerable<int> pids =
  3516. (
  3517. from d in remote_custom_document.Root.Descendants()
  3518. where d.Name.LocalName == "property"
  3519. select int.Parse( d.Attribute( XName.Get( "pid" ) ).Value )
  3520. );
  3521. int pid = pids.Max() + 1;
  3522. foreach( XElement remote_property in remote_custom_document.Root.Elements() )
  3523. {
  3524. bool found = false;
  3525. foreach( XElement local_property in local_custom_document.Root.Elements() )
  3526. {
  3527. var remote_property_name = remote_property.Attribute( XName.Get( "name" ) );
  3528. var local_property_name = local_property.Attribute( XName.Get( "name" ) );
  3529. if( remote_property != null && local_property_name != null && remote_property_name.Value.Equals( local_property_name.Value ) )
  3530. {
  3531. found = true;
  3532. }
  3533. }
  3534. if( !found )
  3535. {
  3536. remote_property.SetAttributeValue( XName.Get( "pid" ), pid );
  3537. local_custom_document.Root.Add( remote_property );
  3538. pid++;
  3539. }
  3540. }
  3541. // Save the modified local custom styles.xml file.
  3542. using( TextWriter tw = new StreamWriter( new PackagePartStream( local_pp.GetStream( FileMode.Create, FileAccess.Write ) ) ) )
  3543. {
  3544. local_custom_document.Save( tw, SaveOptions.None );
  3545. }
  3546. }
  3547. private void merge_numbering( PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote )
  3548. {
  3549. // Add each remote numbering to this document.
  3550. var remote_abstractNums = remote._numbering.Root.Elements( XName.Get( "abstractNum", w.NamespaceName ) );
  3551. int guidd = 0;
  3552. foreach( var an in remote_abstractNums )
  3553. {
  3554. var a = an.Attribute( XName.Get( "abstractNumId", w.NamespaceName ) );
  3555. if( a != null )
  3556. {
  3557. int i;
  3558. if( int.TryParse( a.Value, out i ) )
  3559. {
  3560. if( i > guidd )
  3561. {
  3562. guidd = i;
  3563. }
  3564. }
  3565. }
  3566. }
  3567. guidd++;
  3568. var remote_nums = remote._numbering.Root.Elements( XName.Get( "num", w.NamespaceName ) );
  3569. int guidd2 = 0;
  3570. foreach( var an in remote_nums )
  3571. {
  3572. var a = an.Attribute( XName.Get( "numId", w.NamespaceName ) );
  3573. if( a != null )
  3574. {
  3575. int i;
  3576. if( int.TryParse( a.Value, out i ) )
  3577. {
  3578. if( i > guidd2 )
  3579. {
  3580. guidd2 = i;
  3581. }
  3582. }
  3583. }
  3584. }
  3585. guidd2++;
  3586. foreach( XElement remote_abstractNum in remote_abstractNums )
  3587. {
  3588. var abstractNumId = remote_abstractNum.Attribute( XName.Get( "abstractNumId", w.NamespaceName ) );
  3589. if( abstractNumId != null )
  3590. {
  3591. var abstractNumIdValue = abstractNumId.Value;
  3592. abstractNumId.SetValue( guidd );
  3593. foreach( XElement remote_num in remote_nums )
  3594. {
  3595. var numIds = remote_mainDoc.Descendants( XName.Get( "numId", w.NamespaceName ) );
  3596. foreach( var numId in numIds )
  3597. {
  3598. var attr = numId.Attribute( XName.Get( "val", w.NamespaceName ) );
  3599. if( attr != null && attr.Value.Equals( remote_num.Attribute( XName.Get( "numId", w.NamespaceName ) ).Value ) )
  3600. {
  3601. attr.SetValue( guidd2 );
  3602. }
  3603. }
  3604. remote_num.SetAttributeValue( XName.Get( "numId", w.NamespaceName ), guidd2 );
  3605. var e = remote_num.Element( XName.Get( "abstractNumId", w.NamespaceName ) );
  3606. var a2 = e.Attribute( XName.Get( "val", w.NamespaceName ) );
  3607. if ( a2 != null && a2.Value.Equals( abstractNumIdValue ) )
  3608. a2.SetValue( guidd );
  3609. guidd2++;
  3610. }
  3611. }
  3612. guidd++;
  3613. }
  3614. var abstractNumElement = _numbering.Root.Elements( XName.Get( "abstractNum", w.NamespaceName ) );
  3615. if( ( abstractNumElement != null ) && ( abstractNumElement.Count() > 0 ) )
  3616. {
  3617. abstractNumElement.Last().AddAfterSelf( remote_abstractNums );
  3618. }
  3619. var numElement = _numbering.Root.Elements( XName.Get( "num", w.NamespaceName ) );
  3620. if( ( numElement != null ) && ( numElement.Count() > 0 ) )
  3621. {
  3622. numElement.Last().AddAfterSelf( remote_nums );
  3623. }
  3624. }
  3625. private void merge_fonts( PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote )
  3626. {
  3627. // Add each remote font to this document.
  3628. IEnumerable<XElement> remote_fonts = remote._fontTable.Root.Elements( XName.Get( "font", DocX.w.NamespaceName ) );
  3629. IEnumerable<XElement> local_fonts = _fontTable.Root.Elements( XName.Get( "font", DocX.w.NamespaceName ) );
  3630. foreach( XElement remote_font in remote_fonts )
  3631. {
  3632. bool flag_addFont = true;
  3633. foreach( XElement local_font in local_fonts )
  3634. {
  3635. if( local_font.Attribute( XName.Get( "name", DocX.w.NamespaceName ) ).Value == remote_font.Attribute( XName.Get( "name", DocX.w.NamespaceName ) ).Value )
  3636. {
  3637. flag_addFont = false;
  3638. break;
  3639. }
  3640. }
  3641. if( flag_addFont )
  3642. {
  3643. _fontTable.Root.Add( remote_font );
  3644. }
  3645. }
  3646. }
  3647. private void merge_styles( PackagePart remote_pp, PackagePart local_pp, XDocument remote_mainDoc, DocX remote, XDocument remote_footnotes, XDocument remote_endnotes )
  3648. {
  3649. var local_styles = new Dictionary<string, string>();
  3650. foreach( XElement local_style in _styles.Root.Elements( XName.Get( "style", w.NamespaceName ) ) )
  3651. {
  3652. var temp = new XElement( local_style );
  3653. var styleId = temp.Attribute( XName.Get( "styleId", w.NamespaceName ) );
  3654. var value = styleId.Value;
  3655. styleId.Remove();
  3656. var key = Regex.Replace( temp.ToString(), @"\s+", "" );
  3657. if( !local_styles.ContainsKey( key ) )
  3658. {
  3659. local_styles.Add( key, value );
  3660. }
  3661. }
  3662. // Add each remote style to this document.
  3663. var remote_styles = remote._styles.Root.Elements( XName.Get( "style", w.NamespaceName ) );
  3664. foreach( XElement remote_style in remote_styles )
  3665. {
  3666. var temp = new XElement( remote_style );
  3667. var styleId = temp.Attribute( XName.Get( "styleId", w.NamespaceName ) );
  3668. var value = styleId.Value;
  3669. styleId.Remove();
  3670. var key = Regex.Replace( temp.ToString(), @"\s+", "" );
  3671. String guuid;
  3672. // Check to see if the local document already contains the remote style.
  3673. if( local_styles.ContainsKey( key ) )
  3674. {
  3675. String local_value;
  3676. local_styles.TryGetValue( key, out local_value );
  3677. // If the styleIds are the same then nothing needs to be done.
  3678. if( local_value == value )
  3679. continue;
  3680. // All we need to do is update the styleId.
  3681. guuid = local_value;
  3682. }
  3683. else
  3684. {
  3685. guuid = Guid.NewGuid().ToString();
  3686. // Set the styleId in the remote_style to this new Guid
  3687. remote_style.SetAttributeValue( XName.Get( "styleId", w.NamespaceName ), guuid );
  3688. }
  3689. foreach( XElement e in remote_mainDoc.Root.Descendants( XName.Get( "pStyle", w.NamespaceName ) ) )
  3690. {
  3691. var e_styleId = e.Attribute( XName.Get( "val", w.NamespaceName ) );
  3692. if( ( e_styleId != null ) && e_styleId.Value.Equals( styleId.Value ) )
  3693. {
  3694. e_styleId.SetValue( guuid );
  3695. }
  3696. }
  3697. foreach( XElement e in remote_mainDoc.Root.Descendants( XName.Get( "rStyle", w.NamespaceName ) ) )
  3698. {
  3699. var e_styleId = e.Attribute( XName.Get( "val", w.NamespaceName ) );
  3700. if( ( e_styleId != null ) && e_styleId.Value.Equals( styleId.Value ) )
  3701. {
  3702. e_styleId.SetValue( guuid );
  3703. }
  3704. }
  3705. foreach( XElement e in remote_mainDoc.Root.Descendants( XName.Get( "tblStyle", w.NamespaceName ) ) )
  3706. {
  3707. var e_styleId = e.Attribute( XName.Get( "val", w.NamespaceName ) );
  3708. if( ( e_styleId != null ) && e_styleId.Value.Equals( styleId.Value ) )
  3709. {
  3710. e_styleId.SetValue( guuid );
  3711. }
  3712. }
  3713. if( remote_endnotes != null )
  3714. {
  3715. foreach( XElement e in remote_endnotes.Root.Descendants( XName.Get( "rStyle", w.NamespaceName ) ) )
  3716. {
  3717. var e_styleId = e.Attribute( XName.Get( "val", w.NamespaceName ) );
  3718. if( ( e_styleId != null ) && e_styleId.Value.Equals( styleId.Value ) )
  3719. {
  3720. e_styleId.SetValue( guuid );
  3721. }
  3722. }
  3723. foreach( XElement e in remote_endnotes.Root.Descendants( XName.Get( "pStyle", w.NamespaceName ) ) )
  3724. {
  3725. var e_styleId = e.Attribute( XName.Get( "val", w.NamespaceName ) );
  3726. if( ( e_styleId != null ) && e_styleId.Value.Equals( styleId.Value ) )
  3727. {
  3728. e_styleId.SetValue( guuid );
  3729. }
  3730. }
  3731. }
  3732. if( remote_footnotes != null )
  3733. {
  3734. foreach( XElement e in remote_footnotes.Root.Descendants( XName.Get( "rStyle", w.NamespaceName ) ) )
  3735. {
  3736. var e_styleId = e.Attribute( XName.Get( "val", w.NamespaceName ) );
  3737. if( ( e_styleId != null ) && e_styleId.Value.Equals( styleId.Value ) )
  3738. {
  3739. e_styleId.SetValue( guuid );
  3740. }
  3741. }
  3742. foreach( XElement e in remote_footnotes.Root.Descendants( XName.Get( "pStyle", w.NamespaceName ) ) )
  3743. {
  3744. var e_styleId = e.Attribute( XName.Get( "val", w.NamespaceName ) );
  3745. if( ( e_styleId != null ) && e_styleId.Value.Equals( styleId.Value ) )
  3746. {
  3747. e_styleId.SetValue( guuid );
  3748. }
  3749. }
  3750. }
  3751. // Make sure they don't clash by using a uuid.
  3752. styleId.SetValue( guuid );
  3753. _styles.Root.Add( remote_style );
  3754. }
  3755. }
  3756. protected void clonePackageRelationship( DocX remote_document, PackagePart pp, XDocument remote_mainDoc )
  3757. {
  3758. var url = pp.Uri.OriginalString.Replace( "/", "" );
  3759. var remote_rels = remote_document.PackagePart.GetRelationships();
  3760. foreach( var remote_rel in remote_rels )
  3761. {
  3762. if( url.Equals( "word" + remote_rel.TargetUri.OriginalString.Replace( "/", "" ) ) )
  3763. {
  3764. var remote_Id = remote_rel.Id;
  3765. var local_Id = this.PackagePart.CreateRelationship( remote_rel.TargetUri, remote_rel.TargetMode, remote_rel.RelationshipType ).Id;
  3766. // Replace all instances of remote_Id in the local document with local_Id
  3767. this.ReplaceAllRemoteID( remote_mainDoc, "blip", "embed", a.NamespaceName, remote_Id, local_Id );
  3768. // Replace all instances of remote_Id in the local document with local_Id (for shapes)
  3769. this.ReplaceAllRemoteID( remote_mainDoc, "imagedata", "id", v.NamespaceName, remote_Id, local_Id );
  3770. break;
  3771. }
  3772. }
  3773. }
  3774. protected PackagePart clonePackagePart( PackagePart pp )
  3775. {
  3776. var new_pp = _package.CreatePart( pp.Uri, pp.ContentType, CompressionOption.Normal );
  3777. using( Stream s_read = pp.GetStream() )
  3778. {
  3779. using( Stream s_write = new PackagePartStream( new_pp.GetStream( FileMode.Create ) ) )
  3780. {
  3781. var buffer = new byte[ 32768 ];
  3782. int read;
  3783. while( ( read = s_read.Read( buffer, 0, buffer.Length ) ) > 0 )
  3784. {
  3785. s_write.Write( buffer, 0, read );
  3786. }
  3787. }
  3788. }
  3789. return new_pp;
  3790. }
  3791. protected string GetMD5HashFromStream( Stream stream )
  3792. {
  3793. MD5 md5 = new MD5CryptoServiceProvider();
  3794. byte[] retVal = md5.ComputeHash( stream );
  3795. StringBuilder sb = new StringBuilder();
  3796. for( int i = 0; i < retVal.Length; i++ )
  3797. {
  3798. sb.Append( retVal[ i ].ToString( "x2" ) );
  3799. }
  3800. return sb.ToString();
  3801. }
  3802. private static void PopulateDocument( DocX document, Package package )
  3803. {
  3804. var headers = new Headers();
  3805. headers.Odd = document.GetHeaderByType( "default" );
  3806. headers.Even = document.GetHeaderByType( "even" );
  3807. headers.First = document.GetHeaderByType( "first" );
  3808. var footers = new Footers();
  3809. footers.Odd = document.GetFooterByType( "default" );
  3810. footers.Even = document.GetFooterByType( "even" );
  3811. footers.First = document.GetFooterByType( "first" );
  3812. //// Get the sectPr for this document.
  3813. //XElement sectPr = document.mainDoc.Descendants(XName.Get("sectPr", DocX.w.NamespaceName)).Single();
  3814. //if (sectPr != null)
  3815. //{
  3816. // // Extract the even header reference
  3817. // var header_even_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "headerReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "even");
  3818. // string id = header_even_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
  3819. // var res = document.mainPart.GetRelationship(id);
  3820. // string ans = res.SourceUri.OriginalString;
  3821. // headers.even.xml_filename = ans;
  3822. // // Extract the odd header reference
  3823. // var header_odd_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "headerReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "default");
  3824. // string id2 = header_odd_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
  3825. // var res2 = document.mainPart.GetRelationship(id2);
  3826. // string ans2 = res2.SourceUri.OriginalString;
  3827. // headers.odd.xml_filename = ans2;
  3828. // // Extract the first header reference
  3829. // var header_first_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "h
  3830. //eaderReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "first");
  3831. // string id3 = header_first_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
  3832. // var res3 = document.mainPart.GetRelationship(id3);
  3833. // string ans3 = res3.SourceUri.OriginalString;
  3834. // headers.first.xml_filename = ans3;
  3835. // // Extract the even footer reference
  3836. // var footer_even_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "footerReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "even");
  3837. // string id4 = footer_even_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
  3838. // var res4 = document.mainPart.GetRelationship(id4);
  3839. // string ans4 = res4.SourceUri.OriginalString;
  3840. // footers.even.xml_filename = ans4;
  3841. // // Extract the odd footer reference
  3842. // var footer_odd_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "footerReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "default");
  3843. // string id5 = footer_odd_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
  3844. // var res5 = document.mainPart.GetRelationship(id5);
  3845. // string ans5 = res5.SourceUri.OriginalString;
  3846. // footers.odd.xml_filename = ans5;
  3847. // // Extract the first footer reference
  3848. // var footer_first_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "footerReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "first");
  3849. // string id6 = footer_first_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
  3850. // var res6 = document.mainPart.GetRelationship(id6);
  3851. // string ans6 = res6.SourceUri.OriginalString;
  3852. // footers.first.xml_filename = ans6;
  3853. //}
  3854. document.Xml = document._mainDoc.Root.Element( w + "body" );
  3855. document._headers = headers;
  3856. document._footers = footers;
  3857. document._settingsPart = HelperFunctions.CreateOrGetSettingsPart( package );
  3858. var ps = package.GetParts();
  3859. //document.endnotesPart = HelperFunctions.GetPart();
  3860. foreach( var rel in document.PackagePart.GetRelationships() )
  3861. {
  3862. var uriString = "/word/" + rel.TargetUri.OriginalString.Replace( "/word/", "" ).Replace( "file://", "" );
  3863. switch( rel.RelationshipType )
  3864. {
  3865. case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes":
  3866. document._endnotesPart = package.GetPart( new Uri( uriString, UriKind.Relative ) );
  3867. using( TextReader tr = new StreamReader( document._endnotesPart.GetStream() ) )
  3868. document._endnotes = XDocument.Load( tr );
  3869. break;
  3870. case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes":
  3871. document._footnotesPart = package.GetPart( new Uri( uriString, UriKind.Relative ) );
  3872. using( TextReader tr = new StreamReader( document._footnotesPart.GetStream() ) )
  3873. document._footnotes = XDocument.Load( tr );
  3874. break;
  3875. case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles":
  3876. document._stylesPart = package.GetPart( new Uri( uriString, UriKind.Relative ) );
  3877. using( TextReader tr = new StreamReader( document._stylesPart.GetStream() ) )
  3878. document._styles = XDocument.Load( tr );
  3879. break;
  3880. case "http://schemas.microsoft.com/office/2007/relationships/stylesWithEffects":
  3881. document._stylesWithEffectsPart = package.GetPart( new Uri( uriString, UriKind.Relative ) );
  3882. using( TextReader tr = new StreamReader( document._stylesWithEffectsPart.GetStream() ) )
  3883. document._stylesWithEffects = XDocument.Load( tr );
  3884. break;
  3885. case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable":
  3886. document._fontTablePart = package.GetPart( new Uri( uriString, UriKind.Relative ) );
  3887. using( TextReader tr = new StreamReader( document._fontTablePart.GetStream() ) )
  3888. document._fontTable = XDocument.Load( tr );
  3889. break;
  3890. case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering":
  3891. document._numberingPart = package.GetPart( new Uri( uriString, UriKind.Relative ) );
  3892. using( TextReader tr = new StreamReader( document._numberingPart.GetStream() ) )
  3893. document._numbering = XDocument.Load( tr );
  3894. break;
  3895. default:
  3896. break;
  3897. }
  3898. }
  3899. }
  3900. private string GetNextFreeRelationshipID()
  3901. {
  3902. int id =
  3903. (
  3904. from r in this.PackagePart.GetRelationships()
  3905. where r.Id.Substring( 0, 3 ).Equals( "rId" )
  3906. select int.Parse( r.Id.Substring( 3 ) )
  3907. ).DefaultIfEmpty().Max();
  3908. // The convension for ids is rid01, rid02, etc
  3909. var newId = id.ToString();
  3910. int result;
  3911. if( int.TryParse( newId, out result ) )
  3912. return ( "rId" + ( result + 1 ) );
  3913. var guid = String.Empty;
  3914. do
  3915. {
  3916. guid = Guid.NewGuid().ToString();
  3917. } while( Char.IsDigit( guid[ 0 ] ) );
  3918. return guid;
  3919. }
  3920. private byte[] MergeArrays( byte[] array1, byte[] array2 )
  3921. {
  3922. byte[] result = new byte[ array1.Length + array2.Length ];
  3923. Buffer.BlockCopy( array2, 0, result, 0, array2.Length );
  3924. Buffer.BlockCopy( array1, 0, result, array2.Length, array1.Length );
  3925. return result;
  3926. }
  3927. #endregion
  3928. #region Constructors
  3929. internal DocX( DocX document, XElement xml )
  3930. : base( document, xml )
  3931. {
  3932. }
  3933. #endregion
  3934. #region IDisposable Members
  3935. /// <summary>
  3936. /// Releases all resources used by this document.
  3937. /// </summary>
  3938. /// <example>
  3939. /// If you take advantage of the using keyword, Dispose() is automatically called for you.
  3940. /// <code>
  3941. /// // Load document.
  3942. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  3943. /// {
  3944. /// // The document is only in memory while in this scope.
  3945. ///
  3946. /// }// Dispose() is automatically called at this point.
  3947. /// </code>
  3948. /// </example>
  3949. /// <example>
  3950. /// This example is equilivant to the one above example.
  3951. /// <code>
  3952. /// // Load document.
  3953. /// DocX document = DocX.Load(@"C:\Example\Test.docx");
  3954. ///
  3955. /// // Do something with the document here.
  3956. ///
  3957. /// // Dispose of the document.
  3958. /// document.Dispose();
  3959. /// </code>
  3960. /// </example>
  3961. public void Dispose()
  3962. {
  3963. _package.Close();
  3964. }
  3965. #endregion
  3966. }
  3967. }