在线 word to html5,C# web实现word 转Html、office转Html、pdf转图片 在线预览文件

pdf  转图片

说明:本方法 用acrobat 的官方接口  需要安装  acrobat   professional  8.0或更高版本

然后再vs中添加com组件  abode  acrobat  版本号   type  library  只有安装了上面软件 才回有

在web页 头部 添加   AspCompat="true"    控制台程序 要在main函数上加  [STAThread]  保证单线程模型下 才能访问 剪贴板

    using System.IO;

    using System.Reflection;

    using DataHelp;

    using System.Text;

    using System.Drawing.Imaging;

    using System.Windows.Forms;

    using System.Drawing;

    using System.Runtime.InteropServices;

    /// 

/// Pdf导图片

/// 

/// 

/// 

/// 

/// 

public string PDFToPic(string pdfInputPathDirectory, string pngOutputPath, ImageFormat format)

{

StringBuilder b = new StringBuilder();

ImageFormat formate = format;

// Acrobat objects

Acrobat.CAcroPDDoc pdfDoc = null;

Acrobat.CAcroPDPage pdfPage = null;

Acrobat.CAcroRect pdfRect = null;

Acrobat.CAcroPoint pdfPoint = null;

try

{

string[] files = new string[] { pdfInputPathDirectory };

string dic = Path.GetDirectoryName(pdfInputPathDirectory);

//string[] files = Directory.GetFiles(dic, "*.pdf");

for (int n = 0; n 

{

string inputFile = files[n].ToString();

string fileName = files[n].Substring(files[n].LastIndexOf(@"\") + 1).Replace(".pdf", "");

// Will always be available as .NET framework ships with all

pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");

bool ret = pdfDoc.Open(inputFile);

if (!ret)

{

throw new FileNotFoundException();

}

else

{

b = b.AppendLine("pdfDoc成功打开文件!!!");

}

// Get the number of pages (to be used later if you wanted to store that information)

int pageCount = pdfDoc.GetNumPages();

b = b.AppendLine("

  • ");

for (int i = 0; i 

{

// Get the first page

pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i);

pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();

b = b.AppendLine("

   pdfPoint.x=" + pdfPoint.x + ";pdfPoint.y=" + pdfPoint.y + "   ");

pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

pdfRect.Left = 0;

pdfRect.right = pdfPoint.x;

pdfRect.Top = 0;

pdfRect.bottom = pdfPoint.y;

pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);

System.Windows.Forms.IDataObject clipboardData = Clipboard.GetDataObject();

bool c = false;

if (clipboardData!=null )

{

c = true;

}

b = b.AppendLine("

    Clipboard.GetDataObject()=" + c + "剪贴板中是否有数据;   ");

b = b.AppendLine("

   clipboardData.GetDataPresent(DataFormats.Bitmap)=" + clipboardData.GetDataPresent(DataFormats.Bitmap) + ";   ");

if (clipboardData.GetDataPresent(DataFormats.Bitmap))

{

Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);

b = b.AppendLine("

   生成图片的大小: pdfBitmap.Size.Height=" + pdfBitmap.Size.Height + "; pdfBitmap.Size.Width=" + pdfBitmap.Size.Width + "   ");

string dPath = Path.Combine(pngOutputPath, i.ToString("0000") + "." + formate.ToString());

                b = b.AppendLine("

   生成图片的路径:" + dPath + ";   ");

                pdfBitmap.Save(dPath, formate);

pdfBitmap.Dispose();

}

b = b.AppendLine("

          第" + (i + 1) + "页");

}

pdfDoc.Close();

Marshal.ReleaseComObject(pdfPage);

Marshal.ReleaseComObject(pdfRect);

Marshal.ReleaseComObject(pdfDoc);

}

b = b.AppendLine("

");

}

catch (System.Exception ex)

{

Response.Write(ex.ToString());

}

return b.ToString();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值