-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement-parallel-processing-to-batch-convert-doc-files-to-tiff-for-improved-conversion-t.cs
More file actions
80 lines (66 loc) · 2.93 KB
/
Copy pathimplement-parallel-processing-to-batch-convert-doc-files-to-tiff-for-improved-conversion-t.cs
File metadata and controls
80 lines (66 loc) · 2.93 KB
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
using System;
using System.IO;
using System.Threading.Tasks;
using Aspose.Words;
using Aspose.Words.Saving;
public class ParallelDocToTiffConverter
{
public static void Main()
{
// Create a temporary working directory.
string workDir = Path.Combine(Path.GetTempPath(), "AsposeWordsParallelDemo");
Directory.CreateDirectory(workDir);
// Create a subfolder for source DOC files.
string sourceDir = Path.Combine(workDir, "SourceDocs");
Directory.CreateDirectory(sourceDir);
// Create a subfolder for the resulting TIFF files.
string outputDir = Path.Combine(workDir, "TiffOutputs");
Directory.CreateDirectory(outputDir);
// Generate a few sample DOC files.
const int sampleCount = 5;
for (int i = 1; i <= sampleCount; i++)
{
string docPath = Path.Combine(sourceDir, $"SampleDocument{i}.doc");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln($"Sample Document {i} - Page 1.");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln($"Sample Document {i} - Page 2.");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln($"Sample Document {i} - Page 3.");
doc.Save(docPath);
}
// Gather all DOC files to be processed.
string[] docFiles = Directory.GetFiles(sourceDir, "*.doc");
// Convert each DOC to a multipage TIFF in parallel.
Parallel.ForEach(docFiles, docFile =>
{
// Load the source document.
Document document = new Document(docFile);
// Prepare the output TIFF path.
string tiffFileName = Path.GetFileNameWithoutExtension(docFile) + ".tiff";
string tiffPath = Path.Combine(outputDir, tiffFileName);
// Configure image save options for a multipage TIFF.
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.Tiff)
{
// Render all pages into a single multi‑frame TIFF.
PageLayout = MultiPageLayout.TiffFrames(),
// Set a reasonable resolution (dots per inch).
Resolution = 300
};
// Save the document as TIFF.
document.Save(tiffPath, saveOptions);
// Verify that the TIFF file was created.
if (!File.Exists(tiffPath))
throw new InvalidOperationException($"Failed to create TIFF file: {tiffPath}");
});
// Simple verification: list the generated TIFF files.
Console.WriteLine("Conversion completed. Generated TIFF files:");
foreach (string tiffFile in Directory.GetFiles(outputDir, "*.tiff"))
{
Console.WriteLine(tiffFile);
}
// Cleanup (optional): delete the temporary working directory.
// Directory.Delete(workDir, true);
}
}