Skip to content

Instantly share code, notes, and snippets.

@Vad1mo
Last active August 29, 2015 13:56
Show Gist options
  • Save Vad1mo/b1759fc3ba234d79009e to your computer and use it in GitHub Desktop.
Save Vad1mo/b1759fc3ba234d79009e to your computer and use it in GitHub Desktop.
Attach file to Pdf/A-3B test case
package itext.playground;
import static com.itextpdf.text.pdf.AFRelationshipValue.Alternative;
import static com.itextpdf.text.pdf.PdfName.AFRELATIONSHIP;
import static com.itextpdf.text.pdf.PdfName.MODDATE;
import static com.itextpdf.text.pdf.PdfName.PARAMS;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Before;
import org.junit.Test;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfAConformanceLevel;
import com.itextpdf.text.pdf.PdfAStamper;
import com.itextpdf.text.pdf.PdfDate;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
public class TestCaseForSo {
byte[] content;
InputStream inPdf;
@Before
public void setup(){
content = "<xml>This is a simle text attachment in </xml>".getBytes();
inPdf = getClass().getResourceAsStream("/Existing_PDFA3.pdf");
}
@Test
public void append() throws IOException, DocumentException {
ByteArrayOutputStream result = new ByteArrayOutputStream(16000);
PdfReader reader = new PdfReader(inPdf);
PdfAStamper stamper = new PdfAStamper(reader, result, PdfAConformanceLevel.PDF_A_3B);
stamper.createXmpMetadata();
// Creating PDF/A-3 compliant attachment.
PdfDictionary embeddedFileParams = new PdfDictionary();
embeddedFileParams.put(PARAMS, new PdfName("ZF_NAME"));
embeddedFileParams.put(MODDATE, new PdfDate());
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(stamper.getWriter(), null,"ZF_NAME", content , "text/xml", embeddedFileParams,0);
fs.put(AFRELATIONSHIP, Alternative);
stamper.addFileAttachment("file description",fs);
stamper.close();
reader.close();
assertNotNull(result.toByteArray());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment