usingSpire.Doc;usingSpire.Doc.Documents;usingSystem;usingSystem.Drawing;usingSystem.Drawing.Imaging;namespaceconvert_word_to_tiff
{classProgram
{static void Main(string[] args)
{//实例化一个Document类,加载Word sample
Document document = new Document(@"C:\Users\Administrator\Desktop\sample.docx");//调用方法JoinTiffImages()将Word保存为tiff格式,并运行生成的文档
JoinTiffImages(SaveAsImage(document), "result.tiff", EncoderValue.CompressionLZW);
System.Diagnostics.Process.Start("result.tiff");
}//自定义方法SaveAsImage()将Word文档保存为图像
private staticImage[] SaveAsImage(Document document)
{
Image[] images=document.SaveToImages(ImageType.Bitmap);returnimages;
}private static ImageCodecInfo GetEncoderInfo(stringmimeType)
{
ImageCodecInfo[] encoders=ImageCodecInfo.GetImageEncoders();for (int j = 0; j < encoders.Length; j++)
{if (encoders[j].MimeType ==mimeType)returnencoders[j];
}throw new Exception(mimeType + "mime type not found in ImageCodecInfo");
}//自定义方法JoinTiffImages()将Word保存为TIFF图片格式(使用指定编码器和图像编码参数)
public static void JoinTiffImages(Image[] images, stringoutFile, EncoderValue compressEncoder)
{
System.Drawing.Imaging.Encoder enc=System.Drawing.Imaging.Encoder.SaveFlag;
EncoderParameters ep= new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)compressEncoder);
Image pages= images[0];int frame = 0;
ImageCodecInfo info= GetEncoderInfo("image/tiff");foreach (Image img inimages)
{if (frame == 0)
{
pages=img;
pages.Save(outFile, info, ep);
}else{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(img, ep);
}if (frame == images.Length - 1)
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
}
}
}