Pdf Writer in C# without any using Paid dll | Pdf writer using Microsoft.Office.Interop.Word in c#

Below code for PDf Writer by using Microsoft.Office.Interop.Word  dll

Pdf Writer in C# without any using Paid dll | Pdf writer using Microsoft.Office.Interop.Word in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;
using System.Diagnostics;
using Microsoft.Office.Interop;
using System.Reflection;
namespace PdfGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateDocFileInFolder();
            Microsoft.Office.Interop.Word.Application word =
                new Microsoft.Office.Interop.Word.Application();
          
            object oMissing = System.Reflection.Missing.Value;
            // Get list of Word files in specified directory
            DirectoryInfo dirInfo = new DirectoryInfo(@"C:\New folder\New Folder");
            FileInfo[] wordFiles = dirInfo.GetFiles("*.doc"); word.Visible = false;
            word.ScreenUpdating = false;

            foreach (FileInfo wordFile in wordFiles)
            {     // Cast as Object for word Open method   
                Object filename = (Object)wordFile.FullName;
                // Use the dummy value as a placeholder for optional arguments 
                Document doc = word.Documents.Open(ref filename, 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);
                doc.Activate();
                object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
                object fileFormat = WdSaveFormat.wdFormatPDF;
                // Save document into PDF Format  
                doc.SaveAs(ref outputFileName,
                    ref fileFormat, 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 Word document, but leave the Word application open.
                // doc has to be cast to type _Document so that it will find the  
                // correct Close method.                  
                object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                doc = null;

            }
           


        }

        private static void CreateDocFileInFolder()
        {
          
              

                //Start Word and create a new document.
                Microsoft.Office.Interop.Word._Application oWord;
                Microsoft.Office.Interop.Word._Document oDoc =null;
                try
                {
                    object oMissing = System.Reflection.Missing.Value;
                    object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

                    oWord = new Microsoft.Office.Interop.Word.Application();
                    oWord.Visible = true;
                    oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing);

                    //Insert a paragraph at the beginning of the document.
                    Microsoft.Office.Interop.Word.Paragraph oPara1;
                    oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
                    oPara1.Range.Text = "TUIFly Cabin Crew Application";
                    oPara1.Range.Font.Bold = 1;
                    oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
                    oPara1.Range.InsertParagraphAfter();
                    // Insert Image

                    string filename1 = @"C:\BBC\tuiflylogo.jpg";
                    // now add the picture in active document reference
                    oDoc.InlineShapes.AddPicture(filename1, Type.Missing, Type.Missing, Type.Missing);



                    //Insert a paragraph at the end of the document.
                    Microsoft.Office.Interop.Word.Paragraph oPara2;
                    object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                    oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
                    oPara2.Range.Text = "TUIfly Netbook Application ";
                    oPara2.Format.SpaceAfter = 6;
                    oPara2.Range.InsertParagraphAfter();

                    //Insert another paragraph.
                    Microsoft.Office.Interop.Word.Paragraph oPara3;
                    oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                    oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
                    oPara3.Range.Text = "Dynamic Table for Crew 3LTR Code and Function:";
                    oPara3.Range.Font.Bold = 0;
                    oPara3.Format.SpaceAfter = 24;
                    oPara3.Range.InsertParagraphAfter();

                    //Insert a 3 x 5 table, fill it with data, and make the first row
                    //bold and italic.
                    Microsoft.Office.Interop.Word.Table oTable;
                    Microsoft.Office.Interop.Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                    oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);
                    oTable.Range.ParagraphFormat.SpaceAfter = 6;
                    int r, c;
                    string strText;
                    for (r = 1; r <= 3; r++)
                        for (c = 1; c <= 5; c++)
                        {
                            strText = "r" + r + "c" + c;
                            oTable.Cell(r, c).Range.Text = strText;
                        }
                    oTable.Rows[1].Range.Font.Bold = 1;
                    oTable.Rows[1].Range.Font.Italic = 1;

                    //Add some text after the table.
                    Microsoft.Office.Interop.Word.Paragraph oPara4;
                    oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                    oPara4 = oDoc.Content.Paragraphs.Add(ref oRng);
                    oPara4.Range.InsertParagraphBefore();
                    oPara4.Range.Text = "And Table you Want:";
                    oPara4.Format.SpaceAfter = 24;
                    oPara4.Range.InsertParagraphAfter();

                    //Insert a 5 x 2 table, fill it with data, and change the column widths.
                    wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                    oTable = oDoc.Tables.Add(wrdRng, 5, 2, ref oMissing, ref oMissing);
                    oTable.Range.ParagraphFormat.SpaceAfter = 6;
                    for (r = 1; r <= 5; r++)
                        for (c = 1; c <= 2; c++)
                        {
                            strText = "r" + r + "c" + c;
                            oTable.Cell(r, c).Range.Text = strText;
                        }
                    oTable.Columns[1].Width = oWord.InchesToPoints(2); //Change width of columns 1 & 2
                    oTable.Columns[2].Width = oWord.InchesToPoints(3);

                    //Keep inserting text. When you get to 7 inches from top of the
                    //document, insert a hard page break.
                    object oPos;
                    double dPos = oWord.InchesToPoints(7);
                    oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter();
                    do
                    {
                        wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                        wrdRng.ParagraphFormat.SpaceAfter = 6;
                        wrdRng.InsertAfter("A line of text");
                        wrdRng.InsertParagraphAfter();
                        oPos = wrdRng.get_Information
                                       (Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage);
                    }
                    while (dPos >= Convert.ToDouble(oPos));
                    object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
                    object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
                    wrdRng.Collapse(ref oCollapseEnd);
                    wrdRng.InsertBreak(ref oPageBreak);
                    wrdRng.Collapse(ref oCollapseEnd);
                    wrdRng.InsertAfter(":::::::");
                    wrdRng.InsertParagraphAfter();
                    object filename = @"C:\New folder\New Folder\MyDoc.doc";
                 
                    oDoc.SaveAs(ref filename, 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);

                 
                    wrdRng.InsertAfter("THE END.");
                }

                catch (Exception ex)
                {

                    if (oDoc != null)
                    {
                        oDoc.Close();
                    }
                 

                }
                finally
                {
                    if (oDoc != null)
                    {
                        oDoc.Close();
                    }
                }
         


       

        }
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.