brief introduction
Function: PDF documents are converted into one picture per page, one picture into one PDF, and multiple PDFs are integrated into one multi page PDF document.
Experience: there are always various problems in the search of various websites, especially in the version of the cited version. Either the current method can't be applied or the watermark label appears. Finally, with the help of the big guys, a relatively complete code (mainly to meet the needs) is finally completed.
background
Previously, we met a demand in the project: to upload multi page PDF and turn it into a picture display, it is required to arrange and view one page by one (for example, figure 1), and it is required to be a PDF document when it is transmitted to the service center platform for viewing (the service center platform only provides download).
Click to download the required dll file
O2S.Components.PDFRender4NET.dll version: 4.7.4.0
itextsharp.dll version: 5.5.10.0
The main codes are as follows
/// <summary> /// take PDF How to convert a document to a picture /// </summary> /// <param name="pdfInputPath"></param> /// <param name="desPath">Output relative path</param> /// <param name="definition">Set the sharpness of the picture, the bigger the number, the clearer</param> /// <returns></returns> public static List<string> ConvertPDF2Image(string pdfInputPath, string desPath, Definition definition, string title) { List<string> imgList = new List<string>(); PDFFile pdfFile = PDFFile.Open(pdfInputPath); int startPageNum = 1, endPageNum = pdfFile.PageCount; int number = 1; for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); string filePath = desPath + title + "-" + number + ".jpg"; imgList.Add(filePath); pageImage.Save(System.Web.HttpContext.Current.Server.MapPath(filePath)); number++; } pdfFile.Dispose(); return imgList; } public enum Definition { One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10 } /// <summary> /// merge PDF /// </summary> /// <param name="fileList">Absolute path set</param> /// <param name="outMergeFile">The absolute address path of the merged file exists</param> public static void mergePdfFiles(List<string> fileList, string outMergeFile) { PdfReader reader; //The purpose of extracting content from text into the file stream here is to avoid file occupation,Cannot delete FileStream fs1 = new FileStream(fileList[0], FileMode.Open); byte[] bytes1 = new byte[(int)fs1.Length]; fs1.Read(bytes1, 0, bytes1.Length); fs1.Close(); reader = new PdfReader(bytes1); reader.GetPageSize(1); // iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(1000,800);//Set style iTextSharp.text.Rectangle rec = reader.GetPageSize(1); float width = rec.Width; float height = rec.Height; //Create a document variable iTextSharp.text.Document document = new iTextSharp.text.Document(rec, 50, 50, 50, 50); //Create the document PdfWriter pdfWrite = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create)); //open documents document.Open(); //Add content PdfContentByte contentByte = pdfWrite.DirectContent; PdfImportedPage newPage; for (int i = 0; i < fileList.Count; i++) { FileStream fs = new FileStream(fileList[i], FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); reader = new PdfReader(bytes); int pageNum = reader.NumberOfPages;//Get document pages for (int j = 1; j <= pageNum; j++) { document.NewPage(); newPage = pdfWrite.GetImportedPage(reader, j); contentByte.AddTemplate(newPage, 0, 0); } } document.Close(); }
/// <summary> /// Picture turn PDF /// </summary> /// <param name="imagepath">Picture location (Jedi path)</param> /// <param name="pdfpath">Deposit PDF Address (Jedi path)</param> public static void iTextSharpCreatPDF(string imagepath, string pdfpath) { iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath); float percentage = 1; //Here are the original width and height of the picture float resizedWidht = image.Width; float resizedHeight = image.Height; Document doc = new Document(new iTextSharp.text.Rectangle(resizedWidht, resizedHeight), 0, 0, 0, 0); //new Rectangle(1000,1000) //Specifies that the file is scaled to 100 when it is opened by default% //PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f); try { PdfWriter.GetInstance(doc, new FileStream(pdfpath, FileMode.Create)); doc.Open(); #region Next, operate on the picture ////At this time, determine whether the image width is greater than the page width minus the margin. If so, reduce it. If it is still large, continue to reduce it, ////So the percentage reduction percentage It's going to get smaller and smaller while (resizedWidht > (doc.PageSize.Width - doc.LeftMargin - doc.RightMargin)) { percentage = percentage * 0.9f; resizedHeight = image.Height * percentage; resizedWidht = image.Width * percentage; } #region Notes ////There is a 0.8 here. If the height of the image is too close to the page size height, ////the image will seem so big //while (resizedHeight > (doc.PageSize.Height - doc.TopMargin - doc.BottomMargin) * 0.8) //{ // percentage = percentage * 0.9f; // resizedHeight = image.Height * percentage; // resizedWidht = image.Width * percentage; //} #endregion ////Here we use the calculated percentage to reduce the picture image.ScalePercent(percentage * 100); //Make the center of the picture coincide with the center of the page image.SetAbsolutePosition(doc.PageSize.Width / 2 - resizedWidht / 2, doc.PageSize.Height / 2 - resizedHeight / 2); doc.Add(image); #endregion } catch (DocumentException dex) { System.Web.HttpContext.Current.Response.Write(dex.Message); } catch (IOException ioex) { System.Web.HttpContext.Current.Response.Write(ioex.Message); } catch (Exception ex) { System.Web.HttpContext.Current.Response.Write(ex.Message); } finally { doc.Close(); } }