-
Notifications
You must be signed in to change notification settings - Fork 0
/
PdfConverter.cs
95 lines (84 loc) · 3.83 KB
/
PdfConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.IO;
using System.Linq;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using WorkflowLib.Shared.Models.Documents;
namespace WorkflowLib.Shared.Office.DocFormats
{
/// <summary>
/// Class for using PDF (PDF converter)
/// </summary>
public class PdfConverter : WorkflowLib.Shared.Office.DocFormats.TextBased.ITextBased
{
/// <summary>
/// Method for converting a list of TextDocElement into TXT document.
/// </summary>
public void TextDocElementsToDocument(string foldername, string filename, System.Collections.Generic.List<TextDocElement> elements)
{
if (!Directory.Exists(foldername)) throw new System.Exception("Folder name does not exist");
if (string.IsNullOrEmpty(filename)) throw new System.Exception("File name could not be null or empty");
if (filename.Split('.').Last().ToLower() != "pdf") throw new System.Exception("Incorrect file extension");
// TODO: Check if the following piece of code catches all the possible exceptions!
try
{
string filepath = Path.Combine(foldername, filename);
using (var writer = new PdfWriter(filepath))
using (PdfDocument pdf = new PdfDocument(writer))
using (Document doc = new Document(pdf))
{
foreach (var element in elements)
{
Paragraph paragraph = new Paragraph(element.Content)
.SetTextAlignment((iText.Layout.Properties.TextAlignment)element.TextAlignment)
.SetFontSize(element.FontSize);
doc.Add(paragraph);
}
doc.Close();
}
}
catch (System.Exception)
{
throw;
}
}
#region Convert to list of TextDocElement
/// <summary>
/// Convert to list of TextDocElement
/// </summary>
public System.Collections.Generic.List<TextDocElement> ConvertFileToTde(string foldername, string filename)
{
if (!Directory.Exists(foldername)) throw new System.Exception("Folder does not exist");
if (string.IsNullOrEmpty(filename)) throw new System.Exception("File name could not be null or empty");
return ConvertFileToTde(Path.Combine(foldername, filename));
}
/// <summary>
/// Convert to list of TextDocElement
/// </summary>
public System.Collections.Generic.List<TextDocElement> ConvertFileToTde(string filepath)
{
if (string.IsNullOrEmpty(filepath)) throw new System.Exception("File name could not be null or empty");
if (!File.Exists(filepath)) throw new System.Exception("File does not exist");
return ConvertFileToTde(new FileInfo(filepath));
}
/// <summary>
/// Convert to list of TextDocElement
/// </summary>
public System.Collections.Generic.List<TextDocElement> ConvertFileToTde(FileInfo file)
{
string content = System.IO.File.ReadAllText(file.FullName);
if (string.IsNullOrEmpty(content)) throw new System.Exception("File content could not be empty");
return ConvertStringToTde(content);
}
/// <summary>
/// Convert to list of TextDocElement
/// </summary>
public System.Collections.Generic.List<TextDocElement> ConvertStringToTde(string xmlContent)
{
if (string.IsNullOrEmpty(xmlContent)) throw new System.Exception("XML content could not be empty");
//
return new System.Collections.Generic.List<TextDocElement>();
}
#endregion // Convert to list of TextDocElement
}
}