示例#1
0
        public string SaveRightMovePdf(RightMoveModel rightMoveModel)
        {
            try
            {
                PdfLoadedDocument loadedDocument = new PdfLoadedDocument(new FileStream(templatePath, FileMode.Open));

                if (!Directory.Exists(archiveFolder))
                {
                    Directory.CreateDirectory(archiveFolder);
                }

                if (loadedDocument.PageCount > 0)
                {
                    PdfLoadedPage pdfLoadedPage = loadedDocument.Pages[1] as PdfLoadedPage;

                    PdfTemplate pdfTemplate = new PdfTemplate(900, 600);

                    PdfFont pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 15);

                    PdfBrush brush = new PdfSolidBrush(SfDrawing.Color.Black);

                    byte[] imageBytes  = new WebClient().DownloadData(rightMoveModel.PropertyMainPicture);
                    Stream imageStream = new MemoryStream(imageBytes);

                    pdfTemplate.Graphics.DrawString($"Property Address: {rightMoveModel.PropertyAddress}", pdfFont, brush, 100, 30);
                    pdfTemplate.Graphics.DrawString($"Property Type: {rightMoveModel.PropertyType}", pdfFont, brush, 100, 50);
                    pdfTemplate.Graphics.DrawString($"PropertyPrice: {rightMoveModel.PropertyPrice} ", pdfFont, brush, 100, 70);
                    pdfTemplate.Graphics.DrawImage(PdfImage.FromStream(imageStream), new SfDrawing.PointF(100, 100), new SfDrawing.SizeF(400, 400));

                    pdfLoadedPage.Graphics.DrawPdfTemplate(pdfTemplate, SfDrawing.PointF.Empty);

                    string rawName = rightMoveModel
                                     .PropertyUrl
                                     .Replace("/", "")
                                     .Replace("-", "")
                                     .Replace(".", "")
                                     .Replace(":", "")
                                     .Replace("//", "");

                    string fileName = Regex.Match(rawName, @"(\d+(?:\.\d{1,2})?)").Value;

                    PdfDocument propertyHeatMapPdfDocument = htmlConverter.Convert(rightMoveModel.PropertyHeatHtmlString, string.Empty);
                    PdfDocument homeCoUKHtmlPdfDocument    = htmlConverter.Convert(rightMoveModel.HomeCoUKHtmlString, string.Empty);

                    string tempPropertyHeatMap = Path.Combine(tempFolder, $"propertyHeatMap{fileName}.pdf");

                    using (FileStream propertyHeatMapStream = new FileStream(tempPropertyHeatMap, FileMode.Create))
                    {
                        propertyHeatMapPdfDocument.Save(propertyHeatMapStream);
                        propertyHeatMapPdfDocument.Close(true);
                        propertyHeatMapPdfDocument.Dispose();

                        propertyHeatMapStream.Close();
                        propertyHeatMapStream.Dispose();
                    }

                    string tempHomeCoUK = Path.Combine(tempFolder, $"homeCoUK{fileName}.pdf");

                    using (FileStream homeCoUKHtmlPdfStream = new FileStream(tempHomeCoUK, FileMode.Create))
                    {
                        homeCoUKHtmlPdfDocument.Save(homeCoUKHtmlPdfStream);
                        homeCoUKHtmlPdfDocument.Close(true);
                        homeCoUKHtmlPdfDocument.Dispose();

                        homeCoUKHtmlPdfStream.Close();
                        homeCoUKHtmlPdfStream.Dispose();
                    }

                    using (FileStream propertyHeatMapReadStream = new FileStream(tempPropertyHeatMap, FileMode.Open))
                    {
                        PdfLoadedDocument tempPropertyHeatMapDocument = new PdfLoadedDocument(propertyHeatMapReadStream);
                        loadedDocument.ImportPage(tempPropertyHeatMapDocument, 0);

                        propertyHeatMapReadStream.Close();
                        propertyHeatMapReadStream.Dispose();
                    }

                    using (FileStream homeCoUKReadStream = new FileStream(tempHomeCoUK, FileMode.Open))
                    {
                        PdfLoadedDocument tempHomeCoUKDocument = new PdfLoadedDocument(homeCoUKReadStream);

                        loadedDocument.ImportPage(tempHomeCoUKDocument, 0);

                        homeCoUKReadStream.Close();
                        homeCoUKReadStream.Dispose();
                    }

                    string savePath = Path.Combine(archiveFolder, $"{fileName}.pdf");

                    using (FileStream saveStream = new FileStream(savePath, FileMode.Create))
                    {
                        loadedDocument.Save(saveStream);
                        loadedDocument.Close(true);
                        loadedDocument.Dispose();
                        saveStream.Close();
                        saveStream.Dispose();
                    }

                    return($"file successfully saved at: {savePath}");
                }
                else
                {
                    Console.WriteLine("Invalid PDF file");
                    return($"Invalid PDF file");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unable to save the file. {ex.Message}");

                return($"Unable to save the file");
            }
        }