public static void Run() { // ExStart:ConvertPDFPages // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_Images(); // Create PdfConverter object PdfConverter objConverter = new PdfConverter(); // Bind input pdf file objConverter.BindPdf(dataDir + "ConvertPDFPages.pdf"); // Initialize the converting process objConverter.DoConvert(); objConverter.CoordinateType = PageCoordinateType.CropBox; // Check if pages exist and then convert to image one by one while (objConverter.HasNextImage()) { objConverter.GetNextImage(dataDir + DateTime.Now.Ticks.ToString() + "_out_.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } // Close the PdfConverter object objConverter.Close(); // ExEnd:ConvertPDFPages }
public static void Main() { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); // instantiate PdfPageEditor class to get particular page region Aspose.Pdf.Facades.PdfPageEditor editor = new Aspose.Pdf.Facades.PdfPageEditor(); // bind the source PDF file editor.BindPdf(dataDir + "SampleInput.pdf"); // move the origin of PDF file to particular point editor.MovePosition(0, 700); // create a memory stream object MemoryStream ms = new MemoryStream(); // save the updated document to stream object editor.Save(ms); //create PdfConverter object PdfConverter objConverter = new PdfConverter(); //bind input pdf file objConverter.BindPdf(ms); //set StartPage and EndPage properties to the page number to //you want to convert images from objConverter.StartPage = 1; objConverter.EndPage = 1; //Counter int page = 1; //initialize the converting process objConverter.DoConvert(); //check if pages exist and then convert to image one by one while (objConverter.HasNextImage()) objConverter.GetNextImage(dataDir+ "Specific_Region-Image" + page++ + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg); //close the PdfConverter object objConverter.Close(); // close MemoryStream object holding the updated document ms.Close(); }
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_Images(); //create PdfConverter object PdfConverter objConverter = new PdfConverter(); //bind input pdf file objConverter.BindPdf(dataDir+ "ConvertPDFPages.pdf"); //initialize the converting process objConverter.DoConvert(); objConverter.CoordinateType = PageCoordinateType.CropBox; //check if pages exist and then convert to image one by one while (objConverter.HasNextImage()) objConverter.GetNextImage(dataDir+ DateTime.Now.Ticks.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //close the PdfConverter object objConverter.Close(); }
internal void DoSplit() { string[] files = Directory.GetFiles(sPath, "*.pdf", SearchOption.AllDirectories); int fileCount = files.Length; int fileCounter = 0; foreach (string sourceFile in files) { Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(sourceFile); //Create a PdfConverter object PdfConverter converter = new PdfConverter(); //Bind the input PDF file converter.BindPdf(sourceFile); // Specify the start page to be processed converter.StartPage = 1; // Specify the end page for processing converter.EndPage = pdfDocument.Pages.Count; // Create a Resolution object to specify the resolution of resultant image converter.Resolution = new Aspose.Pdf.Devices.Resolution(300); //Initialize the convertion process converter.DoConvert(); string docPath = Path.Combine(Path.GetDirectoryName(sourceFile), Path.GetFileNameWithoutExtension(sourceFile) + ".docx"); Aspose.Words.Document doc = new Aspose.Words.Document(); DocumentBuilder builder = new DocumentBuilder(doc); int pageCount = 0; //Check if pages exist and then convert to image one by one while (converter.HasNextImage()) { pageCount++; string filePath = Path.Combine(Path.GetDirectoryName(sourceFile), Path.GetFileNameWithoutExtension(sourceFile) + "_" + pageCount.ToString() + ".png"); using (MemoryStream imageStream = new MemoryStream()) { converter.GetNextImage(imageStream, System.Drawing.Imaging.ImageFormat.Png); imageStream.Position = 0; System.IO.File.WriteAllBytes(filePath, imageStream.ToArray()); } builder.InsertImage(filePath); } // Close the PdfConverter instance and release the resources doc.Save(docPath); converter.Close(); // Close the stream holding the image object fileCounter++; Program.mainWindow.updateProgress(Convert.ToInt32(Math.Floor(Convert.ToDouble(fileCounter) * (100 / Convert.ToDouble(fileCount))))); } Program.mainWindow.updateProgress(100); }
public static void Run() { // ExStart:ConvertPageRegion // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_Images(); // Instantiate PdfPageEditor class to get particular page region Aspose.Pdf.Facades.PdfPageEditor editor = new Aspose.Pdf.Facades.PdfPageEditor(); // Bind the source PDF file editor.BindPdf(dataDir + "Convert-PageRegion.pdf"); // Move the origin of PDF file to particular point editor.MovePosition(0, 700); // Create a memory stream object MemoryStream ms = new MemoryStream(); // Save the updated document to stream object editor.Save(ms); // Create PdfConverter object PdfConverter objConverter = new PdfConverter(); // Bind input pdf file objConverter.BindPdf(ms); // Set StartPage and EndPage properties to the page number to // You want to convert images from objConverter.StartPage = 1; objConverter.EndPage = 1; // Counter int page = 1; // Initialize the converting process objConverter.DoConvert(); // Check if pages exist and then convert to image one by one while (objConverter.HasNextImage()) { objConverter.GetNextImage(dataDir + "Specific_Region-Image" + page++ + "_out_.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg); } // Close the PdfConverter object objConverter.Close(); // Close MemoryStream object holding the updated document ms.Close(); // ExEnd:ConvertPageRegion }
public static void Main() { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); //create PdfConverter object PdfConverter objConverter = new PdfConverter(); //bind input pdf file objConverter.BindPdf(dataDir+ "input.pdf"); //initialize the converting process objConverter.DoConvert(); objConverter.ShowHiddenAreas = true; //check if pages exist and then convert to image one by one while (objConverter.HasNextImage()) objConverter.GetNextImage(dataDir+ DateTime.Now.Ticks.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //close the PdfConverter object objConverter.Close(); }
public static void Main() { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); // instantiate PdfPageEditor class to get particular page region Aspose.Pdf.Facades.PdfPageEditor editor = new Aspose.Pdf.Facades.PdfPageEditor(); // bind the source PDF file editor.BindPdf(dataDir + "SampleInput.pdf"); // move the origin of PDF file to particular point editor.MovePosition(0, 700); // create a memory stream object MemoryStream ms = new MemoryStream(); // save the updated document to stream object editor.Save(ms); //create PdfConverter object PdfConverter objConverter = new PdfConverter(); //bind input pdf file objConverter.BindPdf(ms); //set StartPage and EndPage properties to the page number to //you want to convert images from objConverter.StartPage = 1; objConverter.EndPage = 1; //Counter int page = 1; //initialize the converting process objConverter.DoConvert(); //check if pages exist and then convert to image one by one while (objConverter.HasNextImage()) { objConverter.GetNextImage(dataDir + "Specific_Region-Image" + page++ + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg); } //close the PdfConverter object objConverter.Close(); // close MemoryStream object holding the updated document ms.Close(); }
public string ExtactImagesFromFile(EmpModel model) { try { string LData = "PExpY2Vuc2U+CjxEYXRhPgo8TGljZW5zZWRUbz5BdmVQb2ludDwvTGljZW5zZWRUbz4KPEVtYWlsVG8+aXRfYmlsbGluZ0BhdmVwb2ludC5jb208L0VtYWlsVG8+CjxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgT0VNPC9MaWNlbnNlVHlwZT4KPExpY2Vuc2VOb3RlPkxpbWl0ZWQgdG8gMSBkZXZlbG9wZXIsIHVubGltaXRlZCBwaHlzaWNhbCBsb2NhdGlvbnM8L0xpY2Vuc2VOb3RlPgo8T3JkZXJJRD4xOTA1MjAwNzE1NDY8L09yZGVySUQ+CjxVc2VySUQ+MTU0ODI2PC9Vc2VySUQ+CjxPRU0+VGhpcyBpcyBhIHJlZGlzdHJpYnV0YWJsZSBsaWNlbnNlPC9PRU0+CjxQcm9kdWN0cz4KPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0Pgo8L1Byb2R1Y3RzPgo8RWRpdGlvblR5cGU+RW50ZXJwcmlzZTwvRWRpdGlvblR5cGU+CjxTZXJpYWxOdW1iZXI+Y2JmMzVkNWYtOWE2Ni00ZTI4LTg1ZGQtM2ExN2JiZTM0MTNhPC9TZXJpYWxOdW1iZXI+CjxTdWJzY3JpcHRpb25FeHBpcnk+MjAyMDA2MDQ8L1N1YnNjcmlwdGlvbkV4cGlyeT4KPExpY2Vuc2VWZXJzaW9uPjMuMDwvTGljZW5zZVZlcnNpb24+CjxMaWNlbnNlSW5zdHJ1Y3Rpb25zPmh0dHBzOi8vcHVyY2hhc2UuYXNwb3NlLmNvbS9wb2xpY2llcy91c2UtbGljZW5zZTwvTGljZW5zZUluc3RydWN0aW9ucz4KPC9EYXRhPgo8U2lnbmF0dXJlPnpqZDMrdWgzNTdiZHhqR3JWTTZCN3I2c250TkRBTlRXU2MyQi9RWS9hdmZxTnA0VHk5Z0kxR2V1NUdOaWVwRHArY1JrRFBMdjBDRTZ2MHNjYVZwK1JNTkF5SzdiUzdzeGZSL205Z0NtekFNUlptdUxQTm1laEtZVTNvOGJWVDJvWmRJeEY2dVRTMDhIclJxUnk5SWt6c3BxYmRrcEZFY0lGcHlLbDF2NlF2UT08L1NpZ25hdHVyZT4KPC9MaWNlbnNlPg=="; Stream stream = new MemoryStream(Convert.FromBase64String(LData)); stream.Seek(0, SeekOrigin.Begin); new Aspose.Pdf.License().SetLicense(stream); //// file license in app_code folder //Aspose.Pdf.License license = new Aspose.Pdf.License(); //license.SetLicense(Server.MapPath("~/app_code/license.lic")); string rootFolder = Directory.GetCurrentDirectory(); Stream fileStream = model.files.OpenReadStream(); string outPutFilePath = rootFolder + "\\wwwroot\\ExtractedImages\\"; PdfConverter objConverter = new PdfConverter(); objConverter.BindPdf(fileStream); objConverter.DoConvert(); objConverter.CoordinateType = PageCoordinateType.CropBox; var stamp = DateTime.Now.Ticks.ToString() + "_1.jpg"; if (objConverter.HasNextImage()) { objConverter.GetNextImage(outPutFilePath + model.files.FileName + stamp, System.Drawing.Imaging.ImageFormat.Jpeg); } objConverter.Close(); return("Image Extracted Succesfully in this path :- " + System.IO.Path.Combine(outPutFilePath, model.files.FileName + stamp)); } catch (Exception ex) { } return("Some error occured, try after some time"); }
public static void Main() { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); //create PdfConverter object PdfConverter objConverter = new PdfConverter(); //bind input pdf file objConverter.BindPdf(dataDir + "input.pdf"); //initialize the converting process objConverter.DoConvert(); objConverter.ShowHiddenAreas = true; //check if pages exist and then convert to image one by one while (objConverter.HasNextImage()) { objConverter.GetNextImage(dataDir + DateTime.Now.Ticks.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } //close the PdfConverter object objConverter.Close(); }
private List <string> GetDocumentPages(string file, string folderName, int currentPage) { List <string> lstOutput = new List <string>(); string outfileName = "page_{0}"; string outPath = Config.Configuration.OutputDirectory + folderName + "/" + outfileName; currentPage = currentPage - 1; Directory.CreateDirectory(Config.Configuration.OutputDirectory + folderName); string imagePath = string.Format(outPath, currentPage) + ".jpeg"; if (System.IO.File.Exists(imagePath) && currentPage > 0) { lstOutput.Add(imagePath); return(lstOutput); } int i = currentPage; var filename = System.IO.File.Exists(Config.Configuration.WorkingDirectory + folderName + "/" + file) ? Config.Configuration.WorkingDirectory + folderName + "/" + file : Config.Configuration.OutputDirectory + folderName + "/" + file; using (FilePathLock.Use(filename)) { try { if (!System.IO.File.Exists(Config.Configuration.OutputDirectory + folderName + "/" + "outputPDF.pdf")) { Aspose.Page.Live.Demos.UI.Models.License.SetAsposePageLicense(); System.IO.FileStream pdfStream = new System.IO.FileStream(Config.Configuration.OutputDirectory + folderName + "/" + "outputPDF.pdf", System.IO.FileMode.CreateNew, System.IO.FileAccess.Write); System.IO.FileStream psStream = new System.IO.FileStream(Config.Configuration.WorkingDirectory + folderName + "/" + file, System.IO.FileMode.Open, System.IO.FileAccess.Read); try { if (file.ToLower().EndsWith(".xps")) { Page.XPS.XpsDocument document = new XpsDocument(psStream, new Page.XPS.XpsLoadOptions()); Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions(); Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream, new Size(595, 842)); document.Save(device, options); } else { PsDocument document = new PsDocument(psStream); Aspose.Page.EPS.Device.PdfSaveOptions options = new Aspose.Page.EPS.Device.PdfSaveOptions(true); Aspose.Page.EPS.Device.PdfDevice device = new Aspose.Page.EPS.Device.PdfDevice(pdfStream, new Size(595, 842)); document.Save(device, options); } } finally { psStream.Close(); pdfStream.Close(); } } PdfConverter objConverter = new PdfConverter(); objConverter.BindPdf(Config.Configuration.OutputDirectory + folderName + "/" + "outputPDF.pdf"); objConverter.DoConvert(); objConverter.CoordinateType = PageCoordinateType.CropBox; if (currentPage < objConverter.PageCount) { lstOutput.Add(objConverter.PageCount.ToString()); i = 0; while (objConverter.HasNextImage() && i < objConverter.PageCount) { objConverter.StartPage = (currentPage + 1); objConverter.GetNextImage(imagePath, System.Drawing.Imaging.ImageFormat.Png); lstOutput.Add(imagePath); break; } } objConverter.Close(); } catch (Exception ex) { throw ex; } return(lstOutput); } }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage httpRequestMessage, ILogger log) { log.LogInformation($"ImageConverterFunction :HTTP trigger function processed request at : { DateTime.Now}"); //Binding file content from httpRequestMessage var fileContent = httpRequestMessage.Content; string jsonContent = fileContent.ReadAsStringAsync().Result; //Get headers from httpRequestMessage var headers = httpRequestMessage.Headers; var jsonToReturn = string.Empty; string errormessage = string.Empty; HeaderModel headerModel = new HeaderModel(); try { //Validating headers from httprequestMessage if (HeaderValidation.ValidateHeader(headers, ref errormessage)) { headerModel.FileExtension = headers.GetValues(Constants.FILE_EXTENSION).First(); headerModel.FileType = headers.GetValues(Constants.FILE_TYPE).First(); log.LogInformation($"HeaderValidation success for File extension and FileType"); } else { //Header values has empty or null return badrequest response log.LogInformation($"HeaderValidation Failure for File extension and FileType :{errormessage}"); return(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent($"{errormessage}") }); } #region ImageToThumbnail //Converting Image file to Thumbnail image if (headerModel.FileExtension == Constants.PNG || headerModel.FileExtension == Constants.JPEG) { try { //Validating Image specific headers if (HeaderValidation.ValidateImageHeader(headers, ref errormessage)) { headerModel.ThumnailImageHeight = headers.GetValues(Constants.THUMBNAIL_HEIGHT).First(); headerModel.ThumnailImageWidth = headers.GetValues(Constants.THUMBNAIL_WIDTH).First(); headerModel.InlineImageHeight = headers.GetValues(Constants.INLINE_HEIGHT).First(); headerModel.InlineImageWidth = headers.GetValues(Constants.INLINE_WIDTH).First(); log.LogError($"HeaderValidation Success for Image to thumbnail conversion"); } else { //Header values has empty or null return badrequest response log.LogInformation($"HeaderValidation Failure in Image to thumbnail conversion: {errormessage}"); return(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent($"{ errormessage }", Encoding.UTF8, Constants.JSON) }); } //Convert file content to memorystream var memoryStream = new MemoryStream(Convert.FromBase64String(jsonContent)); var imageObj = new { thumbnail = ConvertToThumbnail(memoryStream, Convert.ToInt32(headerModel.ThumnailImageHeight), Convert.ToInt32(headerModel.ThumnailImageWidth), log), inline = ConvertToThumbnail(memoryStream, Convert.ToInt32(headerModel.InlineImageHeight), Convert.ToInt32(headerModel.InlineImageWidth), log) }; jsonToReturn = JsonConvert.SerializeObject(imageObj); } catch (Exception ex) { log.LogError($"Exception occurred in Image file conversion to thumbnail, Error : {ex.Message},Details:{ex.InnerException}"); return(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(ex.Message, Encoding.UTF8, Constants.JSON) }); } } #endregion #region PDFToThumbnail //Converting Pdf first page to Thumbnail image if (headerModel.FileExtension == Constants.PDF) { try { //To include license Aspose.Pdf.License license = new Aspose.Pdf.License(); license.SetLicense("Aspose.Pdf.lic"); log.LogInformation("Aspose SetLicense Success"); ImageFormat format = GetImageFormat(headerModel.FileType, log); PdfConverter pdfConverter = new PdfConverter(); //Validating pdf file specific headers if (HeaderValidation.ValidatePdfFileHeader(headers, ref errormessage)) { headerModel.Height = headers.GetValues(Constants.HEIGHT).First(); headerModel.Width = headers.GetValues(Constants.WIDTH).First(); log.LogInformation($"HeaderValidation Success for PDF to thumbnail conversion"); } else { //Header values has empty or null return badrequest response log.LogError($"HeaderValidation Failure for PDF to thumbnail conversion : {errormessage}"); return(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent($"{errormessage}", Encoding.UTF8, Constants.JSON) }); } var streamContent = httpRequestMessage.Content.ReadAsStreamAsync(); pdfConverter.BindPdf(streamContent.Result); //To convert first page of PDF pdfConverter.StartPage = 1; pdfConverter.EndPage = 1; pdfConverter.Resolution = new Aspose.Pdf.Devices.Resolution(100); pdfConverter.DoConvert(); MemoryStream imageStream = new MemoryStream(); while (pdfConverter.HasNextImage()) { // Save the image in the given image Format pdfConverter.GetNextImage(imageStream, format); // Set the stream position to the beginning of the stream imageStream.Position = 0; } var imageObj = new { content = ConvertToThumbnail(imageStream, Convert.ToInt32(headerModel.Width), Convert.ToInt32(headerModel.Height), log), contentType = "image/" + headerModel.FileType }; jsonToReturn = JsonConvert.SerializeObject(imageObj); pdfConverter.Close(); imageStream.Close(); } catch (Exception ex) { log.LogError($"Exception occurred in pdf file conversion to thumbnail,Error : {ex.Message},Details:{ex.InnerException}"); return(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(ex.Message, Encoding.UTF8, Constants.JSON) }); } } #endregion log.LogInformation("ImageConverterFunction successfully processed."); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(jsonToReturn, Encoding.UTF8, Constants.JSON) }); } catch (Exception ex) { log.LogError($"Exception occurred in ImageConverterFunction,Error: { ex.Message},Details: { ex.InnerException}"); return(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(ex.Message, Encoding.UTF8, Constants.JSON) }); } }