@@ -238,9 +238,11 @@ void testCreateFromFileLock() throws IOException
238238 String tiffG3Path = "src/test/resources/org/apache/pdfbox/pdmodel/graphics/image/ccittg3.tif" ;
239239 File copiedTiffFile = new File (TESTRESULTSDIR , "ccittg3.tif" );
240240 Files .copy (new File (tiffG3Path ).toPath (), copiedTiffFile .toPath (), StandardCopyOption .REPLACE_EXISTING );
241- PDDocument document = new PDDocument ();
242- CCITTFactory .createFromFile (document , copiedTiffFile );
243- assertTrue (copiedTiffFile .delete ());
241+ try (PDDocument document = new PDDocument ())
242+ {
243+ CCITTFactory .createFromFile (document , copiedTiffFile );
244+ assertTrue (copiedTiffFile .delete ());
245+ }
244246 }
245247
246248 /**
@@ -254,9 +256,11 @@ void testCreateFromFileNumberLock() throws IOException
254256 String tiffG3Path = "src/test/resources/org/apache/pdfbox/pdmodel/graphics/image/ccittg3.tif" ;
255257 File copiedTiffFile = new File (TESTRESULTSDIR , "ccittg3n.tif" );
256258 Files .copy (new File (tiffG3Path ).toPath (), copiedTiffFile .toPath (), StandardCopyOption .REPLACE_EXISTING );
257- PDDocument document = new PDDocument ();
258- CCITTFactory .createFromFile (document , copiedTiffFile , 0 );
259- assertTrue (copiedTiffFile .delete ());
259+ try (PDDocument document = new PDDocument ())
260+ {
261+ CCITTFactory .createFromFile (document , copiedTiffFile , 0 );
262+ assertTrue (copiedTiffFile .delete ());
263+ }
260264 }
261265
262266 /**
@@ -292,23 +296,24 @@ void testFillOrder2() throws IOException, URISyntaxException
292296 {
293297 ba = is .readAllBytes ();
294298 }
295- PDDocument document = new PDDocument ();
296- PDImageXObject ximg = CCITTFactory .createFromByteArray (document , ba );
297-
298- validate (ximg , 1 , 4575 , 2232 , "tiff" , PDDeviceGray .INSTANCE .getName ());
299- BufferedImage bim = ImageIO .read (new ByteArrayInputStream (ba ));
300- checkIdent (bim , ximg .getOpaqueImage (null , 1 ));
301- PDPage page = new PDPage (PDRectangle .A4 );
302- document .addPage (page );
303- try (PDPageContentStream contentStream = new PDPageContentStream (document , page , AppendMode .APPEND , false ))
299+ try (PDDocument document = new PDDocument ())
304300 {
305- contentStream .drawImage (ximg , 0 , 0 , ximg .getWidth () / 8 , ximg .getHeight () / 8 );
301+ PDImageXObject ximg = CCITTFactory .createFromByteArray (document , ba );
302+ validate (ximg , 1 , 4575 , 2232 , "tiff" , PDDeviceGray .INSTANCE .getName ());
303+ BufferedImage bim = ImageIO .read (new ByteArrayInputStream (ba ));
304+ checkIdent (bim , ximg .getOpaqueImage (null , 1 ));
305+ PDPage page = new PDPage (PDRectangle .A4 );
306+ document .addPage (page );
307+ try (PDPageContentStream contentStream = new PDPageContentStream (document , page , AppendMode .APPEND , false ))
308+ {
309+ contentStream .drawImage (ximg , 0 , 0 , ximg .getWidth () / 8 , ximg .getHeight () / 8 );
310+ }
311+ document .save (TESTRESULTSDIR + "/Wing.pdf" );
306312 }
307- document .save (TESTRESULTSDIR + "/Wing.pdf" );
308- document .close ();
309313
310- document = Loader .loadPDF (new File (TESTRESULTSDIR , "Wing.pdf" ));
311- assertEquals (1 , document .getNumberOfPages ());
312- document .close ();
314+ try (PDDocument document = Loader .loadPDF (new File (TESTRESULTSDIR , "Wing.pdf" )))
315+ {
316+ assertEquals (1 , document .getNumberOfPages ());
317+ }
313318 }
314319}
0 commit comments