C # converting PDF Images

Source: Internet
Author: User

Recently, due to the fact that writing documents is very confidential, it is ORC image and text recognition to prevent others from being copied. Here we are going to design an old talk about the problem, that is, to make the image format and add interference codes, sine, cosine, noise, and reflective Arc are all added to achieve our goal. Some people have said that Baidu Library, douding, and other methods are used before, but think that they are still not completely handled. At least I have obtained their source files in dozens of ways. The document is written in word, so our idea is to convert word into images, and then consider the complexity of implementation, and those conversion software on the Internet cannot meet our needs. Then I realized that I had to write a program to implement it myself. The first step is to use the file in pdf format that comes with the word itself, and then use the interface provided by Adobe's official Acrobat to implement this function. Although this program is very simple, it is not clearly expressed by words. Now let's go to the code. Comrades have read it!

/// <summary>
/// Convert a PDF document to an image. You can call this method as follows: convert00002image ("F: \ A.pdf", "F :\\", "A", 0, 0, null, 0 );
/// Because most parameters have default values, startPageNum defaults to 1, and endPageNum defaults to the total number of pages,
/// ImageFormat: The default value is ImageFormat. Jpeg and resolution. The default value is 1.
/// </summary>
/// <Param name = "pdfInputPath"> pdf file path </param>
/// <Param name = "imageOutputPath"> image output path </param>
/// <Param name = "imageName"> name of the image without the extension </param>
/// <Param name = "startPageNum"> the conversion starts from the page number of the PDF document. The default value is 1. </param>
/// <Param name = "endPageNum"> the conversion starts from the page number of the PDF document. The default value is the total number of PDF pages. </param>
/// <Param name = "imageFormat"> set the image format </param>
/// <Param name = "resolution"> set the image resolution. The larger the number, the clearer it is. The default value is 1. </param>
publicstaticvoid ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution)
{
Acrobat.CAcroPDDoc pdfDoc = null;
Acrobat.CAcroPDPage pdfPage = null;
Acrobat.CAcroRect pdfRect = null;
Acrobat.CAcroPoint pdfPoint = null;
 
// Create the document (Can only create the AcroExch.PDDoc object using late-binding)
// Note using VisualBasic helper functions, have to add reference to DLL
pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
 
// validate parameter
if (!pdfDoc.Open(pdfInputPath)) { thrownew FileNotFoundException(); }
if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
if (startPageNum <= 0) { startPageNum = 1; }     if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); }     if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; }
if (resolution <= 0) { resolution = 1; }
 
// start to convert each page
for (int i = startPageNum; i <= endPageNum; i++)
{
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
 
int imgWidth = (int)((double)pdfPoint.x * resolution);
int imgHeight = (int)((double)pdfPoint.y * resolution);
 
pdfRect.Left = 0;
pdfRect.right = (short)imgWidth;
pdfRect.Top = 0;
pdfRect.bottom = (short)imgHeight;
 
// Render to clipboard, scaled by 100 percent (ie. original size)
// Even though we want a smaller image, better for us to scale in .NET
// than Acrobat as it would greek out small text
pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution));
 
IDataObject clipboardData = Clipboard.GetDataObject();
 
if (clipboardData.GetDataPresent(DataFormats.Bitmap))
{
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
pdfBitmap.Save(Path.Combine(imageOutputPath, imageName) + ".jpg", imageFormat);
pdfBitmap.Dispose();
}
}
 
pdfDoc.Close();
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);
Marshal.ReleaseComObject(pdfPoint);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.