Пример #1
0
        /// <summary>
        /// Convert pdf to html with picture files
        /// </summary>
        /// <param name="inputFileName">Absolute name of input file</param>
        /// <param name="outputFolder">Folder for saving results</param>
        /// <param name="newFileName">Relative filename of new html</param>
        public static void ConvertToHtml(string inputFileName, string outputFolder, string newFileName, Dictionary <string, string> parameters)
        {
            if (!Directory.Exists(outputFolder))
            {
                Directory.CreateDirectory(outputFolder);
            }
            Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument(inputFileName);
            using (var stream = new MemoryStream())
            {
                pdf.SaveToStream(stream, Spire.Pdf.FileFormat.HTML);
                string result = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                if (result.IndexOf(replaceValuePdf) >= 0)
                {
                    result = result.Replace(replaceValuePdf, "");
                }
                else if (result.IndexOf(replaceValueWithoutTag) >= 0)
                {
                    result = result.Replace(replaceValueWithoutTag, "");
                }
                else if (result.IndexOf(replaceValue) >= 0)
                {
                    result = result.Replace(replaceValue, "");
                }
                result = ExtractImages(outputFolder, result);

                File.WriteAllText(outputFolder + "/" + newFileName, result, Encoding.UTF8);
                result = null;
            }
            pdf.Dispose();
        }
        /// <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);
        }
Пример #3
0
        private void PDF()
        {
            Spire.Pdf.PdfDocument document = new Spire.Pdf.PdfDocument();
            document.LoadFromFile(sciezkaDoPliku);
            Image wynik = document.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Metafile);

            for (int i = 1; i < document.Pages.Count; i++)
            {
                Image pom = document.SaveAsImage(i, Spire.Pdf.Graphics.PdfImageType.Metafile);
                wynik = MergeTwoImages(wynik, pom);
                pom.Dispose();
            }
            wynik.Save(sciezgaDoZapisaniaPliku + /*"_" + document.Pages.Count + */ ".jpg");
            wynik.Dispose();
            document.Dispose();
        }
Пример #4
0
        private void printToPrinter(string printerName, string filePath)
        {
            Spire.Pdf.PdfDocument pdfdocument = new Spire.Pdf.PdfDocument();
            pdfdocument.LoadFromFile(filePath);
            pdfdocument.PrinterName = printerName;

            PaperSize paperSize = new PaperSize();

            paperSize.Width   = 283; //inch*100
            paperSize.Height  = 826; //inch*100
            paperSize.RawKind = (int)PaperKind.Custom;
            //  pdfdocument.PrinterSettings.PaperSize = paperSize;

            //pdfdocument.war
            pdfdocument.PrintDocument.PrinterSettings.Copies = 1;
            pdfdocument.PrintDocument.Print();
            pdfdocument.Dispose();
        }
Пример #5
0
        private byte[] MergePdf(string directoryPath)
        {
            try
            {
                Spire.Pdf.PdfDocument document = new Spire.Pdf.PdfDocument();
                var      mainUrl    = Path.Combine(_env.ContentRootPath, "wwwrootr");
                var      mainPdf    = Path.Combine(mainUrl, "pdf", "PropuestaComercial.pdf");
                int      totalFiles = mobilesPages + 2;
                string[] lstFiles   = new string[totalFiles];
                lstFiles[0] = mainPdf;
                lstFiles[1] = Path.Combine(directoryPath, "Subsidy.pdf");

                int index = 0;
                for (int i = 2; i < totalFiles; i++)
                {
                    lstFiles[i] = Path.Combine(directoryPath, "mobiles" + index.ToString() + ".pdf");
                    index++;
                }

                PdfReader       reader          = null;
                Document        sourceDocument  = null;
                PdfCopy         pdfCopyProvider = null;
                PdfImportedPage importedPage;
                string          outputPdfPath = Path.Combine(directoryPath, "Presupuesto.pdf");
                sourceDocument = new Document();
                FileStream fs = new FileStream(outputPdfPath, FileMode.OpenOrCreate);
                pdfCopyProvider = new PdfCopy(sourceDocument, fs);

                //Open the output file
                sourceDocument.Open();

                foreach (var item in lstFiles)
                {
                    document.LoadFromFile(item);
                    int pages = document.Pages.Count;

                    reader = new PdfReader(item);
                    FileInfo info     = new FileInfo(item);
                    var      fileName = info.Name.Split(".")[0];
                    fileName = fileName.Substring(0, 6); // me quedo solo con la palabra "mobile";

                    if (fileName.Equals("mobile"))
                    {
                        importedPage = pdfCopyProvider.GetImportedPage(reader, 1);
                        pdfCopyProvider.AddPage(importedPage);
                    }
                    else
                    {
                        //Add pages of current file
                        for (int i = 1; i <= pages; i++)
                        {
                            importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                            pdfCopyProvider.AddPage(importedPage);
                        }
                    }

                    reader.Close();
                }

                sourceDocument.Close();
                pdfCopyProvider.Close();
                document.Dispose();
                fs.Close();
                string pdfFilePath = outputPdfPath;
                byte[] bytes       = File.ReadAllBytes(pdfFilePath);

                //elimino el directorio temporal
                DeleteDirectory(directoryPath);

                return(bytes);
            }

            catch (Exception ex)
            {
                DeleteDirectory(directoryPath);
                throw ex;
            }
        }