如何将Word interop转换为pdf, [英] How to convert Word interop to pdf,

查看:99
本文介绍了如何将Word interop转换为pdf,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,用户可以在下拉列表中选择pdf或单词扩展名,我已经使用word interop下载,但是当用户选择pdf时,我可以通过某种方式调用单词interop函数,然后将其转换为pdf?



这是我的单词函数,

I have a dropdownlist there the user can select pdf or word extension in the dropdown, i have made it work with word interop to download , but is i possible on some way when the user select pdf, to call the word interop function, and then convert it to pdf?

here is my function for word,

private void SetWordDocument()
       {


               object strFileName =
               CleanUp(LabelFirstName.Text + "_" + LabelLastName.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd") + "_SWE"+
                       "." + DropDownListDownloadCv.SelectedItem.Text);

               object fs = Server.MapPath("~/Upload/") + strFileName;

               using (var db = new knowitCVdbEntities())
               {
                   var theEmpl = (from p in db.EMPLOYEES
                                  where p.username == strUserName
                                  select p).FirstOrDefault();

                   GetAlSwedishlAssigmentsDoc(theEmpl);

                   if (theEmpl != null)
                   {

                       object missing = Missing.Value;
                       object start1 = 0;

                       object oTemplate = "c:\\knowittemplate.dot";
                       var wordApp = new ApplicationClass();
                       var myDoc = wordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);
                       object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
                       Range rng = myDoc.Range(ref start1, ref missing);
                       const char newLine = (char)11;
                       foreach (Field myMergeField in myDoc.Fields)
                       {

                           Range rngFieldCode = myMergeField.Code;

                           String fieldText = rngFieldCode.Text;



                           // ONLY GETTING THE MAILMERGE FIELDS

                           if (fieldText.StartsWith(" MERGEFIELD"))
                           {

                               // THE TEXT COMES IN THE FORMAT OF

                               // MERGEFIELD  MyFieldName  \\* MERGEFORMAT

                               // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"

                               Int32 endMerge = fieldText.IndexOf("\\", System.StringComparison.Ordinal);

                               Int32 fieldNameLength = fieldText.Length - endMerge;

                               String fieldName = fieldText.Substring(11, endMerge - 11);

                               // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE

                               fieldName = fieldName.Trim();

                               // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//

                               // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

                               if (fieldName == "NAME")
                               {
                                   myMergeField.Select();
                                   wordApp.Selection.TypeText(LabelFirstName.Text + " " + LabelLastName.Text);
                               }



myDoc.SaveAs(ref fs,
                                        ref missing, ref missing, ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing, ref missing, ref missing,
                                        ref missing,
                                        ref missing,
                                        ref missing, ref missing, ref missing);




                           rng.InsertBefore(newLine.ToString());
                           string imageProfPic = ImageProfilePicture.ImageUrl;
                           rng.InlineShapes.AddPicture(imageProfPic, ref missing, ref missing, ref missing);
                           rng.InsertBefore(newLine.ToString());
                           rng.InsertAfter(newLine.ToString());
                           wordApp.Visible = true;
               ref missing, ref missing,ref missing);



                       }
                       catch (Exception)
                       {
                           throw;
                       }

                       finally
                       {

                           myDoc.Save();
                           myDoc.Close(ref doNotSaveChanges, ref missing, ref missing);
                           wordApp.Quit(ref doNotSaveChanges, ref missing, ref missing);





                           System.IO.Stream iStream = null;

                           // Buffer to read 10K bytes in chunk:
                           byte[] buffer = new Byte[10000];

                           // Length of the file:
                           int length;

                           // Total bytes to read:
                           long dataToRead;

                           // Identify the file to download including its path.
                           string filepath = fs.ToString();
                           // Identify the file name.
                           string filename = System.IO.Path.GetFileName(filepath);

                           try
                           {

                               iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                                                                  System.IO.FileAccess.Read, System.IO.FileShare.Read);

                               dataToRead = iStream.Length;

                               Response.ContentType = "application/octet-stream";
                               Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

                               while (dataToRead > 0)
                               {

                                   if (Response.IsClientConnected)
                                   {

                                       length = iStream.Read(buffer, 0, 10000);


                                       Response.OutputStream.Write(buffer, 0, length);

                                       Response.Flush();

                                       buffer = new Byte[10000];
                                       dataToRead = dataToRead - length;
                                   }
                                   else
                                   {
                                       dataToRead = -1;
                                   }
                               }
                           }
                           catch (Exception ex)
                           {
                               Response.Write("Error : " + ex.Message);
                           }
                           finally
                           {
                               if (iStream != null)
                               {

                                   iStream.Close();

                               }
                               Response.Close();

                           }


                       }
                   }
               }


           }
       }
   }

推荐答案

我不太了解,如何在ASP.net中实现这一点。 />
但我在我的一个Windows应用程序中将单词转换为PDF。

为此,我需要保存和XPS和PDF添加:Microsoft另存为PDF或XPS [下载] [ ^ ]及以下是将Word转换为PDF的代码,



I have not much idea, how to implement this in ASP.net.
But i have converted word to PDF in one of my windows application.
For that i need "Save and XPS and PDF " add in : Microsoft Save as PDF or XPS [Download] [^] and below is the code to conver Word to PDF,

using Word = Microsoft.Office.Interop.Word;

private bool Convert(string sourcePath, string targetPath)
        {
            Word.WdExportFormat targetFileType == Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
            bool result;
            object paramMissing = Type.Missing;            
            Word.Application wordApplication = new Word.Application();
            Word.Document wordDocument = null;
            try
            {
                object paramSourceDocPath = sourcePath;
                string paramExportFilePath = targetPath;
                Word.WdExportFormat paramExportFormat = targetFileType;
                bool paramOpenAfterExport = false;
                Word.WdExportOptimizeFor paramExportOptimizeFor =
                Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                Word.WdExportCreateBookmarks paramCreateBookmarks =
                Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;
                wordDocument = wordApplication.Documents.Open(
                                       ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                                       ref paramMissing, ref paramMissing, ref paramMissing,
                                       ref paramMissing, ref paramMissing, ref paramMissing,
                                       ref paramMissing, ref paramMissing, ref paramMissing,
                                       ref paramMissing, ref paramMissing, ref paramMissing,
                                       ref paramMissing);

                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                    paramExportFormat, paramOpenAfterExport,
                    paramExportOptimizeFor, paramExportRange, paramStartPage,
                    paramEndPage, paramExportItem, paramIncludeDocProps,
                    paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                    paramBitmapMissingFonts, paramUseISO19005_1,
                    ref paramMissing);
                result = true;
            }
            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }





sourcePath :word文件的路径

targetPath :带有名称和扩展名的pdf文件的路径。 (即D:\\Output.pdf)



sourcePath : path of word file
targetPath : path of pdf file with name and extension. (i.e. D:\\Output.pdf)


这篇关于如何将Word interop转换为pdf,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆