0

How to convert from word to pdf .

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Xceed.Words.NET;
using Microsoft.Office.Interop.Word;
public partial class new_test : Form
{
    public new_test()
    {
        InitializeComponent();
    }
  public Microsoft.Office.Interop.Word.Document WordDocument { get; private set; }


    private void button1_Click(object sender, EventArgs e)
    {
    var doc = DocX.Load("C:\\Users\\IT-TEAM\\Desktop\\text0\\tm.docx");
   doc.SaveAs("C:\\Users\\IT-TEAM\\Desktop\\text0\\" + textBox1.Text + ".docx");


        Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
        WordDocument = appWord.Documents.Open("C:\\Users\\IT-TEAM\\Desktop\\text0\\" + textBox1.Text + ".docx");
        WordDocument.ExportAsFixedFormat("C:\\Users\\IT-TEAM\\Desktop\\text0\\" + textBox1.Text + ".pdf", WdExportFormat.wdExportFormatPDF);

i use this code but every time it save the word show in Task Manager as still using word i tried

WordDocument.Close();

still showing in Task Manager

  • Visit this link https://www.codeproject.com/Questions/420350/convert-word-file-into-pdf – Marwen Jaffel Jan 19 '20 at 09:25
  • Thank you for your response, your suggestion letting me to use internet connection to send files to the api, but i want a solution working on offline mode because that's our work polices. – Hussen Alkunyali Jan 19 '20 at 09:35
  • There are *two* 'save' calls: the first is a plain `doc.SaveAs`, as Word file, the other is the `ExportAsFixedFormat`. So per run you should receive two new files. Are you sure you are not looking at the wrong one? Try with [`OpenAfterExport`](https://learn.microsoft.com/en-us/office/vba/api/word.document.exportasfixedformat) enabled and see if that shows where your document end up. – Jongware Jan 20 '20 at 22:16
  • @HussenAlkunyali don't use Word for such conversions. For starters, you have to pay for a license for every user using that Word installation. Second, you have to be extra careful to close and clear the **application** reference, `appWord`, not just the document. Failing to do so results in an orphaned Word instance – Panagiotis Kanavos Feb 26 '20 at 09:20
  • @HussenAlkunyali you have to use [Application.Quit](https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.applicationclass.quit?view=word-pia) to tell Word it can close. Word itself (or any other COM server app) won't close until all references to its objects are released. The GC will do that eventually, but you can force it by calling `GC.Collect(); GC.WaitForPendingFinalizers();`. – Panagiotis Kanavos Feb 26 '20 at 09:31
  • Use a *separate* method to work with Word, store all references to variables in that method, not fields, and call `Quit()` inside that method. When execution leaves that method all references will be out of scope and will be released by the GC eventually. Check [this answer for more details](https://stackoverflow.com/questions/25134024/clean-up-excel-interop-objects-with-idisposable/25135685#25135685) – Panagiotis Kanavos Feb 26 '20 at 09:32

2 Answers2

-1

Try This Code.

 public partial class Page: System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
            wordDocument = appWord.Documents.Open(@"C:\\Users\\IT-TEAM\\text0\\y" + textBox1.Text + ".docx");
            wordDocument.ExportAsFixedFormat(@"C:\\Users\\IT-TEAM\\y" + textBox1.Text + ".pdf", WdExportFormat.wdExportFormatPDF);
        }

        public Microsoft.Office.Interop.Word.Document wordDocument { get; set; }
    }
    }
Bibin
  • 492
  • 5
  • 11
  • your solution it's same my code but i use it in from and you in web-from – Hussen Alkunyali Jan 19 '20 at 10:27
  • This starts a new word instance each time the page is loaded and leaves it running. This will quickly flood the web server by starting too many Word instances and eating all memory. COM application references must *always* be closed and disposed, otherwise the application remains running – Panagiotis Kanavos Feb 26 '20 at 09:16
-1

You don't want to write the code for the conversion.Now, there are several saas model API libraries available in market. PDF4me provides an API collection which you can try. [https://developer.pdf4me.com/]:https://developer.pdf4me.com/.

It provides you both Javascript and C# library which is very cost effective and reliable. By using these tools you can reduce development and infrastructure cost.

  • There's no reason to use a web service that probably uses an open source library for the conversion, when you can just use that library. `pandoc` for example can convert between a lot of document types without requiring a Word installation – Panagiotis Kanavos Feb 26 '20 at 09:16
  • [Pdf4me](https://developer.pdf4me.com/) is not just a API lib collection. It also provides you batch processing of files using desktop app. There are several connectors provided by PDF4me which will make your software code clean and product simple. As you told, PDF4me runs without a Word Installation and also reduces threshold on you internal server. – Abins Chittilappillly James Mar 02 '20 at 14:20