最近在网上查了很多资料,刚开始的做的不是太好,感觉实现起来很困难。
那个pdf转换成图片用C++相对来说很麻烦,因为它要调用一起其他的库文件,使用起来不太好办,最近有点忙也没整理这个。但C#和Java的现成的程序都有,用C++的有,是调用Acrobat中的文件自己编写的,但不是生成图片,只是用VC显示,网址是:
http://www.evget.com/zh-CN/Info/catalog/6594.html
有个用C#实现的网址是:
http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx
这里面有具体的步骤及如何调用别人的库,但C++使用起来不太好办。
这里有一个pdflib的操作,可以操作页,也是C#的:
http://www.codeproject.com/KB/graphics/AnotherPDFLib.aspx
下面还有用C++如何调用C#的:
http://blog.csdn.net/suoxd123/archive/2010/01/08/5157668.aspx
其实我建议直接显示PDf页,并在上面直接操作,不用再转成图片,如果转成图片也很麻烦。但用C++语言做起来不太好做。
后来在做了一些研究后,发现利用C#做很容易,利用的是其他人的程序,http://www.codeproject.com/KB/GDI-plus/pdfthumbnail.aspx,在这样上面可以利用Acrobat将pdf转换成图片。
我将程序进行了修改,如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Acrobat;
namespace PDFLibSharp
{
public class PDFLibSharp
{
private string filename;
int page;
/// <summary>
/// the construct function to create the filename and page
/// </summary>
/// <param name="filenamesource"></param>
/// <param name="pagesource"></param>
public int ConvertPtoI(string filenamesource, int pagesource)
{
filename = filenamesource;
page = pagesource;
//assert the file is pdf or not
if(!(filename.EndsWith(".pdf") || filename.EndsWith(".PDF")))
{
return 0; //0 represent there is no file
}
//begin to change
try
{
Acrobat.CAcroPDDoc pdfDoc;
pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
bool ret = pdfDoc.Open(filename);
if(!ret)
{
return 1; //1 can not open to read
}
//the page is too big
int pageCount = pdfDoc.GetNumPages();
if(page >= pageCount || page <= 0)
{
return 2;
}
//get the page and the size
Acrobat.CAcroPDPage pdfPage;
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(page);
Acrobat.CAcroPoint pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
Acrobat.CAcroRect pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
pdfRect.Left = 0;
pdfRect.right = pdfPoint.x;
pdfRect.Top = 0;
pdfRect.bottom = pdfPoint.y;
//copy it to the clipboard
pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);
IDataObject clipboardData = Clipboard.GetDataObject();
if (clipboardData.GetDataPresent(DataFormats.Bitmap))
{
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
// Size of generated thumbnail in pixels
int thumbnailWidth = 1000;
int thumbnailHeight = 1000;
// Render to small image using the bitmap class
Image pdfImage = pdfBitmap.GetThumbnailImage(thumbnailWidth, thumbnailHeight,
null, IntPtr.Zero);
// Create new blank bitmap (+ 7 for template border)
Bitmap thumbnailBitmap = new Bitmap(thumbnailWidth + 7, thumbnailHeight + 7,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// To overlayout the template with the image, we need to set the transparency
// http://www.sellsbrothers.com/writing/default.aspx?content=dotnetimagerecoloring.htm
// templateBitmap.MakeTransparent();
using (Graphics thumbnailGraphics = Graphics.FromImage(thumbnailBitmap))
{
// Draw rendered pdf image to new blank bitmap
thumbnailGraphics.DrawImage(pdfImage, 2, 2, thumbnailWidth, thumbnailHeight);
// Draw template outline over the bitmap (pdf with show through the transparent area)
// thumbnailGraphics.DrawImage(templateBitmap, 0, 0);
// Save as .png file
string outputFile = filename.Substring(0, filename.Length - 4) + '_' + page.ToString() + ".png";
thumbnailBitmap.Save(outputFile, System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine("Generated thumbnail... {0}", outputFile);
}
pdfDoc.Close();
// Not sure how why it is to do this, but Acrobat is not the best behaved COM object
// see http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);
}
}
catch (System.Exception ex)
{
return 10;
}
return 5;
}
//
//
}
}
后面的一篇文章是利用C++调用这个类库,可以参考下一篇文章。