|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package com.itextpdf.kernel.pdf; |
| 7 | + |
| 8 | +import com.itextpdf.test.ExtendedITextTest; |
| 9 | +import static com.itextpdf.test.ITextTest.createOrClearDestinationFolder; |
| 10 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | + |
| 14 | +import org.junit.BeforeClass; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.experimental.categories.Category; |
| 17 | + |
| 18 | +import junit.framework.Assert; |
| 19 | + |
| 20 | +/** |
| 21 | + * |
| 22 | + * @author benoit |
| 23 | + */ |
| 24 | +@Category(IntegrationTest.class) |
| 25 | +public class PdfXrefTableTest extends ExtendedITextTest { |
| 26 | + |
| 27 | + public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/PdfXrefTableTest/"; |
| 28 | + public static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/PdfXrefTableTest/"; |
| 29 | + |
| 30 | + @BeforeClass |
| 31 | + public static void beforeClass() { |
| 32 | + createOrClearDestinationFolder(destinationFolder); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testCreateAndUpdateXMP() throws IOException { |
| 37 | + String created = destinationFolder + "testCreateAndUpdateXMP_create.pdf"; |
| 38 | + String updated = destinationFolder + "testCreateAndUpdateXMP_update.pdf"; |
| 39 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(created)); |
| 40 | + pdfDocument.addNewPage(); |
| 41 | + |
| 42 | + pdfDocument.getXmpMetadata(true); // create XMP metadata |
| 43 | + pdfDocument.close(); |
| 44 | + |
| 45 | + pdfDocument = new PdfDocument(new PdfReader(created), new PdfWriter(updated)); |
| 46 | + pdfDocument.close(); |
| 47 | + pdfDocument = new PdfDocument(new PdfReader(updated)); |
| 48 | + PdfXrefTable xref = pdfDocument.getXref(); |
| 49 | + PdfIndirectReference freeRef = xref.get(xref.size() - 2); // 6 |
| 50 | + |
| 51 | + /* |
| 52 | + Current xref structure: |
| 53 | + xref |
| 54 | + 0 8 |
| 55 | + 0000000006 65535 f % this is object 0; 6 refers to free object 6 |
| 56 | + 0000000203 00000 n |
| 57 | + 0000000510 00000 n |
| 58 | + 0000000263 00000 n |
| 59 | + 0000000088 00000 n |
| 60 | + 0000000015 00000 n |
| 61 | + 0000000000 00001 f % this is object 6; 0 refers to free object 0; note generation number |
| 62 | + 0000000561 00000 n |
| 63 | + */ |
| 64 | + |
| 65 | + Assert.assertTrue(freeRef.isFree()); |
| 66 | + Assert.assertEquals(xref.get(0).offsetOrIndex, freeRef.objNr); |
| 67 | + Assert.assertEquals(1, freeRef.genNr); |
| 68 | + pdfDocument.close(); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testCreateAndUpdateTwiceXMP() throws IOException { |
| 74 | + String created = destinationFolder + "testCreateAndUpdateTwiceXMP_create.pdf"; |
| 75 | + String updated = destinationFolder + "testCreateAndUpdateTwiceXMP_update.pdf"; |
| 76 | + String updatedAgain = destinationFolder + "testCreateAndUpdateTwiceXMP_updatedAgain.pdf"; |
| 77 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(created)); |
| 78 | + pdfDocument.addNewPage(); |
| 79 | + |
| 80 | + pdfDocument.getXmpMetadata(true); // create XMP metadata |
| 81 | + pdfDocument.close(); |
| 82 | + |
| 83 | + pdfDocument = new PdfDocument(new PdfReader(created), new PdfWriter(updated)); |
| 84 | + pdfDocument.close(); |
| 85 | + pdfDocument = new PdfDocument(new PdfReader(updated), new PdfWriter(updatedAgain)); |
| 86 | + pdfDocument.close(); |
| 87 | + pdfDocument = new PdfDocument(new PdfReader(updatedAgain)); |
| 88 | + PdfXrefTable xref = pdfDocument.getXref(); |
| 89 | + PdfIndirectReference freeRef1 = xref.get(xref.size() - 3); // 6 |
| 90 | + PdfIndirectReference freeRef2 = xref.get(xref.size() - 2); // 7 |
| 91 | + |
| 92 | + /* |
| 93 | + Current xref structure: |
| 94 | + xref |
| 95 | + 0 9 |
| 96 | + 0000000006 65535 f % this is object 0; 6 refers to free object 6 |
| 97 | + 0000000203 00000 n |
| 98 | + 0000000510 00000 n |
| 99 | + 0000000263 00000 n |
| 100 | + 0000000088 00000 n |
| 101 | + 0000000015 00000 n |
| 102 | + 0000000007 00002 f % this is object 6; 7 refers to free object 7; note generation number |
| 103 | + 0000000000 00001 f % this is object 7; 0 refers to free object 0; note generation number |
| 104 | + 0000000561 00000 n |
| 105 | + */ |
| 106 | + |
| 107 | + Assert.assertTrue(freeRef1.isFree()); |
| 108 | + Assert.assertEquals(xref.get(0).offsetOrIndex, freeRef1.objNr); |
| 109 | + Assert.assertEquals(2, freeRef1.genNr); |
| 110 | + Assert.assertTrue(freeRef2.isFree()); |
| 111 | + Assert.assertEquals(freeRef1.offsetOrIndex, freeRef2.objNr); |
| 112 | + Assert.assertEquals(1, freeRef2.genNr); |
| 113 | + pdfDocument.close(); |
| 114 | + } |
| 115 | +} |
0 commit comments