Showing posts with label Convert. Show all posts
Showing posts with label Convert. Show all posts

Friday, 16 December 2011

Convert docx to pdf document in SharePoint 2010 using Word Automation Services

Word Automation Services is a new feature available in SharePoint 2010. It supports converting Word documents to other formats. Here we are going to convert the word document into pdf document. In the API for Word Automation Services there are different ways to convert documents including AddFile(), AddFolder() and AddLibrary() methods. Here we will be using AddFile() method to convert the word document (this method will convert the word document within the same list).
This article describes the following steps to show how to call the Word Automation Services to convert a document:




  • Add a word document to the SharePoint Shared Documents.

  • Create a console Application.

  • Add a reference to the Microsoft.Office.Word.Server assembly.


Add a word document in the Shared Documents:
Create a console application:


  • Open Visual Studio 2010.

  • Go to File => New => Project.

  • Select the Console Application template and enter the Name.

  • Click Ok.

  • Add a reference to the Microsoft.Office.Word.Server assembly (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\ Microsoft.Office.Word.Server.dll)

  • Open Program.cs file and replace the code with the following.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Word.Server.Conversions;
using Microsoft.SharePoint;

namespace Test
{
class Program
{
static void Main(string[] args)
{
using (SPSite spSite = new SPSite( "http://servername:4040/"))
{
//Defines a collection of settings for all conversions within a single conversion job.
ConversionJobSettings jobSettings=new ConversionJobSettings();
jobSettings.OutputFormat = SaveFormat.PDF;
//Represents a collection of file conversions (of single files or entire libraries) that share a common set of conversion properties and are
logically tracked as a single unit.
ConversionJob pdfConversion = new ConversionJob("Word Automation Services", jobSettings);
// Set the credentials to use when running the conversion job.
pdfConversion.UserToken = spSite.UserToken;
pdfConversion.AddFile("http://servername:4040/Shared%20Documents/ExternalList.docx",
"http://servername:4040/Shared%20Documents/ExternalList.pdf");
pdfConversion.Start();
}
}
}
}

 

Programmatically Convert Office Documents (.docx, .pptx, .pub) into PDF

Word

========

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.Word;

 

namespace DocConverter

{

class Program

{

static void Main(string[] args)

{

ApplicationClass wordApplication = new ApplicationClass();

 

Document wordDocument = null;

object paramSourceDocPath = @"D:\Fun\Test.docx";

object paramMissing = Type.Missing;

string paramExportFilePath = @"D:\Fun\Test.pdf";

WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;

bool paramOpenAfterExport = false;

WdExportOptimizeFor paramExportOptimizeFor =

WdExportOptimizeFor.wdExportOptimizeForPrint;

WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

int paramStartPage = 0;

int paramEndPage = 0;

WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;

bool paramIncludeDocProps = true;

bool paramKeepIRM = true;

WdExportCreateBookmarks paramCreateBookmarks =

WdExportCreateBookmarks.wdExportCreateWordBookmarks;

bool paramDocStructureTags = true;

bool paramBitmapMissingFonts = true;

bool paramUseISO19005_1 = false;

try

{

// Open the source document.

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);

 

// Export it in the specified format.

if (wordDocument != null)

wordDocument.ExportAsFixedFormat(paramExportFilePath,

paramExportFormat, paramOpenAfterExport,

paramExportOptimizeFor, paramExportRange, paramStartPage,

paramEndPage, paramExportItem, paramIncludeDocProps,

paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,

paramBitmapMissingFonts, paramUseISO19005_1,

ref paramMissing);

}

catch (Exception ex)

{

// Respond to the error

}

finally

{

// Close and release the Document object.

if (wordDocument != null)

{

wordDocument.Close(ref paramMissing, ref paramMissing,

ref paramMissing);

wordDocument = null;

}

 

// Quit Word and release the ApplicationClass object.

if (wordApplication != null)

{

wordApplication.Quit(ref paramMissing, ref paramMissing,

ref paramMissing);

wordApplication = null;

}

 

GC.Collect();

GC.WaitForPendingFinalizers();

GC.Collect();

GC.WaitForPendingFinalizers();

}

 

}

}

}

 

PowerPoint

=============

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.PowerPoint;

namespace PowerPointConverter

{

class Program

{

static void Main(string[] args)

{

Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();

Microsoft.Office.Interop.PowerPoint.Presentation presentation = ppApp.Presentations.Open(@"D:\Fun\Test.pptx", Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

presentation.ExportAsFixedFormat(@"D:\Fun\Test.xps",

PpFixedFormatType.ppFixedFormatTypeXPS,

PpFixedFormatIntent.ppFixedFormatIntentPrint,

Microsoft.Office.Core.MsoTriState.msoFalse,

PpPrintHandoutOrder.ppPrintHandoutHorizontalFirst,

PpPrintOutputType.ppPrintOutputSlides,

Microsoft.Office.Core.MsoTriState.msoFalse,

null,

PpPrintRangeType.ppPrintAll,

"",

false,

false,

false,

true,

true,

System.Reflection.Missing.Value);

presentation.Close();

presentation = null;

ppApp = null;

GC.Collect();

 

}

}

}

 

Publisher
===============

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.Publisher;

using System.Reflection;

 

namespace PublisherConverter

{

class Program

{

static void Main(string[] args)

{

Microsoft.Office.Interop.Publisher.Application pbApp = new Microsoft.Office.Interop.Publisher.Application();

Microsoft.Office.Interop.Publisher.Document pbDoc = pbApp.Open(@"D:\Fun\Test4.pub", true, true, PbSaveOptions.pbDoNotSaveChanges);

pbDoc.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, @"D:\Fun\Test.pdf", PbFixedFormatIntent.pbIntentPrinting, true, 300, 450, 1200, 1800, 1, pbDoc.Pages.Count, 1, false, PbPrintStyle.pbPrintStyleDefault, false, false, false, System.Reflection.Missing.Value);

pbDoc.Close();

pbDoc = null;

pbApp = null;

GC.Collect();

}

}

}

Convert Xml document to String

    Default.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="Literal1" runat="server" Mode="Encode"></asp:Literal></div>
</form>
</body>
</html>

Default.aspx.cs :

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<deneme/>");
#region just to remind myself something
XmlNode node = doc.CreateElement("serkan");
doc.DocumentElement.AppendChild(node);
#endregion
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
doc.WriteTo(tx);
// i use literal control because when you set its mode to "encode", you can write xml tags to browser window as they are
Literal1.Text = sw.ToString();

}
}

Monday, 15 February 2010

Convert Bookmarks or Favorites to html or text

I have a couple of folders of internet explorer bookmarks / favorites, and I'd like to post them to a web page so that others can view the text description and click thru on them. Is there a free / quick / simple way to do this?

Ans:  From the IE menu:

File -> Import and Export -> Next -> Export Favorites