最近在網上查了很多資料,剛開始的做的不是太好,感覺實現起來很困難。
那個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++調用這個類庫,可以參考下一篇文章。