DOCX to PDF... with Custom Document Properties

1

TLDR: How can I convert a DOC(X) to a PDF and keep CustomDocumentProperties and Fields - so that I can programmatically update the Custom Properties and print the PDF via a Windows Service?

!TLDR

We have a custom in-house application that pairs DOCX creation - followed with field updates and printing. The application as it is - a Windows Form based program - works but requires it to be run from user shell. I want to upgrade this program to be run from a Windows Service.

THE issue with DOCX in a Windows Service is printing. Must have Word Installed on the Server. Word doesn't play well outside of a user shell - Modal Dialogs, user prompts (do you want to restore one of these? Do you want to enable editing of this document? etc), required registry entries, etc.

I'm exploring adjusting our workflow to PDFs on the server.

Workflow

DOC(X) Prep/Creation (Manual, not in-program):

  • Import/Run VBScript:
    • Get table/view columns out of a database.
    • Each column goes into a Custom DocumentProperty.
  • Place Fields into document for desired Custom Properties.

So CustomerName, AddressLine1, City, etc get added as CustomDocumentProperties... then the fields get placed in the right spots on a form to print automatically in the automated program.

DOCX Usage (Automatic, in program):

  • Pull row from same table
  • Put correct data into each CustomDocumentPropertyField
  • Update fields (replaces CustomerName with Doe, John, City with Whoville, etc)
  • Print document to correct printer

Desired Changes

I'm looking to either

a. Convert the DOC(X) to a PDF in such a way that the custom properties transfer - as well as the ability to replace the fields - remains intact. Would be 1) Create DOCX (lots of these currently exist and is current workflow), 2) Convert to PDF, 3) Update/print PDF. PRO: Continued use of word products that customers are happy with/trained in. CONS: Possibly complex docx/pdf conversion in the middle in exchange for the easier PDF printing inside a windows service.

b. Replace DOC(X) completely and instead use PDF creation tools. PRO: Same two step process. CON: Users are used to DOC(X) creation. Additional software/training may be needed. Lots of documents would need to be converted.

c. Work around DOCX as a service and get it to print inside a service.

This question is exploring a - DOC(X) conversion to PDF. Given a Word Document with Custom Document Properties and Fields placed on the document... I seem to lose those properties AND the fields are replaced with the current values when I convert to PDF via Save As PDF in Word. What other viable options are there to convert to PDF - keeping that information for step 2) DOCX Usage/Printing.

WernerCD

Posted 2014-02-05T04:08:52.263

Reputation: 4 263

I think you would be better off exploring how to make/update .docx files without Word, e.g. Using the Office Open XML SDK. But you'll really need to switch from vbscript or write some vbscript-callable libraries. Have a look for Eric White's Document Builder material for some pointers. – None – 2014-02-05T07:37:57.133

@bibadia the main issue with sticking with DOCX isn't document editing/updating (I've actually explored updating the current process with OOXML and don't think THAT part would be onerous.). THE issue is printing from inside of a Windows Service (Desired change: GUI to Windows Service). Everything I read says it requires word to be installed on the server and that word doesn't play well as a service (modal dialog errors, requests for user interaction on errors, required user registry entries, etc). – WernerCD – 2014-02-05T13:56:15.350

@Werner, You can look into Aspose.Words for .NET which can retain custom document properties when converting DOCX to PDF without using word automation. It is also fully supported in a server scenario. – Adam – 2014-02-06T12:48:07.447

Answers

0

Use pdfsharp, it is an open source library. You can set the custom properties of pdf like this:

PdfDocument document = PdfReader.Open("c:\\Test.pdf"); 
document.Info.Elements.Add(
     new KeyValuePair<String, PdfItem>("/MyKey", new PdfString("MyValue")));
document.Save("c:\\test.pdf");

StartCoding

Posted 2014-02-05T04:08:52.263

Reputation: 101