{"id":276,"date":"2014-09-05T13:01:34","date_gmt":"2014-09-05T03:01:34","guid":{"rendered":"http:\/\/www.docx4java.org\/blog\/?p=276"},"modified":"2015-02-15T06:37:00","modified_gmt":"2015-02-14T20:37:00","slug":"docx-to-pdf-in-c-net","status":"publish","type":"post","link":"https:\/\/www.docx4java.org\/blog\/2014\/09\/docx-to-pdf-in-c-net\/","title":{"rendered":"docx to PDF in C#\/.NET"},"content":{"rendered":"<p>How to convert docx to PDF without using Microsoft Word?<\/p>\n<p>If you docx is mainly text, tables and images,\u00a0<span style=\"font-size: 13px;\">docx4j.NET may work well for you. \u00a0<strong>Edit (Feb 2015): if not, you may be interested in our new <a href=\"\/blog\/2015\/02\/high-fidelity-pdf-output\/\">commercial high fidelity PDF renderer<\/a>.<\/strong><\/span><\/p>\n<p><span style=\"font-size: 13px;\">docx4j.NET is open source (Apache software license v2), identical to the Java version, but made into a DLL using IKVM. \u00a0Currently we&#8217;re at v3.2.0, released last week.<\/span><\/p>\n<p><span style=\"font-size: 13px;\">It is easy to test; you can upload your docx to the <a href=\"http:\/\/webapp.docx4java.org\/OnlineDemo\/demo_landing.html\">docx4j demo webapp<\/a><\/span><\/p>\n<p>Or with very little effort, you can run it from a sample project in Visual Studio. \u00a0Its very easy, because docx4j.NET is in the <a href=\"https:\/\/www.nuget.org\/packages\/docx4j.NET\/\">NuGet.org repository<\/a>:<\/p>\n<p><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/nugetorg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-278\" title=\"nugetorg\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/nugetorg-300x139.png\" alt=\"\" width=\"300\" height=\"139\" srcset=\"https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/nugetorg-300x139.png 300w, https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/nugetorg.png 906w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>To create your sample project:<\/p>\n<ol>\n<li><span style=\"font-size: 13px;\"> make sure you have <a href=\"http:\/\/docs.nuget.org\/docs\/start-here\/installing-nuget\">NuGet Package Manager installed<\/a><\/span>\n<ul>\n<li>for VS 2012 and later, its installed by default<\/li>\n<li>for VS 2010, NuGet is available through the Visual Studio Extension Manager; see the above link.<\/li>\n<\/ul>\n<\/li>\n<li>create a new project in Visual Studio (File &gt; New &gt; Project). \u00a0A Console Application is fine. \u00a0I chose that from the .NET 3.5 list.<\/li>\n<li>from the Tools menu, choose NuGet Package Manager &gt; Package Manager Console<\/li>\n<li>type <strong>Install-Package docx4j.NET<\/strong><\/li>\n<\/ol>\n<p>You should see something like:<\/p>\n<p><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/install.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-279\" title=\"install\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/install.png\" alt=\"\" width=\"513\" height=\"468\" srcset=\"https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/install.png 513w, https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/install-300x273.png 300w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><\/a><\/p>\n<p>And then, your project\/solution will be populated to look like:<\/p>\n<p><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/solution-explorer.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-280\" title=\"solution-explorer\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/solution-explorer.png\" alt=\"\" width=\"384\" height=\"1210\" srcset=\"https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/solution-explorer.png 384w, https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/solution-explorer-95x300.png 95w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><\/a><\/p>\n<p>We&#8217;re nearly there! \u00a0Notice the file src\/samples\/c_sharp\/Docx4NET\/DocxToPDF.cs<\/p>\n<p>Click on your project in Solution Explorer, then right click (or hit Alt+Enter) to get the properties pane:<\/p>\n<p><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/startup-object.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-281\" title=\"startup-object\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/startup-object.png\" alt=\"\" width=\"553\" height=\"234\" srcset=\"https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/startup-object.png 553w, https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/startup-object-300x126.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/a><\/p>\n<p>Then set the &#8220;startup object&#8221; as shown in the above image.<\/p>\n<p>Now you can hit Ctrl+F5 (&#8220;Start without Debugging&#8221;) &#8211; you don&#8217;t want to debug, since that&#8217;s really slow.<\/p>\n<p>You should see some logging in the console window, culminating in &#8220;done! Press any key to continue..&#8221;<\/p>\n<p><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/console-log.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-thumbnail wp-image-282\" title=\"console-log\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2014\/09\/console-log-150x150.png\" alt=\"\" width=\"150\" height=\"150\" \/><\/a><\/p>\n<p>What just happened? \u00a0All being well, the sample docx\u00a0<span style=\"font-size: 13px;\">&#8220;src\\samples\\resources\\sample-docx.docx&#8221; was saved as a PDF <\/span><span style=\"font-size: 13px;\">&#8220;OUT_sample-docx.pdf&#8221; in your project directory.<\/span><\/p>\n<p>You can modify src\/samples\/c_sharp\/Docx4NET\/DocxToPDF.cs to read your own test docx.<\/p>\n<p>A few comments.<\/p>\n<p><strong>XSL FO; Apache FOP. <\/strong>docx4j creates PDF via XSL FO. \u00a0It generates XSL FO, then uses Apache FOP (v1.1) to convert the XSL FO to PDF. \u00a0FOP also supports other output formats (the subject of another blog post).<\/p>\n<p><strong>Logging, Commons Logging. <\/strong> Logging is via Commons Logging. \u00a0In the demo, it is configured programmatically (ie in \u00a0DocxToPDF.cs). \u00a0Alternatively, you could do it in app.config.<\/p>\n<p><strong>OpenXML SDK interop:<\/strong> src\/main\/c_sharp\/Plutext\/Docx4NET contains code for converting between a docx4j representation of a docx package, and the Open XML SDK&#8217;s representation.<\/p>\n<p><strong>Improving PDF support.<\/strong> To improve the quality of the PDF output, typically you&#8217;d make the improvement to docx4j first (ie the Java version), then create a new DLL using the ant build target dist.NET. \u00a0 docx4j is on GitHub, and is most easily setup using Maven (see earlier blog post).<\/p>\n<p><strong>Help\/support\/discussion.<\/strong> You can post in the <a href=\"http:\/\/www.docx4java.org\/forums\/pdf-output-f27\/\">docx4j PDF output forum<\/a>, or on StackOverflow (be sure to use tag docx4j, plus some\/all of c#, docx, pdf, fop, xslfo as you think appropriate). \u00a0Please don&#8217;t cross post at both!<\/p>\n<p><span style=\"font-size: 13px;\"><br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to convert docx to PDF without using Microsoft Word? If you docx is mainly text, tables and images,\u00a0docx4j.NET may work well for you. \u00a0Edit (Feb 2015): if not, you may be interested in our new commercial high fidelity PDF renderer. docx4j.NET is open source (Apache software license v2), identical to the Java version, but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,71,1],"tags":[89,34],"class_list":["post-276","post","type-post","status-publish","format-standard","hentry","category-docx4j","category-microsoft-word","category-uncategorized","tag-docx4j-net","tag-pdf"],"_links":{"self":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":5,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":285,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/276\/revisions\/285"}],"wp:attachment":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}