Home > Software engineering >  Converting docx template to pdf
Converting docx template to pdf

Time:06-24

I'm trying to programmatically populate a template (.docx file) and save it as a PDF. Is there a free library to do this? Or possibly another format to use for the template?

Would prefer not pay use interop methods, online services, or pay tons of money if possible.

I could use RDLC but I'd prefer to be able to modify the template with something like Word.

Using asp.net core 6.

CodePudding user response:

You could try to use enter image description here

Below is my test code which created the Word file.

using Word=Microsoft.Office.Interop.Word;

object missing = System.Reflection.Missing.Value;
            var wordApp = new Word.Application();
            wordApp.Visible = true;
            Microsoft.Office.Interop.Word.Document document= wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            object filename = @"C:\Users\Administrator\Desktop\temp1.docx";
            document.SaveAs2(ref filename);
            document.Close(ref missing, ref missing, ref missing);
            document = null;
            wordApp.Quit(ref missing, ref missing, ref missing);
            wordApp = null;

Further, you could modify it as per your requirements.

Below are some helpful links:

  1. enter image description here

    see https://github.com/GitHubRulesOK/MyNotes/raw/master/AppNotes/Doc2PDF.cmd

    Since DocX is similar is some areas and features to Open Office Documents, then either Open office or Libre Office save as PDF is the second best option.

    Libre/Open Office is cross platform and can be driven via command line, or Basic for office applications, thus an ideal candidate for conversions.

    The list of command line options for Libre Office is currently at https://help.libreoffice.org/latest/en-US/text/shared/guide/start_parameters.html

    To call writer you simply invoke along the lines of

     soffice --headless --convert-to pdf --outdir /folder/ /path/to/input.docx 
    

    using options as per https://stackoverflow.com/a/70735498/10802527

  • Related