About Me

My photo
Ireland
Hello, my name is Cathal Coffey. I am best described as a hybrid between a developer and an adventurer. When I am not behind a keyboard coding, I am hiking and climbing the beautiful mountains of my home country Ireland. I am a full time student studying Computer Science & Software Engineering at the National University of Ireland Maynooth. I am finishing the final year of a 4 year degree in September 2009. I am the creator of an open source project on codeplex.com called DocX. At the moment I spend a lot of my free time advancing DocX and I enjoy this very much. My aim is to build a community around DocX and add features based on requests from this community. I really enjoy hearing about how people are using DocX in their work\personal projects. So if you are one of these people, please send me an email. Cathal coffey.cathal@gmail.com

Monday, June 14, 2010

Cathal: Why did you create DocX?

Before I created the OpenSource library “DocX”, there were two methods by which a .docx document could be generated programmatically. This blog post demonstrates each method in turn by generating a simple HelloWorld document. 

After experiencing both methods for document creation I hope you will agree that DocX is a interesting alternative. DocX was designed with simplicity in mind. It dose not require the user to have any knowledge of how .docx documents are stored or organised internally.

Method 1: HelloWorld using Office Interop

Make sure that you have added a reference to

1) Microsoft.Office.Interop.Word (version 12.0.0.0)

image

OfficeInterop HelloWorld
  1. using System;
  2. using Microsoft.Office.Interop.Word;
  3. using System.Reflection;
  4. using System.IO;
  5.  
  6. namespace OfficeInterop
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             // Get the path of the executing assembly.
  13.             string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  14.  
  15.             // The location to save the file.
  16.             Object oSaveAsFile = (Object)(path + @"\Test.docx");
  17.  
  18.             // Object of Missing "Null Value".
  19.             Object oMissing = System.Reflection.Missing.Value;
  20.  
  21.             // Object of false.
  22.             Object oFalse = false;
  23.  
  24.             // Create object of Word and Document.
  25.             Application oWord = new Application();
  26.             Document oWordDoc = new Document();
  27.  
  28.             // Don't display Word.
  29.             oWord.Visible = false;
  30.  
  31.             // Add a new document.
  32.             oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  33.             
  34.             // Enter some text with the font Arial Black.
  35.             oWord.Selection.Font.Name = "Arial Black";
  36.             oWord.Selection.TypeText("Hello World");
  37.  
  38.             // Save the document.
  39.             oWordDoc.SaveAs
  40.             (
  41.                 ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
  42.                 ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing,
  43.                 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  44.                 ref oMissing, ref oMissing
  45.             );
  46.  
  47.  
  48.             // Close the file.
  49.             oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);
  50.              
  51.             // Quit Word.exe
  52.             oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
  53.         }
  54.     }
  55. }

 

Method 2: HelloWorld using the OOXML SDK

Make sure that you have added a reference to both

1) DocumentFormat.OpenXml (version 2.0.5022.0)
2) WindowsBase (version 3.0.0.0)

image

OOXML HelloWorld
  1. using DocumentFormat.OpenXml.Packaging;
  2. using DocumentFormat.OpenXml.Wordprocessing;
  3. using DocumentFormat.OpenXml;
  4.  
  5. namespace OOXML
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // Create a package.
  12.             WordprocessingDocument doc =
  13.             WordprocessingDocument.Create
  14.             (
  15.                 @"Test.docx",
  16.                 WordprocessingDocumentType.Document
  17.             );
  18.  
  19.             // Create a main document part and add it to the package.
  20.             MainDocumentPart mainPart = doc.AddMainDocumentPart();
  21.             mainPart.Document = new Document();
  22.             
  23.             // Create some content.
  24.             Text textFirstLine = new Text("Hello World");
  25.             Run run = new Run(textFirstLine);
  26.             Paragraph para = new Paragraph(run);
  27.            
  28.             // Apply a font.
  29.             RunProperties runProp = new RunProperties();
  30.             RunFonts runFont = new RunFonts();
  31.             runFont.Ascii = "Arial Black";
  32.             runProp.Append(runFont);
  33.             run.PrependChild<RunProperties>(runProp);
  34.             // Add the content into the document.
  35.             Body body = new Body(para);
  36.             mainPart.Document.Append(body);
  37.             
  38.             /* Save and close */
  39.             mainPart.Document.Save();
  40.             doc.Close();
  41.         }
  42.     }
  43. }

HelloWorld using DocX

Make sure that you have added a reference to

1) DocX (version 1.0.0.9)
2) System.Drawing (version 2.0.0.0)

image

DocX HelloWorld
  1. using System;
  2. using Novacode;
  3. using System.Drawing;
  4.  
  5. namespace DocXHelloWorld
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             using (DocX document = DocX.Create("Test.docx"))
  12.             {
  13.                 // Add a new Paragraph to the document.
  14.                 Paragraph p = document.InsertParagraph();
  15.                 
  16.                 // Append some text.
  17.                 p.Append("Hello World").Font(new FontFamily("Arial Black"));
  18.  
  19.                 // Save the document.
  20.                 document.Save();
  21.             }
  22.         }
  23.     }
  24. }

34 comments:

  1. I need help here!
    I need to open a word file stored on the server using asp.net but using VB and not C#.
    I tried to use the code for editing the file but it gives and error saying
    "Novacode.docX.Private Sub New(document as Novacode.DocX, xml As System.Xml.Linq.XElement)' is not accessible in this context because ot os 'Private'.
    Please help...

    ReplyDelete
  2. Mandy, you may want to try converting the c# code above to VB using an online convertor, such as http://www.developerfusion.com/tools/convert/csharp-to-vb/

    ReplyDelete
  3. how can i save word as pdf using docx?

    ReplyDelete
  4. Cathal Coffey , Thank you for your blog , it is helpful

    ReplyDelete
  5. is there a way to integrate an audio file into an existing document using DocX?

    ReplyDelete
  6. hi, I can edit in the browser a file created with docX.dll, summing up ... I'm trying to edit the file in an asp.net page. thanks ... carlitolima@hotmail.com, i am from Brasil..

    ReplyDelete
  7. I appreciate this, but as long as there is no Style support, what's the use? Anyone who works with Word (and document production in general) surely would only ever want to work with Styles rather than setting fonts and such manually?

    ReplyDelete
  8. Hi, I am using vb in my asp.net project. I have 2 problems
    1. Where should I place the word file when I am using DocX.load?
    2. I can't create paragraph, Visual Studio say "Paragraph is ambiguous". What can I do?

    Thanks for your support

    ReplyDelete
  9. Hola estoy muy interesado en esta librería y quisiera saber si existe algún manual en español sobre el mismo.

    ReplyDelete
  10. Thank you for creating this library.

    ReplyDelete
  11. Hello, Cathal! I just want you to know that your powerful and easy library just made me look like a rock star at work. If I'm ever in Ireland, I owe you a drink!

    -Jeremy

    ReplyDelete
  12. Hello, Cathal! I just want you to know that your powerful and easy library just made me look like a rock star at work. If I'm ever in Ireland, I owe you a drink!

    -Jeremy

    ReplyDelete
  13. Hi Cathal,

    Does the library support cloning of pages or mail merge support.

    I like the replacetext function but what if I want to clone a page (in a loop) is that possible?

    ReplyDelete
  14. Hi Cathal,

    How I would look in DocX?
    //TablesOfContents UPDATE in Microsoft.Office.Interop.Word
    foreach (Word.TableOfContents T in wordDoc.TablesOfContents)
    {
    T.Update();
    }

    Tanks

    ReplyDelete
  15. I have been trying to use DocX to overthrow the government of Uruguay but it's not working. Is it because I am using VB?

    ReplyDelete
  16. Yhym... Trying to add NEW core properties to NEW docx file is impossible from the early begining of DocX.... Awsome piece of software ... Truly ..

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Dear Cathal, first of it is a wonderful effort and initiative by you, really appreciate it.

    The problem I am facing is that I want to get text of a .doc file (not .docx), but I have not succeeded yet and I had to use office interop libraries to process .doc and Docx for .docx respectively which I do not want to use. Please let me know how can I get all the text of a .doc file using DocX. Any help much appreciated.
    Regards,
    Farhan.

    ReplyDelete
  19. Hi Cathal, I have applied the DocX in .Net core 2.0. The process does create the document, but there is no content added in the document. Can you help me on this one?

    Thanks & Regards,
    Idhris

    ReplyDelete
  20. Hi, Is there a way to password protect a document using Docx? I am not referring to edit restriction but password protection. i.e when trying to open the file it should prompt for the password.

    ReplyDelete
  21. We produce all kind of novelty documents and we guarantee you a New Identity, we produce novelty documents like (passport,Buy Novelty Documents "blogspot", driver’s license, I.D, Birth certificate, diplomas) starting from a clean new
    Genuine Birth Certificate, IELTS CERTIFICATE, ID card, Drivers License, , Novelty Drivers License, Genuine Passports, Novelty Passport, Social security card with SSN, school diplomas, school degrees, UK Bank Statements,Ireland Bank Statements,UK Utility Bills,Ireland Utility Bills and Bank Statements all in an entirely NEW NAME issued and registered in the government database system so you can travel with no problem across security borders even when the authorities checked them you documents will be report legit with fresh
    number . We produce documents principally in two formats, that’s Genuine and Novelty Formats. We offer only original high-quality novelty passports, driver’s licenses, ID cards, Buy Novelty Documents "blogspot", stamps and other products for following countries: Australia, Belgium, Brazil, Canada, Finland, France, Germany, Ireland, Italy, Netherlands, Norway, Spain,
    Sweden, Switzerland, UK, USA ETC. contact us via contact us via
    Email: info@noveltydocumentationexpress.com
    Whatsapp:+44(744) 849-0535

    ReplyDelete
  22. Producem permisul de conducere real și fals. Pentru permisul de conducere real, înregistrăm toate informațiile în sistemul de baze de date și dacă licența de șofer este verificată folosind o mașină de citire a datelor, toate informațiile dvs. vor apărea în sistem și veți utiliza legal documentul. De asemenea, producem permisul de conducere, care sunt la fel cu permisul de conducere. Dar nicio informație din document nu va fi înregistrată în sistemul de baze de date. Deci, documentul va fi fals. Dar toate caracteristicile secrete ale permisului de conducere Real vor fi duplicate și imprimate pe copia Fake. Prin urmare, ne sfătuim întotdeauna clienții să ne permită să le producem documentele reale dacă doresc legal să folosească documentul. Contactați-ne acum
    cumpăra permis de conducere
    cumpăra permisul de conducere din SUA
    cumpăra permis de conducere CANADIAN
    cumpăra permis de conducere ASIAN
    cumpăra permis de conducere UE
    cumpăra permis de conducere polonez
    cumpăra permis de conducere german
    cumpărați pașaport real american
    Cumpără pașaport real CANADIAN
    cumpără pașaport real GERMAN
    Cumpărați pașaportul din Marea Britanie
    Cumpără pașaport real RUSSIAN
    cumpărați un pașaport real TURKISH
    cumpărați pașaport real SPANISH
    cumpărați un pașaport real francez
    cumpărați pașaport real DUTCH
    Cumpără pașaport real ITALIAN
    Cumpără cartea de identitate din SUA
    cumpără cartea de identitate CANADIAN
    Cumpără cartea de identitate ITALIANĂ
    cumpără cartea de identitate GERMAN
    cumpărați o carte de identitate poloneză
    cumpără cartea de identitate TURKISH
    cumpără cartea de identitate SPANISH
    cumpăra certificat de naștere online
    cumpărați SSN
    cumpărați o diplomă
    cumpără IELTS
    cumpărați Visa online
    cumpărați cartea verde din SUA online

    ReplyDelete
  23. Producem permisul de conducere real și fals. Pentru permisul de conducere real, înregistrăm toate informațiile în sistemul de baze de date și dacă licența de șofer este verificată folosind o mașină de citire a datelor, toate informațiile dvs. vor apărea în sistem și veți utiliza legal documentul. De asemenea, producem permisul de conducere, care sunt la fel cu permisul de conducere. Dar nicio informație din document nu va fi înregistrată în sistemul de baze de date. Deci, documentul va fi fals. Dar toate caracteristicile secrete ale permisului de conducere Real vor fi duplicate și imprimate pe copia Fake. Prin urmare, ne sfătuim întotdeauna clienții să ne permită să le producem documentele reale dacă doresc legal să folosească documentul. Contactați-ne acum
    cumpăra permis de conducere
    cumpăra permisul de conducere din SUA
    cumpăra permis de conducere CANADIAN
    cumpăra permis de conducere ASIAN
    cumpăra permis de conducere UE
    cumpăra permis de conducere polonez
    cumpăra permis de conducere german
    cumpărați pașaport real american
    Cumpără pașaport real CANADIAN
    cumpără pașaport real GERMAN
    Cumpărați pașaportul din Marea Britanie
    Cumpără pașaport real RUSSIAN
    cumpărați un pașaport real TURKISH
    cumpărați pașaport real SPANISH
    cumpărați un pașaport real francez
    cumpărați pașaport real DUTCH
    Cumpără pașaport real ITALIAN
    Cumpără cartea de identitate din SUA
    cumpără cartea de identitate CANADIAN
    Cumpără cartea de identitate ITALIANĂ
    cumpără cartea de identitate GERMAN
    cumpărați o carte de identitate poloneză
    cumpără cartea de identitate TURKISH
    cumpără cartea de identitate SPANISH
    cumpăra certificat de naștere online
    cumpărați SSN
    cumpărați o diplomă
    cumpără IELTS
    cumpărați Visa online
    cumpărați cartea verde din SUA online

    ReplyDelete
  24. Wir produzieren sowohl echte als auch gefälschte Führerscheine. Für den Real Driver’s License registrieren wir alle Informationen im Datenbanksystem. Wenn der Führerschein mit einem Datenlesegerät überprüft wird, werden alle Ihre Informationen im System angezeigt und Sie müssen das Dokument legal verwenden. Wir produzieren auch einen Führerschein, der mit dem Führerschein identisch ist. Es werden jedoch keine Informationen auf dem Dokument im Datenbanksystem registriert. Das Dokument wird also gefälscht sein. Alle geheimen Merkmale des Real-Führerscheins werden jedoch dupliziert und auf die gefälschte Kopie gedruckt. Daher raten wir unseren Kunden immer, die Originaldokumente von uns erstellen zu lassen, wenn sie das Dokument legal verwenden möchten. Kontaktiere uns jetzt
    Führerschein kaufen
    US-Führerschein kaufen
    Kanadischer Führerschein kaufen
    ASIAN-Führerschein kaufen
    EU-Führerschein kaufen
    polnischen Führerschein kaufen
    deutschen Führerschein kaufen
    Kaufen Sie einen echten US-Pass
    Kaufen Sie einen echten kanadischen Pass
    echten deutschen Reisepass kaufen
    Real UK Passport kaufen
    RUSSISCHEN Reisepass kaufen
    Real TURKISH Passport kaufen
    Kaufen Sie einen echten spanischen Pass
    echten französischen Reisepass kaufen
    Kaufen Sie einen echten niederländischen Reisepass
    echten italienischen Reisepass kaufen
    US-Personalausweis kaufen
    KANADISCHE ID-Karte kaufen
    ITALIENISCHE ID-Karte kaufen
    DEUTSCHEN Personalausweis kaufen
    POLNISCHE ID-Karte kaufen
    TÜRKISCHE ID-Karte kaufen
    Kaufen Sie einen spanischen Personalausweis
    Geburtsurkunde online kaufen
    SSN kaufen
    Diplom kaufen
    IELTS kaufen
    Visum online kaufen
    US Green Card online kaufen

    ReplyDelete
  25. Wir produzieren sowohl echte als auch gefälschte Führerscheine. Für den Real Driver’s License registrieren wir alle Informationen im Datenbanksystem. Wenn der Führerschein mit einem Datenlesegerät überprüft wird, werden alle Ihre Informationen im System angezeigt und Sie müssen das Dokument legal verwenden. Wir produzieren auch einen Führerschein, der mit dem Führerschein identisch ist. Es werden jedoch keine Informationen auf dem Dokument im Datenbanksystem registriert. Das Dokument wird also gefälscht sein. Alle geheimen Merkmale des Real-Führerscheins werden jedoch dupliziert und auf die gefälschte Kopie gedruckt. Daher raten wir unseren Kunden immer, die Originaldokumente von uns erstellen zu lassen, wenn sie das Dokument legal verwenden möchten. Kontaktiere uns jetzt
    Führerschein kaufen
    US-Führerschein kaufen
    Kanadischer Führerschein kaufen
    ASIAN-Führerschein kaufen
    EU-Führerschein kaufen
    polnischen Führerschein kaufen
    deutschen Führerschein kaufen
    Kaufen Sie einen echten US-Pass
    Kaufen Sie einen echten kanadischen Pass
    echten deutschen Reisepass kaufen
    Real UK Passport kaufen
    RUSSISCHEN Reisepass kaufen
    Real TURKISH Passport kaufen
    Kaufen Sie einen echten spanischen Pass
    echten französischen Reisepass kaufen
    Kaufen Sie einen echten niederländischen Reisepass
    echten italienischen Reisepass kaufen
    US-Personalausweis kaufen
    KANADISCHE ID-Karte kaufen
    ITALIENISCHE ID-Karte kaufen
    DEUTSCHEN Personalausweis kaufen
    POLNISCHE ID-Karte kaufen
    TÜRKISCHE ID-Karte kaufen
    Kaufen Sie einen spanischen Personalausweis
    Geburtsurkunde online kaufen
    SSN kaufen
    Diplom kaufen
    IELTS kaufen
    Visum online kaufen
    US Green Card online kaufen

    ReplyDelete
  26. Amazing, this is great as you want to learn more, I invite to This is my page. buy real Drivers license

    ReplyDelete
  27. Awesome work, in case you want any docx file converter then use this one;

    File Spinner

    ReplyDelete
  28. That spinner will converter your one file to other formats like those;

    Docx to Zip

    Mp2 to Mp3

    Mp4 to Png

    Oga to Mp3

    Tga to Jpg

    ReplyDelete
  29. führerschein kaufen ohne prüfung

    Willkommen zu EU-Führerschein, Kaufen sie einen führerschein in deutschland ohne schriftliche und praktische prüfungen. Wir bieten absolut alle kategorien von legal führerscheinen in deutschland an.


    to get more - https://eufuhrerschein.org/

    ReplyDelete
  30. READ CAREFULLY TO HELP SOMEONE
    My name is clara i'm from uk,i want to use this opportunity to disclose a vital information which I was asked not to.i have a brother who work as an anonymous he was the one who told me about this FREE CARD that you can used in buying and cash out a limit amount of money.he further told me their manager specialize in doing FREE CARD that he his going to send me his email but i should not expose him to manager in case he ask me how i got his email, that i found it on the internet so two days after i contacted him he reply was what's your name and how did you got my email which i told him exactly what my brother told me so after that he further asked me how he can be off help to me which i did by explaining my financial situation.After my explanation he asked some question which i answer then he reply by telling me he his a God fearing man that he was touch by my story that his going to help me.i was so happy he didn't rejected my offer.   So after the interaction he ask me to give him some days that he will get back to me,then after a week interval in the morning i receive a package unknowingly to me he was the FREE CARD follow by the instruction how am going to register and used it.today making it a year plus that have been using this FREE CARD.  NOTE;my major aim of revealing this information is because someone out there is passing through financial challenge you can as well reach him via email [officialfreecardmanager@gmail.com]
     TRY YOUR LUCK WITH GOD ALL THINGS ARE POSSIBLE

    ReplyDelete
  31. Ihr Führerschein

    Mit dem EU-Führerschein erhalten Sie einen exklusiven Service rund um das Thema Führerschein. Wir stellen unter anderem sicher, dass Sie Ihren zurückgezogenen Führerschein mit einer MPU wiedererlangen, die Sie einfach erworben haben. Sie werden die positive Stellungnahme bei der Behörde einreichen und Ihre Sperrfrist wird dann aufgehoben.https://fuhrerschein-kauf.com/mpu/

    ReplyDelete
  32. Wenn Sie einen negativen MPU-Bericht erhalten haben, sollten Sie diesen unter keinen Umständen den Führerscheinbehörden vorlegen, um Ihre Fahrzeugregistrierung zurückzudrängen. Können Sie einen positiven MPU-Bericht kaufen? In der Tat! Ab sofort brauchen Sie sich keine Sorgen mehr zu machen, wir helfen Ihnen gerne weiter. Um Ihren Führerschein zu verbessern, werden wir Sie mit vollständiger Sicherheit durch diesen Test führen. Bei uns erhalten Sie Ihren MPU-Bericht für Geld.
    https://fuhrerschein-kauf.com/mpu/

    ReplyDelete
  33. Hi There,
    Thank you for sharing the knowledgeable blog with us I hope that you will post many more blog with us:-
    Beantragen Sie den niederländischen Führerschein online, ohne die Führerscheinprüfung abzulegen Kaufen Sie echte und gefälschte Niederlande Führerschein Suchen Sie einen niederländischen Führerschein, möchten aber nicht die Fahrerlaubnisprüfung ablegen? Haben Sie den Führerscheintest viele Male nicht bestanden und möchten Sie doch den Führerschein online? Haben Sie Ihren Führerschein aufgrund von Trunkenheit am Steuer oder aufgrund von.
    Email:wergofuhrerscheindienste@gmail.com
    Click here for more information:- more info

    ReplyDelete
  34. deutschen führerschein kaufenEinen deutschen Führerschein legal und unkompliziert erwerben. Schneller und zuverlässiger Service für Ihren originalen Führerschein.

    ReplyDelete