//public void ConvertDocumentToPDF(byte[] bytes, string pathFileOut,string pathFileOutTemp,List<Archive> ImageRoutes) //{ // try { // var inputAsString = Convert.ToBase64String(bytes.ToArray(), 0, bytes.ToArray().Length); // File.WriteAllBytes(pathFileOutTemp, Convert.FromBase64String(inputAsString)); // //Incrustando Imagenes // IncrustingImage(pathFileOutTemp, ImageRoutes,pathFileOut); // //if (ImageRoutes != null && ImageRoutes.Count > 0) // string textDelete = "Unlicensed version. Please register @ templater.info"; // DocumentCore document = DocumentCore.Load(pathFileOutTemp, new DocxLoadOptions()); // int countDel = 0; // foreach (ContentRange cr in document.Content.Find(textDelete).Reverse()) // { // cr.Delete(); // countDel++; // } // if (File.Exists(pathFileOut)) File.Delete(pathFileOut); // if (ImageRoutes.Where(x=>x.typeAdjunte == TypeAdjunte.pdf).Count() > 0) // { // List<byte[]> listByte = new List<byte[]>(); // using (MemoryStream PDFGenerate = new MemoryStream()) // { // document.Save(PDFGenerate, SaveOptions.PdfDefault); // listByte.Add(PDFGenerate.ToArray()); // } // foreach (var file in ImageRoutes.Where(x => x.typeAdjunte == TypeAdjunte.pdf)) // { // byte[] PdfBype = null; // if (File.Exists(file.RutaArchivo)) // { // PdfBype = System.IO.File.ReadAllBytes(file.RutaArchivo); // if (PdfBype != null) // { // listByte.Add(PdfBype); // } // } // } // byte[] PdfFinale = MergeFiles(listByte); // var inputFinale = Convert.ToBase64String(PdfFinale.ToArray(), 0, PdfFinale.ToArray().Length); // File.WriteAllBytes(pathFileOut, Convert.FromBase64String(inputFinale)); // } // else // { // document.Save(pathFileOut); // } // File.Delete(pathFileOutTemp); // } // catch(Exception ex) // { // throw ex; // } //} public static byte[] MergeFiles(Dictionary <int, byte[]> sourceFiles) { iTextSharp.text.Document document = new iTextSharp.text.Document(); using (MemoryStream ms = new MemoryStream()) { PdfCopy copy = new PdfCopy(document, ms); document.Open(); int documentPageCounter = 0; sourceFiles = sourceFiles.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value); // Iterate through all pdf documents foreach (KeyValuePair <int, byte[]> pair in sourceFiles) { // Create pdf reader PdfReader reader = new PdfReader(pair.Value); int numberOfPages = reader.NumberOfPages; // Iterate through all pages for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++) { documentPageCounter++; PdfImportedPage importedPage = copy.GetImportedPage(reader, currentPageIndex); PdfCopy.PageStamp pageStamp = copy.CreatePageStamp(importedPage); // Write header ColumnText.ShowTextAligned(pageStamp.GetOverContent(), iTextSharp.text.Element.ALIGN_CENTER, new Phrase(""), importedPage.Width / 2, importedPage.Height - 30, importedPage.Width < importedPage.Height ? 0 : 1); // Write footer ColumnText.ShowTextAligned(pageStamp.GetOverContent(), iTextSharp.text.Element.ALIGN_CENTER, new Phrase(String.Format("", documentPageCounter)), importedPage.Width / 2, 30, importedPage.Width < importedPage.Height ? 0 : 1); pageStamp.AlterContents(); copy.AddPage(importedPage); } copy.FreeReader(reader); reader.Close(); } document.Close(); return(ms.GetBuffer()); } }
public ResponseModel ConvertImageToPdf(string fileLocation, string outLocation) { iTextSharp.text.Document document = null; try { if (!File.Exists(outLocation)) { document = new iTextSharp.text.Document(PageSize.A4, 25, 25, 25, 25); using (var stream = new FileStream(outLocation, FileMode.Create, FileAccess.Write, FileShare.None)) { PdfWriter.GetInstance(document, stream); document.Open(); using (var imageStream = new FileStream(fileLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var image = Image.GetInstance(imageStream); if (image.Height > PageSize.A4.Height - 25) { image.ScaleToFit(PageSize.A4.Width - 25, PageSize.A4.Height - 25); } else if (image.Width > PageSize.A4.Width - 25) { image.ScaleToFit(PageSize.A4.Width - 25, PageSize.A4.Height - 25); } image.Alignment = Element.ALIGN_MIDDLE; document.Add(image); } document.Close(); } } else { return(new ResponseModel { IsSucceed = false, ErrorMessage = outLocation + " file-ı artıq mövcuddur." }); } return(new ResponseModel { Data = outLocation, IsSucceed = true, ErrorMessage = string.Empty }); } catch (Exception e) { document.Close(); return(new ResponseModel { IsSucceed = false, ErrorMessage = "File convert oluna bilmədi: " + e.Message }); } }
private bool ConvertImageToPdf(string tempPath, string output) { if (string.IsNullOrEmpty(tempPath) || string.IsNullOrEmpty(output)) { return(false); } try { using (FileStream fs = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.None)) { using (iTextSharp.text.Document doc = new iTextSharp.text.Document()) { using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) { doc.Open(); Image image = Image.GetInstance(tempPath); image.SetAbsolutePosition(0, 0); doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, image.Width, image.Height, 0)); doc.NewPage(); writer.DirectContent.AddImage(image, false); doc.Close(); return(true); } } } } catch (Exception ex) { Console.WriteLine(ex); return(false); } }