@@ -99,33 +99,46 @@ public virtual void KeywordsInfoTestPdfA2b() {
9999 }
100100
101101 [ NUnit . Framework . Test ]
102- public virtual void SaveAndReadDocumentWithCanonicalXmpMetadata ( )
103- {
102+ public virtual void SaveAndReadDocumentWithCanonicalXmpMetadata ( ) {
104103 String outFile = destinationFolder + "saveAndReadDocumentWithCanonicalXmpMetadata.pdf" ;
104+ String cmpFile = cmpFolder + "cmp_saveAndReadDocumentWithCanonicalXmpMetadata.pdf" ;
105105 PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel . PDF_A_2B ;
106- Stream @is = new FileStream ( sourceFolder + "sRGB Color Space Profile.icm" , FileMode . Open , FileAccess . Read ) ;
107- PdfOutputIntent outputIntent = new PdfOutputIntent ( "Custom" , "" , "http://www.color.org" , "sRGB IEC61966-2.1"
108- , @is ) ;
109- PdfADocument doc = new PdfADocument ( new PdfWriter ( outFile ) , conformanceLevel , outputIntent ) ;
110- doc . AddNewPage ( ) ;
111- XMPMeta xmp = XMPMetaFactory . Create ( ) ;
112- xmp . SetProperty ( XMPConst . NS_PDFA_ID , XMPConst . PART , conformanceLevel . GetPart ( ) , new PropertyOptions ( ) . SetSchemaNode ( true ) ) ;
113- xmp . SetProperty ( XMPConst . NS_PDFA_ID , XMPConst . CONFORMANCE , conformanceLevel . GetConformance ( ) , new PropertyOptions ( ) . SetSchemaNode ( true ) ) ;
114- var options = new SerializeOptions ( ) . SetUseCanonicalFormat ( true ) . SetUseCompactFormat ( false ) ;
115- doc . SetXmpMetadata ( xmp , options ) ;
116- doc . SetTagged ( ) ;
117- doc . Close ( ) ;
118- PdfReader reader = new PdfReader ( outFile ) ;
119- PdfDocument resultDoc = new PdfDocument ( reader ) ;
120- PdfAConformanceLevel conformanceLevelResult = resultDoc . GetReader ( ) . GetPdfAConformanceLevel ( ) ;
121- NUnit . Framework . Assert . IsNotNull ( conformanceLevelResult ) ;
122- NUnit . Framework . Assert . AreEqual ( conformanceLevel . GetConformance ( ) , conformanceLevelResult . GetConformance ( ) ) ;
123- NUnit . Framework . Assert . AreEqual ( conformanceLevel . GetPart ( ) , conformanceLevelResult . GetPart ( ) ) ;
124- byte [ ] xmpResultBytes = resultDoc . GetXmpMetadata ( ) ;
125- resultDoc . Close ( ) ;
126- NUnit . Framework . Assert . IsNotNull ( xmpResultBytes ) ;
127- XMPMeta xmpResult = XMPMetaFactory . ParseFromBuffer ( xmpResultBytes ) ;
128- NUnit . Framework . Assert . IsNotNull ( xmpResult ) ;
106+ PdfOutputIntent outputIntent ;
107+ using ( Stream @is = new FileStream ( sourceFolder + "sRGB Color Space Profile.icm" , FileMode . Open , FileAccess . Read
108+ ) ) {
109+ outputIntent = new PdfOutputIntent ( "Custom" , "" , "http://www.color.org" , "sRGB IEC61966-2.1" , @is ) ;
110+ }
111+ using ( PdfADocument doc = new PdfADocument ( new PdfWriter ( outFile ) , conformanceLevel , outputIntent ) ) {
112+ doc . AddNewPage ( ) ;
113+ XMPMeta xmp = XMPMetaFactory . Create ( ) ;
114+ xmp . SetProperty ( XMPConst . NS_PDFA_ID , XMPConst . PART , conformanceLevel . GetPart ( ) , new PropertyOptions ( ) . SetSchemaNode
115+ ( true ) ) ;
116+ xmp . SetProperty ( XMPConst . NS_PDFA_ID , XMPConst . CONFORMANCE , conformanceLevel . GetConformance ( ) , new PropertyOptions
117+ ( ) . SetSchemaNode ( true ) ) ;
118+ SerializeOptions options = new SerializeOptions ( ) . SetUseCanonicalFormat ( true ) . SetUseCompactFormat ( false ) ;
119+ doc . SetXmpMetadata ( xmp , options ) ;
120+ doc . SetTagged ( ) ;
121+ }
122+ // Closing document and reopening it to flush it XMP metadata ModifyDate
123+ using ( PdfDocument doc_1 = new PdfDocument ( new PdfReader ( outFile ) ) ) {
124+ using ( PdfDocument cmpDoc = new PdfDocument ( new PdfReader ( cmpFile ) ) ) {
125+ byte [ ] rdf = doc_1 . GetXmpMetadata ( ) ;
126+ byte [ ] expectedRdf = cmpDoc . GetXmpMetadata ( ) ;
127+ // Comparing angle brackets, since it's the main difference between canonical and compact format.
128+ NUnit . Framework . Assert . AreEqual ( Count ( expectedRdf , ( byte ) '<' ) , Count ( rdf , ( byte ) '<' ) ) ;
129+ NUnit . Framework . Assert . IsNull ( new CompareTool ( ) . CompareXmp ( cmpFile , outFile , true ) ) ;
130+ }
131+ }
132+ }
133+
134+ private int Count ( byte [ ] array , byte b ) {
135+ int counter = 0 ;
136+ foreach ( byte each in array ) {
137+ if ( each == b ) {
138+ counter ++ ;
139+ }
140+ }
141+ return counter ;
129142 }
130143 }
131144}
0 commit comments