コード例 #1
0
    private void convertDocumentFiles()
    {
        UseOffice u = new UseOffice();
        int       ret;
        string    inputFileAndPath, outputFileAndPath;

        for (int i = 0; i < documentFileNames.Length - 1; i++)
        {
            inputFileAndPath  = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), lecture_Folder + documentFileNames[i]));
            outputFileAndPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), lecture_Folder + documentFileNamesWithoutExtension[i] + ".pdf"));

            if (documentFileExtensions[i] == ".doc")
            {
                ret = u.ConvertFile(inputFileAndPath, outputFileAndPath, UseOffice.eDirection.DOC_to_PDF);
            }
            else if (documentFileExtensions[i] == ".docx")
            {
                ret = u.ConvertFile(inputFileAndPath, outputFileAndPath, UseOffice.eDirection.DOCX_to_PDF);
            }
            else if (documentFileExtensions[i] == ".ppt")
            {
                ret = u.ConvertFile(inputFileAndPath, outputFileAndPath, UseOffice.eDirection.PPT_to_PDF);
            }
            else if (documentFileExtensions[i] == ".pptx")
            {
                ret = u.ConvertFile(inputFileAndPath, outputFileAndPath, UseOffice.eDirection.PPTX_to_PDF);
            }
            else if (documentFileExtensions[i] == ".txt")
            {
                ret = u.ConvertFile(inputFileAndPath, outputFileAndPath, UseOffice.eDirection.TEXT_to_PDF);
            }
        }
    }
        /// <summary> Convert a Microsoft Powerpoint file to a PDF </summary>
        /// <param name="Powerpoint_In_File"> Input file (powerpoint) </param>
        /// <param name="PDF_Out_File"> Outpuf file (pdf) </param>
        /// <returns>An error value</returns>
        public static int Powerpoint_To_PDF(string Powerpoint_In_File, string PDF_Out_File)
        {
            UseOffice u = new UseOffice();

            //Prepare UseOffice .Net, loads MS Powerpoint in memory
            int ret = u.InitPowerPoint();

            u.Serial = "10006108851";

            //Return values:
            //0 - Loading successfully
            //1 - Can't load MS Powerpoint® library in memory (returned as 4)

            if (ret == 1)
            {
                return(4);
            }

            if (Powerpoint_In_File.ToUpper().IndexOf(".PPTX") > 0)
            {
                //Converting
                ret = u.ConvertFile(Powerpoint_In_File, PDF_Out_File, UseOffice.eDirection.PPTX_to_PDF);
            }
            else
            {
                //Converting
                ret = u.ConvertFile(Powerpoint_In_File, PDF_Out_File, UseOffice.eDirection.PPT_to_PDF);
            }

            //Release MS Powerpoint from memory
            u.ClosePowerPoint();

            //0 - Converting successfully
            //1 - Can't open input file. Check that you are using full local path to input file, URL and relative path are not supported
            //2 - Can't create output file. Please check that you have permissions to write by this path or probably this path already used by another application
            //3 - Converting failed, please contact with our Support Team
            //4 - MS Office isn't installed. The component requires that any of these versions of MS Office should be installed: 2000, XP, 2003, 2007 or 2010
            return(ret);
        }
        /// <summary> Convert a Microsoft Word file to a PDF </summary>
        /// <param name="Word_In_File"> Input file (word) </param>
        /// <param name="PDF_Out_File"> Outpuf file (pdf) </param>
        /// <returns>An error value</returns>
        public static int Word_To_PDF( string Word_In_File, string PDF_Out_File )
        {
            UseOffice u = new UseOffice();

            //Prepare UseOffice .Net, loads MS Word in memory
            int ret = u.InitWord();
            u.Serial = "10006108851";

            //Return values:
            //0 - Loading successfully
            //1 - Can't load MS Word® library in memory (returned as 4)

            if (ret == 1)
                return 4;

            if (Word_In_File.ToUpper().IndexOf(".DOCX") > 0)
            {
                //Converting
                ret = u.ConvertFile(Word_In_File, PDF_Out_File, UseOffice.eDirection.DOCX_to_PDF);
            }
            else
            {
                //Converting
                ret = u.ConvertFile(Word_In_File, PDF_Out_File, UseOffice.eDirection.DOC_to_PDF);
            }

            //Release MS Word from memory
            u.CloseWord();

            //0 - Converting successfully
            //1 - Can't open input file. Check that you are using full local path to input file, URL and relative path are not supported
            //2 - Can't create output file. Please check that you have permissions to write by this path or probably this path already used by another application
            //3 - Converting failed, please contact with our Support Team
            //4 - MS Office isn't installed. The component requires that any of these versions of MS Office should be installed: 2000, XP, 2003, 2007 or 2010
            return ret;
        }
コード例 #4
0
ファイル: PrintRepository.cs プロジェクト: MartinBG/Gva
        public Stream ConvertWordStreamToPdfStream(Stream stream)
        {
            var tmpDocFile = Path.GetTempFileName();
            var tmpPdfFile = Path.GetTempFileName();

            lock (syncRoot)
            {
                try
                {
                    using (var tmpFileStream = File.OpenWrite(tmpDocFile))
                    {
                        this.CopyStream(stream, tmpFileStream);
                    }

                    UseOffice useOffice = new UseOffice();
                    useOffice.Serial = ConfigurationManager.AppSettings["Gva.Api:UseOfficeSerialNumber"];

                    useOffice.InitWord();
                    useOffice.ConvertFile(tmpDocFile, tmpPdfFile, UseOffice.eDirection.DOCX_to_PDF);
                    useOffice.CloseWord();
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    File.Delete(tmpDocFile);
                }

            }

            return new FileStream(tmpPdfFile,
                FileMode.Open,
                FileAccess.Read,
                FileShare.None,
                DEFAULT_BUFFER_SIZE,
                FileOptions.DeleteOnClose);
        }