1
- using System . IO ;
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . IO ;
4
+ using System . Linq ;
5
+ using PdfSharpCore . Drawing ;
6
+ using PdfSharpCore . Drawing . Layout ;
2
7
using PdfSharpCore . Pdf ;
3
8
using PdfSharpCore . Pdf . IO ;
4
9
using PdfSharpCore . Test . Helpers ;
5
10
using Xunit ;
11
+ using Xunit . Abstractions ;
6
12
7
13
namespace PdfSharpCore . Test
8
14
{
9
15
public class Merge
10
16
{
17
+ private readonly ITestOutputHelper _output ;
18
+
19
+ public Merge ( ITestOutputHelper output )
20
+ {
21
+ _output = output ;
22
+ }
23
+
11
24
[ Fact ]
12
25
public void CanMerge2Documents ( )
13
26
{
14
27
var pdf1Path = PathHelper . GetInstance ( ) . GetAssetPath ( "FamilyTree.pdf" ) ;
15
28
var pdf2Path = PathHelper . GetInstance ( ) . GetAssetPath ( "test.pdf" ) ;
16
29
30
+ var outputDocument = MergeDocuments ( new [ ] { pdf1Path , pdf2Path } ) ;
31
+
32
+ var outFilePath = CreateOutFilePath ( "merge.pdf" ) ;
33
+ outputDocument . Save ( outFilePath ) ;
34
+ }
35
+
36
+ [ Fact ]
37
+ public void CanConsolidateImageDataInDocument ( )
38
+ {
39
+ var doc1 = CreateTestDocumentWithImage ( "lenna.png" ) ;
40
+ var doc2 = CreateTestDocumentWithImage ( "frog-and-toad.jpg" ) ;
41
+
42
+ var pdf1Path = CreateOutFilePath ( "image-doc1.pdf" ) ;
43
+ doc1 . Save ( pdf1Path ) ;
44
+
45
+ var pdf2Path = CreateOutFilePath ( "image-doc2.pdf" ) ;
46
+ doc2 . Save ( pdf2Path ) ;
47
+
48
+ var pdfPathsForMerge = Enumerable . Range ( 1 , 50 ) . SelectMany ( _ => new [ ] { pdf1Path , pdf2Path } ) ;
49
+ var outputDocument = MergeDocuments ( pdfPathsForMerge ) ;
50
+
51
+ var mergedFilePath = CreateOutFilePath ( "images-merged.pdf" ) ;
52
+ outputDocument . Save ( mergedFilePath ) ;
53
+
54
+ outputDocument . ConsolidateImages ( ) ;
55
+ var consolidatedFilePath = CreateOutFilePath ( "images-merged-consolidated.pdf" ) ;
56
+ outputDocument . Save ( consolidatedFilePath ) ;
57
+
58
+ long mergedLength = new FileInfo ( mergedFilePath ) . Length ;
59
+ long consolidatedLength = new FileInfo ( consolidatedFilePath ) . Length ;
60
+ Assert . True ( consolidatedLength < mergedLength / 4 ) ;
61
+ }
62
+
63
+ private static PdfDocument MergeDocuments ( IEnumerable < string > pdfPaths )
64
+ {
17
65
var outputDocument = new PdfDocument ( ) ;
18
66
19
- foreach ( var pdfPath in new [ ] { pdf1Path , pdf2Path } )
67
+ foreach ( var pdfPath in pdfPaths )
20
68
{
21
69
using var fs = File . OpenRead ( pdfPath ) ;
22
70
var inputDocument = Pdf . IO . PdfReader . Open ( fs , PdfDocumentOpenMode . Import ) ;
71
+
23
72
var count = inputDocument . PageCount ;
24
73
for ( var idx = 0 ; idx < count ; idx ++ )
25
74
{
@@ -28,14 +77,34 @@ public void CanMerge2Documents()
28
77
}
29
78
}
30
79
31
- var outFilePath = Path . Combine ( PathHelper . GetInstance ( ) . RootDir , "Out" , "merge.pdf" ) ;
80
+ return outputDocument ;
81
+ }
82
+
83
+ private static string CreateOutFilePath ( string filename )
84
+ {
85
+ var outFilePath = Path . Combine ( PathHelper . GetInstance ( ) . RootDir , "Out" , filename ) ;
32
86
var dir = Path . GetDirectoryName ( outFilePath ) ;
33
87
if ( ! Directory . Exists ( dir ) )
34
88
{
35
89
Directory . CreateDirectory ( dir ) ;
36
90
}
37
91
38
- outputDocument . Save ( outFilePath ) ;
92
+ return outFilePath ;
93
+ }
94
+
95
+ private static PdfDocument CreateTestDocumentWithImage ( string imageFilename )
96
+ {
97
+ var document = new PdfDocument ( ) ;
98
+
99
+ var pageNewRenderer = document . AddPage ( ) ;
100
+ var renderer = XGraphics . FromPdfPage ( pageNewRenderer ) ;
101
+ var textFormatter = new XTextFormatter ( renderer ) ;
102
+
103
+ var layout = new XRect ( 12 , 12 , 400 , 50 ) ;
104
+ textFormatter . DrawString ( imageFilename , new XFont ( "Arial" , 12 ) , XBrushes . Black , layout ) ;
105
+ renderer . DrawImage ( XImage . FromFile ( PathHelper . GetInstance ( ) . GetAssetPath ( imageFilename ) ) , new XPoint ( 12 , 100 ) ) ;
106
+
107
+ return document ;
39
108
}
40
109
}
41
110
}
0 commit comments