Upload
Maximum file size: 1 MB. Files accepted: doc, docx, txt, rtf.
Click here to browse files.fileerrors
Convert to
Source file:
filename
Target file type:
- Demo
- Java
- C# source
This demo shows you how to convert a Word document (doc/docx) to PDF, HTML, Image, XPS, RTF and other file format.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.ImageType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ConvertDemo {
public void convertDemo(String filePath, String convertTo, String resultFileName) throws IOException {
Document document = new Document();
document.loadFromFile(filePath);
convertion(document,convertTo,resultFileName);
}
private void convertion(Document doc, String convertTo, String resultFileName) throws IOException {
switch (convertTo) {
case "PDF":
doc.saveToFile(resultFileName+".pdf", FileFormat.PDF);
break;
case "XPS":
doc.saveToFile(resultFileName + ".xps", FileFormat.XPS);
break;
case "IMAGE":
BufferedImage[] images = doc.saveToImages(ImageType.Bitmap);
if (images != null && images.length > 0){
if (images.length == 1){
ImageIO.write(images[0],".PNG", new File(resultFileName+".png"));
}else {
for (int j = 0; j < images.length;j++){
String fileName = String.format("image-{0}.png",j);
ImageIO.write(images[j],".PNG",new File(fileName));
}
}
}
break;
case "RTF":
doc.saveToFile(resultFileName+".rtf", FileFormat.Rtf);
break;
case "TIFF":
doc.saveToTiff(resultFileName +".tiff");
break;
case "HTML":
doc.getHtmlExportOptions().setImageEmbedded(true);
doc.getHtmlExportOptions().setCssStyleSheetType(CssStyleSheetType.Internal);
doc.saveToFile(resultFileName+".html", FileFormat.Html);
break;
}
}
}
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Doc;
using Spire.Doc.Documents;
namespace DemoOnlineCode
{
class Convertors
{
public void demoConversion(String docFile, string format)
{
Document document = new Document(docFile, FileFormat.Auto);
ConvertFormat(document, format);
}
private void ConvertFormat(Document document, string format)
{
switch (format)
{
case "PDF":
document.SaveToFile("demo.pdf", FileFormat.PDF);
break;
case "XPS":
document.SaveToFile("demo.xps", FileFormat.XPS);
break;
case "Image":
Image[] Images = document.SaveToImages(ImageType.Bitmap);
if (Images != null && Images.Length > 0)
{
if (Images.Length == 1)
{
Images[0].Save("demo.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
else
{
for (int i = 0; i < Images.Length; i++)
{
String fileName = String.Format("Image-{0}.png", i);
Images[i].Save(fileName, ImageFormat.Png);
}
}
}
break;
case "RTF":
document.SaveToFile("demo.rtf", FileFormat.Rtf);
break;
case "TIFF":
Image[] TiffImage = document.SaveToImages(ImageType.Bitmap);
Image result = JoinTiffImages(TiffImage, EncoderValue.CompressionLZW);
result.Save("demo.tiff", ImageFormat.Tiff);
break;
case "HTML":
document.HtmlExportOptions.CssStyleSheetType = CssStyleSheetType.Internal;
document.HtmlExportOptions.ImageEmbedded = true;
document.SaveToFile("demo.html", FileFormat.Html);
break;
}
}
private static Image JoinTiffImages(Image[] images, EncoderValue compressEncoder)
{
//use the save encoder
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
Image pages = null;
int frame = 0;
ImageCodecInfo info = GetEncoderInfo("image/tiff");
foreach (Image img in images)
{
if (frame == 0)
{
pages = img;
pages.Save("demo.tiff", info, ep);
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(img, ep);
}
if ((frame == images.Length - 1) && frame != 0)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
return pages;
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
}
}
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the function you want, please request a free demo from us.
