/// <summary> /// Uploads a file to the webserver. /// </summary> /// <param name="title">The title for the file, to be used in the Database.</param> /// <param name="type">What type of file is uploaded. Example: 'FirstAid' or 'DoctorsNote'.</param> /// <param name="fileLocation">The local filepath to the document.</param> /// <param name="url">The url of the server, where the file will be uploaded.</param> /// <returns>Returns a bool wether the operation was completed or not.</returns> private bool UploadFile(string title, string type, string fileLocation, string url) { Spire.Pdf.PdfDocument document = new Spire.Pdf.PdfDocument(); FileInfo file = new FileInfo(fileLocation); if (!file.Exists) { return(false); } if (file.Extension != ".pdf") { Spire.Pdf.PdfPageBase page = document.Pages.Add(); PdfImage image = PdfImage.FromFile(fileLocation); fileLocation = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.pdf"); float widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width; float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height; float fitRate = Math.Max(widthFitRate, heightFitRate); float fitWidth = image.PhysicalDimension.Width / fitRate; float fitHeight = image.PhysicalDimension.Height / fitRate; page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight); document.DocumentInformation.Title = $"{Session.LoggedInUser.Fullname} - {type}"; document.DocumentInformation.Author = Session.LoggedInUser.Fullname; document.DocumentInformation.CreationDate = DateTime.Now; document.SaveToFile(fileLocation); document.Close(); } else { document.LoadFromFile(fileLocation); document.DocumentInformation.Title = $"{Session.LoggedInUser.Fullname} - {type}"; document.DocumentInformation.Author = Session.LoggedInUser.Fullname; document.DocumentInformation.CreationDate = DateTime.Now; document.SaveToFile(fileLocation); document.Close(); } document.Dispose(); string fileUrl = SendToServer(fileLocation, url); if (fileUrl == "null") { return(false); } if (!MySql.UploadDocument(title, type, DateTime.Today, Session.LoggedInUser.Id, fileUrl)) { return(false); } return(true); }
//public static void ToTIFF(List<string> imagesPath,string folderName) //{ // System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag; // EncoderParameters ep = new EncoderParameters(1); // ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); // Bitmap pages = null; // File.MemoryStream ms = new MemoryStream(File.ReadAllBytes()); // pages = (Bitmap)Image.FromStream(ms); // int frame = 0; // foreach (var item in imagesPath) // { // MemoryStream mss = new MemoryStream(File.ReadAllBytes(item)); // Bitmap bm = (Bitmap)Image.FromStream(mss); // int frameCount = bm.GetFrameCount(FrameDimension.Page); // for (int i = 0; i < frameCount; i++) // { // bm.SelectActiveFrame(FrameDimension.Page, i); // pages.SaveAdd(bm, ep); // } // } // pages.Save(@"C:\1\TiffFile.tif"); //} //public static class TiffHelper //{ // /// <summary> // /// Merges multiple TIFF files (including multipage TIFFs) into a single multipage TIFF file. // /// </summary> // public static byte[] MergeTiff(params byte[][] tiffFiles) // { // byte[] tiffMerge = null; // using (var msMerge = new MemoryStream()) // { // //get the codec for tiff files // ImageCodecInfo ici = null; // foreach (ImageCodecInfo i in ImageCodecInfo.GetImageEncoders()) // if (i.MimeType == "image/tiff") // ici = i; // System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag; // EncoderParameters ep = new EncoderParameters(1); // Bitmap pages = null; // int frame = 0; // foreach (var tiffFile in tiffFiles) // { // using (var imageStream = new MemoryStream(tiffFile)) // { // using (Image tiffImage = Image.FromStream(imageStream)) // { // foreach (Guid guid in tiffImage.FrameDimensionsList) // { // //create the frame dimension // FrameDimension dimension = new FrameDimension(guid); // //Gets the total number of frames in the .tiff file // int noOfPages = tiffImage.GetFrameCount(dimension); // for (int index = 0; index < noOfPages; index++) // { // FrameDimension currentFrame = new FrameDimension(guid); // tiffImage.SelectActiveFrame(currentFrame, index); // using (MemoryStream tempImg = new MemoryStream()) // { // tiffImage.Save(tempImg, ImageFormat.Tiff); // { // if (frame == 0) // { // //save the first frame // pages = (Bitmap)Image.FromStream(tempImg); // ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); // pages.Save(msMerge, ici, ep); // } // else // { // //save the intermediate frames // ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); // pages.SaveAdd((Bitmap)Image.FromStream(tempImg), ep); // } // } // frame++; // } // } // } // } // } // } // if (frame > 0) // { // //flush and close. // ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); // pages.SaveAdd(ep); // } // msMerge.Position = 0; // tiffMerge = msMerge.ToArray(); // } // return tiffMerge; // } //} public static MemoryStream ToPDF(List <byte[]> input) { try { MemoryStream memoryStream = new MemoryStream(); using (Spire.Pdf.PdfDocument document = new Spire.Pdf.PdfDocument()) { foreach (byte[] element in input) { MemoryStream imageMemoryStream = new MemoryStream(element); Spire.Pdf.PdfPageBase page = document.Pages.Add(); Spire.Pdf.Graphics.PdfImage image = Spire.Pdf.Graphics.PdfImage.FromStream(imageMemoryStream); //page.PageInfo.Margin.Bottom = 0; //page.PageInfo.Margin.Top = 0; //page.PageInfo.Margin.Left = 0; //page.PageInfo.Margin.Right = 0; //page.PageInfo.IsLandscape = false; float widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width; float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height; float fitRate = Math.Max(widthFitRate, heightFitRate); float fitWidth = image.PhysicalDimension.Width / fitRate; float fitHeight = image.PhysicalDimension.Height / fitRate; page.Canvas.DrawImage(image, 0, 0, fitWidth, fitHeight); } //document.PDFStandard.CompressionLevel(new Document.ConvertOptions() //{ // LinkDuplcateStreams = true, // RemoveUnusedObjects = true, // RemoveUnusedStreams = true, // CompressImages = true //IMP //}); document.SaveToStream(memoryStream); } return(memoryStream); } catch { throw; } }
public async Task GeneratePdf(string html, string filePath) { await Task.Run(() => { using (FileStream ms = new FileStream(filePath, FileMode.Create)) { PdfSharp.Pdf.PdfDocument pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4); pdf.Save(ms); ms.Close(); } Spire.Pdf.PdfDocument abc = new Spire.Pdf.PdfDocument(filePath); Spire.Pdf.Graphics.PdfImage image = Spire.Pdf.Graphics.PdfImage.FromFile(@"D:\Logo.png"); float width = image.Width * 0.75f; float height = image.Height * 0.75f; Spire.Pdf.PdfPageBase page0 = abc.Pages[0]; float x = (page0.Canvas.ClientSize.Width - width) / 2; page0.Canvas.DrawImage(image, x, 60, width, height); abc.SaveToFile(filePath); }); }