From f6990cd5a6971c5be028aca73ad773052019bac0 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Tue, 1 Oct 2024 10:59:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=20=D1=81=D0=BB=D0=B8=D1=82=D1=8B=D1=85?= =?UTF-8?q?=20=D1=8F=D1=87=D0=B5=D0=B9=D0=BA=D0=B0=D1=85=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B3=D1=83=D1=82=20=D0=B1=D1=8B=D1=82=D1=8C=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D1=82=D0=B5=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ExexForm2/Form1.cs | 58 +++++++++++++++++++++++++++--------- Library14Petrushin/PdfImg.cs | 24 +++++++++++++-- 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/ExexForm2/Form1.cs b/ExexForm2/Form1.cs index dd45fa6..9a8781b 100644 --- a/ExexForm2/Form1.cs +++ b/ExexForm2/Form1.cs @@ -70,6 +70,7 @@ namespace ExexForm2 private void btnCreatePdfTable_Click(object sender, EventArgs e) { string selectDirTable = ""; + string fileName = ""; using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog()) { @@ -79,27 +80,41 @@ namespace ExexForm2 } } - string fileName = Path.Combine(selectDirTable, "output2.pdf"); + using (SaveFileDialog saveFileDialog = new SaveFileDialog()) + { + saveFileDialog.Filter = "PDF Files (*.pdf)|*.pdf"; + saveFileDialog.DefaultExt = "pdf"; + saveFileDialog.InitialDirectory = selectDirTable; + saveFileDialog.FileName = "output2.pdf"; - var generator = new PdfColGroupTable(); + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + fileName = saveFileDialog.FileName; + } + } - var data1 = new List + if (!string.IsNullOrEmpty(fileName)) + { + var generator = new PdfColGroupTable(); + + var data1 = new List { new Person { FirstName = "John", LastName = "Doe", Age = 30 }, new Person { FirstName = "Jane", LastName = "Smith", Age = 25 } }; - var headers = new List { "First Name", "Last Name", "Age" }; - var commonHeaders = new List { "Name", "", "" }; - var propertyNames = new List { "FirstName", "LastName", "Age" }; - var rowHeights = new List { 20, 20 }; - var mergeCells = new List<(int startRow, int endRow, int startColumn, int endColumn)> + var headers = new List { "First Name", "Last Name", "Age" }; + var commonHeaders = new List { "Name", "", "" }; + var propertyNames = new List { "FirstName", "LastName", "Age" }; + var rowHeights = new List { 20, 20 }; + var mergeCells = new List<(int startRow, int endRow, int startColumn, int endColumn)> { (0, 1, 0, 0), // 0 1 (2, 2, 0, 1) - }; + }; - generator.GeneratePdf(fileName, "Example Document", mergeCells, rowHeights, headers, commonHeaders, propertyNames, data1); + generator.GeneratePdf(fileName, "Example Document", mergeCells, rowHeights, headers, commonHeaders, propertyNames, data1); + } } @@ -109,6 +124,7 @@ namespace ExexForm2 private void btnCreatePdfCircl_Click(object sender, EventArgs e) { string selectDir = ""; + string fileName = ""; using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog()) { @@ -118,11 +134,24 @@ namespace ExexForm2 } } - string fileName = Path.Combine(selectDir, "output3.pdf"); + using (SaveFileDialog saveFileDialog = new SaveFileDialog()) + { + saveFileDialog.Filter = "PDF Files (*.pdf)|*.pdf"; + saveFileDialog.DefaultExt = "pdf"; + saveFileDialog.InitialDirectory = selectDir; + saveFileDialog.FileName = "output3.pdf"; - var pdfCirclDiagr = new PdfCirclDiagr(); + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + fileName = saveFileDialog.FileName; + } + } - List chartData = new List + if (!string.IsNullOrEmpty(fileName)) + { + var pdfCirclDiagr = new PdfCirclDiagr(); + + List chartData = new List { new ChartData { SeriesName = "Series 1", Value = 10 }, new ChartData { SeriesName = "Series 2", Value = 20 }, @@ -130,7 +159,8 @@ namespace ExexForm2 new ChartData { SeriesName = "Series 4", Value = 40 } }; - pdfCirclDiagr.GeneratePdf(fileName, "Document Title", "Chart Title", LegendPosition.Right, chartData); + pdfCirclDiagr.GeneratePdf(fileName, "Document Title", "Chart Title", LegendPosition.Right, chartData); + } } } } \ No newline at end of file diff --git a/Library14Petrushin/PdfImg.cs b/Library14Petrushin/PdfImg.cs index ad4e9b6..b11748e 100644 --- a/Library14Petrushin/PdfImg.cs +++ b/Library14Petrushin/PdfImg.cs @@ -32,14 +32,34 @@ namespace Library14Petrushin PdfPage firstPage = document.AddPage(); XGraphics gfxFirstPage = XGraphics.FromPdfPage(firstPage); XFont font = new XFont("Arial", 20, XFontStyleEx.BoldItalic); + + // Рисуем заголовок на первой странице gfxFirstPage.DrawString(documentTitle, font, XBrushes.Black, new XRect(0, 0, firstPage.Width, 50), XStringFormats.Center); - foreach (var imageData in images) + // Добавляем изображение на первую страницу + if (images.Count > 0) + { + using (XImage img = XImage.FromFile(images[0].ImagePath)) + { + double imageWidth = img.PixelWidth * 72 / img.HorizontalResolution; + double imageHeight = img.PixelHeight * 72 / img.VerticalResolution; + + double scale = Math.Min(firstPage.Width / imageWidth, (firstPage.Height - 50) / imageHeight); // Учитываем высоту заголовка + + double imageX = (firstPage.Width - imageWidth * scale) / 2; // Центрируем изображение по горизонтали + double imageY = 50; // Начинаем рисовать изображение под заголовком + + gfxFirstPage.DrawImage(img, imageX, imageY, imageWidth * scale, imageHeight * scale); + } + } + + // Добавляем остальные изображения на новые страницы + for (int i = 1; i < images.Count; i++) { PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); - using (XImage img = XImage.FromFile(imageData.ImagePath)) + using (XImage img = XImage.FromFile(images[i].ImagePath)) { double imageWidth = img.PixelWidth * 72 / img.HorizontalResolution; double imageHeight = img.PixelHeight * 72 / img.VerticalResolution;