/// <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);
        }
Пример #2
0
        private void ConvertPdfToPng(byte[] pdfImage)
        {
            var ms = new MemoryStream();

            new MemoryStream(pdfImage).CopyTo(ms);
            Spire.Pdf.PdfDocument d = new Spire.Pdf.PdfDocument(ms);
            pngImage = d.SaveAsImage(0, 300, 300);
            d.Close();
        }