不通过虚拟打印机word转tif

 

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  Microsoft.Office.Interop.Word;
using  System.IO;
using  System.Reflection;
using  oWord  =  Microsoft.Office.Interop.Word;
using  System.Drawing.Imaging;
using  System.Runtime.Serialization.Formatters.Binary;


  
/// <summary>
        
/// 不通过虚拟打印程序WordToTif
        
/// </summary>
        
/// <param name="wordPath">word全路径</param>
        
/// <param name="outputFilePath">保存文件路径</param>
        
/// <param name="handle">Handle</param>

         public   static   void  PrintWortWithClipBoard( string  wordPath,  string  outputFilePath,IntPtr handle)
        
{
            oWord.ApplicationClass word 
= new oWord.ApplicationClass();
            Type wordType 
= word.GetType();
            
//打开WORD文档
            oWord.Documents docs = word.Documents;
            Type docsType 
= docs.GetType();
            
object objDocName = wordPath;
            oWord.Document doc 
= (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, truetrue });
           
            Microsoft.Office.Interop.Word.Range mg 
= doc.Content;
            mg.Select();
            mg.CopyAsPicture();
            System.Windows.Forms.IDataObject data 
= System.Windows.Forms.Clipboard.GetDataObject();
            Object obj 
= null;
            
if (data.GetDataPresent(DataFormats.MetafilePict))
            
{
                obj 
= data.GetData(DataFormats.MetafilePict);
                Metafile mf 
= ClipboardMetafileHelper.GetEnhMetafileOnClipboard(handle);
                Image img 
= mf;

             
                img.Save(@outputFilePath, ImageFormat.Tiff);
            }

            
//退出WORD
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }

        
private   static  ImageCodecInfo GetEncoderInfo(String mimeType)
        
{
            
int j;
            ImageCodecInfo[] encoders;
            encoders 
= ImageCodecInfo.GetImageEncoders();
            
for (j = 0; j < encoders.Length; ++j)
            
{
                
if (encoders[j].MimeType == mimeType)
                    
return encoders[j];
            }

            
return null;
        }
 

 

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Drawing.Imaging;
using  System.Runtime.InteropServices;

namespace  ThreadStudy
{
    
class ClipboardMetafileHelper
    
{
        [DllImport(
"user32.dll")]
        
static extern bool OpenClipboard(IntPtr hWndNewOwner);
        [DllImport(
"user32.dll")]
        
static extern bool EmptyClipboard();
        [DllImport(
"user32.dll")]
        
static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
        [DllImport(
"user32.dll")]
        
static extern bool CloseClipboard();
        [DllImport(
"gdi32.dll")]
        
static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, IntPtr hNULL);
        [DllImport(
"gdi32.dll")]
        
static extern bool DeleteEnhMetaFile(IntPtr hemf);

        
// Metafile mf is set to an invalid state inside this function
        static public bool PutEnhMetafileOnClipboard(IntPtr hWnd, Metafile mf)
        
{
            
bool bResult = false;
            IntPtr hEMF, hEMF2;
            hEMF 
= mf.GetHenhmetafile(); // invalidates mf
            if (!hEMF.Equals(new IntPtr(0)))
            
{
                hEMF2 
= CopyEnhMetaFile(hEMF, new IntPtr(0));
                
if (!hEMF2.Equals(new IntPtr(0)))
                
{
                    
if (OpenClipboard(hWnd))
                    
{
                        
if (EmptyClipboard())
                        
{
                            IntPtr hRes 
= SetClipboardData(14 /**//*CF_ENHMETAFILE*/, hEMF2);
                            bResult 
= hRes.Equals(hEMF2);
                            CloseClipboard();
                        }

                    }

                }

                DeleteEnhMetaFile(hEMF);
            }

            
return bResult;
        }

        
private const int CF_ENHMETAFILE = 14;
        [DllImport(
"user32.dll")]
        
private static extern int IsClipboardFormatAvailable(int wFormat);
        [DllImport(
"user32.dll")]
        
private static extern IntPtr GetClipboardData(int wFormat);

        
static public Metafile GetEnhMetafileOnClipboard(IntPtr hWnd)
        
{
            Metafile meta 
= null;
            
if (OpenClipboard(hWnd))
            
{
                
try
                
{
                    
if (IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0)
                    
{
                        IntPtr hEMF 
= GetClipboardData(CF_ENHMETAFILE);
                        meta 
= new Metafile(hEMF, true);
                    }

                }

                
finally
                
{
                    CloseClipboard();
                }

            }

            
return meta;
        }



    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值