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 ;
27using PdfSharpCore . Pdf ;
38using PdfSharpCore . Pdf . IO ;
49using PdfSharpCore . Test . Helpers ;
510using Xunit ;
11+ using Xunit . Abstractions ;
612
713namespace PdfSharpCore . Test
814{
915 public class Merge
1016 {
17+ private readonly ITestOutputHelper _output ;
18+
19+ public Merge ( ITestOutputHelper output )
20+ {
21+ _output = output ;
22+ }
23+
1124 [ Fact ]
1225 public void CanMerge2Documents ( )
1326 {
1427 var pdf1Path = PathHelper . GetInstance ( ) . GetAssetPath ( "FamilyTree.pdf" ) ;
1528 var pdf2Path = PathHelper . GetInstance ( ) . GetAssetPath ( "test.pdf" ) ;
1629
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+ {
1765 var outputDocument = new PdfDocument ( ) ;
1866
19- foreach ( var pdfPath in new [ ] { pdf1Path , pdf2Path } )
67+ foreach ( var pdfPath in pdfPaths )
2068 {
2169 using var fs = File . OpenRead ( pdfPath ) ;
2270 var inputDocument = Pdf . IO . PdfReader . Open ( fs , PdfDocumentOpenMode . Import ) ;
71+
2372 var count = inputDocument . PageCount ;
2473 for ( var idx = 0 ; idx < count ; idx ++ )
2574 {
@@ -28,14 +77,34 @@ public void CanMerge2Documents()
2877 }
2978 }
3079
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 ) ;
3286 var dir = Path . GetDirectoryName ( outFilePath ) ;
3387 if ( ! Directory . Exists ( dir ) )
3488 {
3589 Directory . CreateDirectory ( dir ) ;
3690 }
3791
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 ;
39108 }
40109 }
41110}
0 commit comments