KOP_Library_v5/ViewComponents/NotVisualComponents/PdfImages.cs

74 lines
2.0 KiB
C#

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);
}
}
}
}