private OASResponse ConvertWordImmediately(OASModels.ConversionSettings settings, SPUserToken userToken)
        {
            OASResponse oasResponse = new OASResponse();

            ConversionJobSettings set = FillWordConversionOptions(settings.Options);

            set.OutputFormat = SaveFormat.PDF;

            SyncConverter syncConv = new SyncConverter(ConfigurationManager.AppSettings["WASName"], set);

            if (userToken != null)
            {
                syncConv.UserToken = userToken;
            }

            byte[]             input = Convert.FromBase64String(settings.Content);
            byte[]             output;
            ConversionItemInfo convInfo = syncConv.Convert(input, out output);

            if (convInfo.Succeeded)
            {
                oasResponse.Content   = Convert.ToBase64String(output);
                oasResponse.ErrorCode = OASErrorCodes.Success;
            }
            else
            {
                oasResponse.ErrorCode = OASErrorCodes.ErrFailedConvert;
                oasResponse.Message   = convInfo.ErrorMessage;
            }

            return(oasResponse);
        }
示例#2
0
        private void WordDocsToConvertToPdf(SPList library, int ElementID)
        {
            //Perform a SPQuery that returns only Word Documents.
            SPQuery query = new SPQuery();

            query.Folder = library.RootFolder;
            //Include all subfolders so include Recursive Scope.
            query.ViewXml = @"<View Scope='Recursive'>
                                <Query>
                                   <Where>
                                        <Or>
                                            <Contains>
                                                <FieldRef Name='File_x0020_Type'/>
                                                <Value Type='Text'>doc</Value>
                                            </Contains>
                                            <Contains>
                                                <FieldRef Name='File_x0020_Type'/>
                                                <Value Type='Text'>docx</Value>
                                            </Contains>
                                        </Or>
                                    </Where>
                                </Query>
                            </View>";

            //Get Documents
//          SPListItemCollection listItems = library.GetItems(query);

            SPListItem listitem = library.Items.GetItemById(ElementID);

            //Check that there are any documents to convert.
//            if (listItems.Count > 0)
//            {
//                foreach (SPListItem li in listItems)
//                {
            //Perform the conversion in memory first, therefore we require a MemoryStream.
            using (MemoryStream destinationStream = new MemoryStream())
            {
                //Call the syncConverter class, passing in the name of the Word Automation Service for your Farm.
                SyncConverter sc = new SyncConverter(WASName);
                //Pass in your User Token or credentials under which this conversion job is executed.

                if (SPContext.Current != null)
                {
                    sc.UserToken = SPContext.Current.Site.UserToken;
                }

                sc.Settings.UpdateFields = true;

                //Save format
                sc.Settings.OutputFormat = SaveFormat.PDF;

                //Convert to PDF by opening the file stream, and then converting to the destination memory stream.
                ConversionItemInfo info = sc.Convert(listitem.File.OpenBinaryStream(), destinationStream);

                var filename = Path.GetFileNameWithoutExtension(listitem.File.Name) + ".pdf";
                if (info.Succeeded)
                {
                    //File conversion successful, then add the memory stream to the SharePoint list.
                    SPFile newfile = library.RootFolder.Files.Add(filename, destinationStream, true);
                }
                else if (info.Failed)
                {
                    throw new Exception(info.ErrorMessage);
                }
            }
//                }
//            }
        }