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
;
public int ConvertPtoI(string filenamesource
, int pagesource
)
{
filename
= filenamesource
;
page
= pagesource
;
if(!(filename
.EndsWith(".pdf") || filename
.EndsWith(".PDF")))
{
return 0;
}
try
{
Acrobat.CAcroPDDoc pdfDoc
;
pdfDoc
= (Acrobat
.CAcroPDDoc
)Microsoft
.VisualBasic
.Interaction
.CreateObject("AcroExch.PDDoc", "");
bool ret
= pdfDoc
.Open(filename
);
if(!ret
)
{
return 1;
}
int pageCount
= pdfDoc
.GetNumPages();
if(page
>= pageCount
|| page
<= 0)
{
return 2;
}
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
;
pdfPage
.CopyToClipboard(pdfRect
, 0, 0, 100);
IDataObject clipboardData
= Clipboard
.GetDataObject();
if (clipboardData
.GetDataPresent(DataFormats
.Bitmap
))
{
Bitmap pdfBitmap
= (Bitmap
)clipboardData
.GetData(DataFormats
.Bitmap
);
int thumbnailWidth
= 1000;
int thumbnailHeight
= 1000;
Image pdfImage
= pdfBitmap
.GetThumbnailImage(thumbnailWidth
, thumbnailHeight
,null, IntPtr
.Zero
);
Bitmap thumbnailBitmap
= new Bitmap(thumbnailWidth
+ 7, thumbnailHeight
+ 7,System
.Drawing
.Imaging
.PixelFormat
.Format32bppArgb
);
using (Graphics thumbnailGraphics
= Graphics
.FromImage(thumbnailBitmap
))
{
thumbnailGraphics
.DrawImage(pdfImage
, 2, 2, thumbnailWidth
, thumbnailHeight
);
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();
Marshal
.ReleaseComObject(pdfPage
);
Marshal
.ReleaseComObject(pdfRect
);
Marshal
.ReleaseComObject(pdfDoc
);
}
}
catch (System.Exception ex
)
{
return 10;
}
return 5;
}
}
}
转载请注明原文地址:https://tech.qufami.com/read-24011.html