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)
- using System;
- using Microsoft.Office.Interop.Word;
- using System.Reflection;
- using System.IO;
- namespace OfficeInterop
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Get the path of the executing assembly.
- string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- // The location to save the file.
- Object oSaveAsFile = (Object)(path + @"\Test.docx");
- // Object of Missing "Null Value".
- Object oMissing = System.Reflection.Missing.Value;
- // Object of false.
- Object oFalse = false;
- // Create object of Word and Document.
- Application oWord = new Application();
- Document oWordDoc = new Document();
- // Don't display Word.
- oWord.Visible = false;
- // Add a new document.
- oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
- // Enter some text with the font Arial Black.
- oWord.Selection.Font.Name = "Arial Black";
- oWord.Selection.TypeText("Hello World");
- // Save the document.
- oWordDoc.SaveAs
- (
- ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
- ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing,
- ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
- ref oMissing, ref oMissing
- );
- // Close the file.
- oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);
- // Quit Word.exe
- oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
- }
- }
- }
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)
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Wordprocessing;
- using DocumentFormat.OpenXml;
- namespace OOXML
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Create a package.
- WordprocessingDocument doc =
- WordprocessingDocument.Create
- (
- @"Test.docx",
- WordprocessingDocumentType.Document
- );
- // Create a main document part and add it to the package.
- MainDocumentPart mainPart = doc.AddMainDocumentPart();
- mainPart.Document = new Document();
- // Create some content.
- Text textFirstLine = new Text("Hello World");
- Run run = new Run(textFirstLine);
- Paragraph para = new Paragraph(run);
- // Apply a font.
- RunProperties runProp = new RunProperties();
- RunFonts runFont = new RunFonts();
- runFont.Ascii = "Arial Black";
- runProp.Append(runFont);
- run.PrependChild<RunProperties>(runProp);
- // Add the content into the document.
- Body body = new Body(para);
- mainPart.Document.Append(body);
- /* Save and close */
- mainPart.Document.Save();
- doc.Close();
- }
- }
- }
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)
- using System;
- using Novacode;
- using System.Drawing;
- namespace DocXHelloWorld
- {
- class Program
- {
- static void Main(string[] args)
- {
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Add a new Paragraph to the document.
- Paragraph p = document.InsertParagraph();
- // Append some text.
- p.Append("Hello World").Font(new FontFamily("Arial Black"));
- // Save the document.
- document.Save();
- }
- }
- }
- }
I need help here!
ReplyDeleteI 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...
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/
ReplyDeletehow can i save word as pdf using docx?
ReplyDeleteCathal Coffey , Thank you for your blog , it is helpful
ReplyDeleteis there a way to integrate an audio file into an existing document using DocX?
ReplyDeletehi, 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..
ReplyDeleteI 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?
ReplyDeleteHi, I am using vb in my asp.net project. I have 2 problems
ReplyDelete1. 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
Hola estoy muy interesado en esta librería y quisiera saber si existe algún manual en español sobre el mismo.
ReplyDeleteThank you for creating this library.
ReplyDeleteHello, 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!
ReplyDelete-Jeremy
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!
ReplyDelete-Jeremy
Hi Cathal,
ReplyDeleteDoes 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?
Hi Cathal,
ReplyDeleteHow I would look in DocX?
//TablesOfContents UPDATE in Microsoft.Office.Interop.Word
foreach (Word.TableOfContents T in wordDoc.TablesOfContents)
{
T.Update();
}
Tanks
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?
ReplyDeleteYhym... Trying to add NEW core properties to NEW docx file is impossible from the early begining of DocX.... Awsome piece of software ... Truly ..
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteDear Cathal, first of it is a wonderful effort and initiative by you, really appreciate it.
ReplyDeleteThe 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.
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?
ReplyDeleteThanks & Regards,
Idhris
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.
ReplyDeleteWe 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
ReplyDeleteGenuine 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
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
ReplyDeletecumpă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
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
ReplyDeletecumpă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
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
ReplyDeleteFü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
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
ReplyDeleteFü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
Amazing, this is great as you want to learn more, I invite to This is my page. buy real Drivers license
ReplyDeleteAwesome work, in case you want any docx file converter then use this one;
ReplyDeleteFile Spinner
That spinner will converter your one file to other formats like those;
ReplyDeleteDocx to Zip
Mp2 to Mp3
Mp4 to Png
Oga to Mp3
Tga to Jpg
führerschein kaufen ohne prüfung
ReplyDeleteWillkommen 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/
READ CAREFULLY TO HELP SOMEONE
ReplyDeleteMy 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
Ihr Führerschein
ReplyDeleteMit 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/
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.
ReplyDeletehttps://fuhrerschein-kauf.com/mpu/
Hi There,
ReplyDeleteThank 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
deutschen führerschein kaufenEinen deutschen Führerschein legal und unkompliziert erwerben. Schneller und zuverlässiger Service für Ihren originalen Führerschein.
ReplyDelete