Пдф с изображениями.

This commit is contained in:
Артем Харламов 2023-10-20 09:00:04 +04:00
parent bee04958e0
commit 2068af18d0
17 changed files with 190 additions and 4 deletions

View File

@ -28,6 +28,7 @@
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
buttonAdd = new Button();
buttonClear = new Button();
buttonSelect = new Button();
@ -42,6 +43,9 @@
buttonGetIndex = new Button();
buttonSetIndex = new Button();
myTreeView = new ViewComponents.MyTreeView();
buttonPdfImages = new Button();
pdfImages1 = new ViewComponents.NotVisualComponents.PdfImages(components);
openFileDialog1 = new OpenFileDialog();
SuspendLayout();
//
// buttonAdd
@ -85,7 +89,10 @@
//
// input_text
//
input_text.Element = "Range exeption";
input_text.Location = new Point(312, 12);
input_text.MaxLen = -1;
input_text.MinLen = -1;
input_text.Name = "input_text";
input_text.Size = new Size(240, 119);
input_text.TabIndex = 5;
@ -179,11 +186,27 @@
myTreeView.Size = new Size(226, 204);
myTreeView.TabIndex = 8;
//
// buttonPdfImages
//
buttonPdfImages.Location = new Point(60, 363);
buttonPdfImages.Name = "buttonPdfImages";
buttonPdfImages.Size = new Size(197, 29);
buttonPdfImages.TabIndex = 15;
buttonPdfImages.Text = "Загрузить изображения";
buttonPdfImages.UseVisualStyleBackColor = true;
buttonPdfImages.Click += buttonPdfImages_Click;
//
// openFileDialog1
//
openFileDialog1.FileName = "openFileDialog1";
openFileDialog1.Multiselect = true;
//
// Form1
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(909, 450);
ClientSize = new Size(899, 600);
Controls.Add(buttonPdfImages);
Controls.Add(myTreeView);
Controls.Add(buttonSetIndex);
Controls.Add(buttonGetIndex);
@ -220,5 +243,8 @@
private Button buttonGetIndex;
private Button buttonSetIndex;
private ViewComponents.MyTreeView myTreeView;
private Button buttonPdfImages;
private ViewComponents.NotVisualComponents.PdfImages pdfImages1;
private OpenFileDialog openFileDialog1;
}
}

View File

@ -1,4 +1,6 @@
using System.Windows.Forms;
using ViewComponents.Exeption;
using ViewComponents.NotVisualComponents;
namespace TestView
{
@ -108,5 +110,17 @@ namespace TestView
{
}
private void buttonPdfImages_Click(object sender, EventArgs e)
{
var res = openFileDialog1.ShowDialog(this);
if (res != DialogResult.OK) return;
var files = openFileDialog1.FileNames;
openFileDialog1.Dispose();
string path = "C:\\Users\\xarla\\OneDrive\\Äîêóìåíòû\\test.pdf";
MessageBox.Show(path);
if (pdfImages1.CreatePdfDoc(new ImagesForPDF(path, "Ðóññêàÿ ëèòåðàòóðà", files))) MessageBox.Show("Óñïåõ!");
else MessageBox.Show("Îøèáêà, ïðîâåðüòå êîíñîëü");
}
}
}

View File

@ -57,4 +57,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="pdfImages1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="openFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>150, 17</value>
</metadata>
</root>

View File

@ -9,6 +9,10 @@
<RootNamespace>TestView</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ViewComponents\ViewComponents.csproj" />
</ItemGroup>

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ViewComponents.NotVisualComponents
{
public class ImagesForPDF
{
public ImagesForPDF(string filepath, string header, string[] imagepaths)
{
this.filepath = filepath;
this.header = header;
this.imagepaths = imagepaths;
}
public string filepath;
public string header;
public string[] imagepaths;
}
}

View File

@ -0,0 +1,36 @@
namespace ViewComponents.NotVisualComponents
{
partial class PdfImages
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -0,0 +1,73 @@
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ViewComponents.NotVisualComponents
{
public partial class PdfImages : Component
{
public PdfImages()
{
InitializeComponent();
}
public PdfImages(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public bool CreatePdfDoc (ImagesForPDF imagesForPDF)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
CheckFileExsists(imagesForPDF);
// создание документа
Document _document = new Document();
var style = _document.Styles["Normal"];
style.Font.Name = "Arial";
style.Font.Size = 25;
style = _document.Styles.AddStyle("NormalTitle", "Normal");
style.Font.Bold = true;
//добавление заголовка
var section = _document.AddSection();
var paragraph = section.AddParagraph(imagesForPDF.header);
paragraph.Format.Alignment = ParagraphAlignment.Center;
paragraph.Format.SpaceAfter = "2cm";
//добавление изображений
foreach (string path in imagesForPDF.imagepaths)
if (File.Exists(path)) section.AddImage(path);
//сохранение документа
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
renderer.Document = _document;
renderer.RenderDocument();
renderer.PdfDocument.Save(imagesForPDF.filepath);
return true;
}
private void CheckFileExsists(ImagesForPDF imagesForPDF)
{
if (string.IsNullOrEmpty(imagesForPDF.filepath) || string.IsNullOrEmpty(imagesForPDF.header) || imagesForPDF.imagepaths.Length == 0)
{
throw new ArgumentNullException();
}
if (!File.Exists(imagesForPDF.filepath))
{
throw new FileNotFoundException(imagesForPDF.filepath);
}
}
}
}

View File

@ -8,7 +8,12 @@
</PropertyGroup>
<ItemGroup>
<Compile Update="Input_text.cs">
<PackageReference Include="PDFsharp-MigraDoc" Version="1.50.5147" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Update="VisualComponents\Input_text.cs">
<SubType>UserControl</SubType>
<CustomToolNamespace>TestView</CustomToolNamespace>
</Compile>