Skip to content

Commit 808af27

Browse files
authored
Merge pull request #136 from aspose-pdf-cloud/refactored-parser
Refactored Parser use cases
2 parents 1038e03 + 879b889 commit 808af27

File tree

8 files changed

+45
-97
lines changed

8 files changed

+45
-97
lines changed

Uses-Cases/Parser/GetImage.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

Uses-Cases/Parser/GetImages.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using Aspose.Pdf.Cloud.Sdk.Model;
2+
using Newtonsoft.Json;
23

34
namespace Parser
45
{
56
public class GetImages
67
{
7-
public static async Task Extract(ParserHelper helper, string documentName, int pageNumber, string remoteFolder)
8+
public static async Task Extract(ParserHelper helper, string documentName, int pageNumber, string localFolder, string remoteFolder)
89
{
910
await helper.UploadFile(documentName);
1011

@@ -21,7 +22,17 @@ public static async Task Extract(ParserHelper helper, string documentName, int p
2122
Console.WriteLine("GetImages(): Images successfully received from the document '{0}.", documentName);
2223
foreach (var image in response.Images.List)
2324
{
24-
Console.WriteLine(image.ToString());
25+
using (var respImage = await helper.pdfApi.GetImageExtractAsPngAsync(documentName, image.Id, folder: remoteFolder))
26+
{
27+
Console.WriteLine(image.ToString());
28+
29+
string fileName = Path.Combine(localFolder, image.Id + ".png");
30+
using (var file = File.Create(fileName)) {
31+
respImage.Seek(0, SeekOrigin.Begin);
32+
await respImage.CopyToAsync(file);
33+
Console.WriteLine("GetImages(): File '{0}' successfully downloaded.", fileName);
34+
}
35+
}
2536
}
2637
}
2738
}

Uses-Cases/Parser/GetTable.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

Uses-Cases/Parser/GetTables.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using Aspose.Pdf.Cloud.Sdk.Model;
2+
using Newtonsoft.Json;
3+
using static System.Net.Mime.MediaTypeNames;
24

35
namespace Parser
46
{
57
public class GetTables
68
{
7-
public static async Task Extract(ParserHelper helper, string documentName, string remoteFolder)
9+
public static async Task Extract(ParserHelper helper, string documentName, string localFolder, string remoteFolder)
810
{
911
await helper.UploadFile(documentName);
1012

@@ -19,10 +21,19 @@ public static async Task Extract(ParserHelper helper, string documentName, strin
1921
else
2022
{
2123
Console.WriteLine("GetTables(): Tables successfully received from the document '{0}.", documentName);
24+
string jsonResult = "[\n";
2225
foreach (var table in response.Tables.List)
2326
{
24-
Console.WriteLine(table.ToString());
27+
var tabResp = await helper.pdfApi.GetTableAsync(documentName, table.Id, folder: remoteFolder);
28+
29+
Console.WriteLine(tabResp.Table);
30+
31+
jsonResult += JsonConvert.SerializeObject(tabResp.Table) + ",\n\n";
2532
}
33+
jsonResult += "]";
34+
string fileName = Path.Combine(localFolder, "parsed_tables_output.json");
35+
File.WriteAllText(fileName, jsonResult);
36+
Console.WriteLine("GetImages(): File '{0}' successfully downloaded.", fileName);
2637
}
2738
}
2839
}

Uses-Cases/Parser/GetTextBox.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

Uses-Cases/Parser/GetTextBoxes.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using Aspose.Pdf.Cloud.Sdk.Model;
2+
using Newtonsoft.Json;
23

34
namespace Parser
45
{
56
public class GetTextBoxes
67
{
7-
public static async Task Extract(ParserHelper helper, string documentName, string remoteFolder)
8+
public static async Task Extract(ParserHelper helper, string documentName, string localFolder, string remoteFolder)
89
{
910
await helper.UploadFile(documentName);
1011

@@ -19,10 +20,19 @@ public static async Task Extract(ParserHelper helper, string documentName, strin
1920
else
2021
{
2122
Console.WriteLine("GetTextBoxes(): TextBox fields successfully received from the document '{0}.", documentName);
22-
foreach (TextBoxField textBox in response.Fields.List)
23+
string jsonResult = "[\n";
24+
foreach (var textBox in response.Fields.List)
2325
{
24-
Console.WriteLine(textBox.ToString());
26+
var respTextBox = await helper.pdfApi.GetTextBoxFieldAsync(documentName, textBox.FullName, folder: remoteFolder);
27+
28+
Console.WriteLine(respTextBox.Field);
29+
30+
jsonResult += JsonConvert.SerializeObject(respTextBox.Field) + ",\n\n";
2531
}
32+
jsonResult += "]";
33+
string fileName = Path.Combine(localFolder, "parsed_text_boxes_output.json");
34+
File.WriteAllText(fileName, jsonResult);
35+
Console.WriteLine("GetTextBoxes(): File '{0}' successfully downloaded.", fileName);
2636
}
2737
}
2838
}

Uses-Cases/Parser/ParserHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ConfigParams
1515
public string FDF_OUTPUT { get; } = "output_sample.fdf";
1616
public string XML_OUTPUT { get; } = "output_sample.xml";
1717

18-
public int PAGE_NUMBER { get; } = 2;
18+
public int PAGE_NUMBER { get; } = 1;
1919

2020
public string TEXT_BOX_FIELD_NAME { get; } = "Signature_1";
2121
public string TABLE_ID { get; } = "GE5TCOZTHAZCYMRUGMWDKOBXFQZDMNY";
@@ -65,4 +65,5 @@ public async Task DownloadFile(string fileName, string outputName, string output
6565
Console.WriteLine("DownloadFile(): File '{0}' successfully downloaded.", outputPrefix + outputName);
6666
}
6767
}
68-
}
68+
69+
}

Uses-Cases/Parser/Program.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@
33
ParserHelper helper = new ParserHelper();
44
ConfigParams config = helper.config;
55

6-
await GetTextBoxes.Extract(helper, config.PDF_DOCUMENT, config.REMOTE_TEMP_FOLDER);
6+
await GetTextBoxes.Extract(helper, config.PDF_DOCUMENT, config.LOCAL_FOLDER, config.REMOTE_TEMP_FOLDER);
77

8-
await GetTextBox.Extract(helper, config.PDF_DOCUMENT, config.TEXT_BOX_FIELD_NAME, config.REMOTE_TEMP_FOLDER);
8+
await GetTables.Extract(helper, config.PDF_DOCUMENT, config.LOCAL_FOLDER, config.REMOTE_TEMP_FOLDER);
99

10-
await GetTables.Extract(helper, config.PDF_DOCUMENT, config.REMOTE_TEMP_FOLDER);
11-
12-
await GetTable.Extract(helper, config.PDF_DOCUMENT, config.TABLE_ID, config.REMOTE_TEMP_FOLDER);
13-
14-
await GetImages.Extract(helper, config.PDF_DOCUMENT, config.PAGE_NUMBER, config.REMOTE_TEMP_FOLDER);
15-
16-
await GetImage.Extract(helper, config.PDF_DOCUMENT, config.IMAGE_ID, config.REMOTE_TEMP_FOLDER);
10+
await GetImages.Extract(helper, config.PDF_DOCUMENT, config.PAGE_NUMBER, config.LOCAL_FOLDER, config.REMOTE_TEMP_FOLDER);
1711

1812
await ExportFormToFdf.Extract(helper, config.PDF_DOCUMENT, config.FDF_OUTPUT, config.REMOTE_TEMP_FOLDER);
1913

0 commit comments

Comments
 (0)