tab 1
此 demo 展示如何将 word 文档 (doc/docx) 转换为 pdf、html、image、xps、rtf 和 tiff 文件格式。
如果这不是您想要的 demo,您可以通过填写表格获取免费定制 demo。
如您有与我们产品相关的其他技术问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。;销售相关的问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。。
tab 2
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.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");
}
}
}
tab 3
imports system.drawing
imports system.drawing.imaging
imports spire.doc
imports spire.doc.documents
namespace demoonlinecode
class convertors
public sub democonversion(docfile as [string], format as string)
dim document as new document(docfile, fileformat.auto)
convertformat(document, format)
end sub
private sub convertformat(document as document, format as string)
select case format
case "pdf"
document.savetofile("demo.pdf", fileformat.pdf)
exit select
case "xps"
document.savetofile("demo.xps", fileformat.xps)
exit select
case "image"
dim images as image() = document.savetoimages(imagetype.bitmap)
if images isnot nothing andalso images.length > 0 then
if images.length = 1 then
images(0).save("demo.bmp", system.drawing.imaging.imageformat.bmp)
else
for i as integer = 0 to images.length - 1
dim filename as [string] = [string].format("image-{0}.png", i)
images(i).save(filename, imageformat.png)
next
end if
end if
exit select
case "rtf"
document.savetofile("demo.rtf", fileformat.rtf)
exit select
case "tiff"
dim tiffimage as image() = document.savetoimages(imagetype.bitmap)
dim result as image = jointiffimages(tiffimage, encodervalue.compressionlzw)
result.save("demo.tiff", imageformat.tiff)
exit select
case "html"
document.savetofile("demo.html", fileformat.html)
exit select
end select
end sub
private shared function jointiffimages(images as image(), _
compressencoder as encodervalue) as image
'use the save encoder
dim enc as encoder = encoder.saveflag
dim ep as new encoderparameters(2)
ep.param(0) = new encoderparameter(enc, clng(encodervalue.multiframe))
ep.param(1) = new encoderparameter(encoder.compression, clng(compressencoder))
dim pages as image = nothing
dim frame as integer = 0
dim info as imagecodecinfo = getencoderinfo("image/tiff")
for each img as image in images
if frame = 0 then
pages = img
pages.save("demo.tiff", info, ep)
else
'save the intermediate frames
ep.param(0) = new encoderparameter(enc, clng(encodervalue.framedimensionpage))
pages.saveadd(img, ep)
end if
if (frame = images.length - 1) andalso frame <> 0 then
'flush and close.
ep.param(0) = new encoderparameter(enc, clng(encodervalue.flush))
pages.saveadd(ep)
end if
frame = 1
next
return pages
end function
private shared function getencoderinfo(mimetype as string) as imagecodecinfo
dim encoders as imagecodecinfo() = imagecodecinfo.getimageencoders()
for j as integer = 0 to encoders.length - 1
if encoders(j).mimetype = mimetype then
return encoders(j)
end if
next
throw new exception(mimetype & " mime type not found in imagecodecinfo")
end function
end class
end namespace