Denzen, van Carl
2014-12-22 15:25:45 UTC
setLocalGoto doesnât work when I create a PDF/A-1B document. When I change the document to ordinary pdf (the commented-out writer code), it works as expected.
I use iText 5.5.4.
My code is:
package nl.vandenzen.pdfa1b;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ICC_Profile;
import com.itextpdf.text.pdf.PdfAConformanceLevel;
import com.itextpdf.text.pdf.PdfAWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author CDN
*/
public class PdfA {
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
PdfA pdfA = new PdfA();
try {
pdfA.test1();
} catch (FileNotFoundException ex) {
Logger.getLogger(PdfA.class.getName()).log(Level.SEVERE, null, ex);
} catch (DocumentException ex) {
Logger.getLogger(PdfA.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PdfA.class.getName()).log(Level.SEVERE, null, ex);
}
}
void test1() throws FileNotFoundException, DocumentException, IOException {
Document document;
document = new Document();
// Set conformance level to PDF/A-3B
PdfAWriter writer = PdfAWriter.getInstance(document,
new FileOutputStream("./target/pdfa4-example.pdf"),
PdfAConformanceLevel.PDF_A_1B);
/*
* PdfWriter writer = PdfWriter.getInstance(document, new
* FileOutputStream("./target/pdfa3-example.pdf"));
*/
// XMP metadata is a PDF/A requirement. The XMP metadata is constructed
// automatically from the document info.
writer.createXmpMetadata();
document.open();
// Output intent is a PDF/A requirement.
ICC_Profile icc = ICC_Profile
.getInstance(new FileInputStream(
"./src/nl/vandenzen/pdfa1b/resources/sRGB Color Space Profile.icm"));
writer.setOutputIntents("Custom", "", "http://www.color.org",
"sRGB IEC61966-2.1", icc);
// All fonts should be embedded.
Font bold10 = FontFactory.getFont(
"./src/nl/vandenzen/pdfa1b/resources/arial.ttf",
BaseFont.WINANSI, BaseFont.EMBEDDED, 10);
if (bold10.getFamilyname().equals("unknown")) {
throw new FileNotFoundException("Font file not found.");
}
final String anchorName = "a";
HashMap<String, Object> attribs = new HashMap<>();
// Create a link to another paragraph, with an embedded font as
// required for pdf/a-1b
Chunk chunkWithLink = new Chunk("See addendum A", bold10);
// attribs.put("F", 4 + 8 + 16);
chunkWithLink.setLocalGoto(anchorName);
// Adding empty attribs avoids the error message
// "com.itextpdf.text.DocumentException:
// com.itextpdf.text.pdf.PdfAConformanceException:
// An annotation dictionary shall contain the F key."
chunkWithLink.setAttributes(attribs);
Paragraph p = new Paragraph();
p.add(chunkWithLink);
Chapter chapter = new Chapter(p, 1);
chapter.setNumberDepth(0);
document.add(chapter);
// Create some space between the paragraphs
for (int i = 0; i < 140; i++) {
Chunk space = new Chunk("-----", bold10);
Paragraph ps = new Paragraph();
ps.add(space);
document.add(ps);
}
document.newPage();
// Create an anchor for the addendum
Chunk chunkAddendum = new Chunk(
"This is some extra information about something", bold10);
chunkAddendum.setLocalDestination(anchorName);
Paragraph p1 = new Paragraph();
p1.add(chunkAddendum);
document.add(p1);
document.close();
}
}
Met vriendelijke groet,
Carl van Denzen
Consultant
ma-do 09:00-17:30
[cid:***@9e3b06fa.4fa3c6ca]
E ***@PinkRoccade.nl<mailto:***@PinkRoccade.nl> | M +31 6 4339 2584 | T +31 88 660 3000 | W www.pinkroccade-healthcare.nl<http://www.pinkroccade-healthcare.nl/> | KvK 27322973 Apeldoorn
<Loading Image...
@1553752d.45836c24]<http://www.twitter.com/pinkroccade>
[cid:***@ccc9d6c3.418c7d4b]<http://www.pinkroccade-healthcare.nl/>banner
Disclaimer | The information transmitted is intended only for use by the addressee and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of it, or the taking of any action in reliance upon this information by persons and/or entities other than the intended recipient is prohibited. If you received this in error, please inform the sender and/or addressee immediately and delete the material. Thank you.
[cid:***@7866dfca.4a85bced]Please consider the environment before printing this email
I use iText 5.5.4.
My code is:
package nl.vandenzen.pdfa1b;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ICC_Profile;
import com.itextpdf.text.pdf.PdfAConformanceLevel;
import com.itextpdf.text.pdf.PdfAWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author CDN
*/
public class PdfA {
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
PdfA pdfA = new PdfA();
try {
pdfA.test1();
} catch (FileNotFoundException ex) {
Logger.getLogger(PdfA.class.getName()).log(Level.SEVERE, null, ex);
} catch (DocumentException ex) {
Logger.getLogger(PdfA.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PdfA.class.getName()).log(Level.SEVERE, null, ex);
}
}
void test1() throws FileNotFoundException, DocumentException, IOException {
Document document;
document = new Document();
// Set conformance level to PDF/A-3B
PdfAWriter writer = PdfAWriter.getInstance(document,
new FileOutputStream("./target/pdfa4-example.pdf"),
PdfAConformanceLevel.PDF_A_1B);
/*
* PdfWriter writer = PdfWriter.getInstance(document, new
* FileOutputStream("./target/pdfa3-example.pdf"));
*/
// XMP metadata is a PDF/A requirement. The XMP metadata is constructed
// automatically from the document info.
writer.createXmpMetadata();
document.open();
// Output intent is a PDF/A requirement.
ICC_Profile icc = ICC_Profile
.getInstance(new FileInputStream(
"./src/nl/vandenzen/pdfa1b/resources/sRGB Color Space Profile.icm"));
writer.setOutputIntents("Custom", "", "http://www.color.org",
"sRGB IEC61966-2.1", icc);
// All fonts should be embedded.
Font bold10 = FontFactory.getFont(
"./src/nl/vandenzen/pdfa1b/resources/arial.ttf",
BaseFont.WINANSI, BaseFont.EMBEDDED, 10);
if (bold10.getFamilyname().equals("unknown")) {
throw new FileNotFoundException("Font file not found.");
}
final String anchorName = "a";
HashMap<String, Object> attribs = new HashMap<>();
// Create a link to another paragraph, with an embedded font as
// required for pdf/a-1b
Chunk chunkWithLink = new Chunk("See addendum A", bold10);
// attribs.put("F", 4 + 8 + 16);
chunkWithLink.setLocalGoto(anchorName);
// Adding empty attribs avoids the error message
// "com.itextpdf.text.DocumentException:
// com.itextpdf.text.pdf.PdfAConformanceException:
// An annotation dictionary shall contain the F key."
chunkWithLink.setAttributes(attribs);
Paragraph p = new Paragraph();
p.add(chunkWithLink);
Chapter chapter = new Chapter(p, 1);
chapter.setNumberDepth(0);
document.add(chapter);
// Create some space between the paragraphs
for (int i = 0; i < 140; i++) {
Chunk space = new Chunk("-----", bold10);
Paragraph ps = new Paragraph();
ps.add(space);
document.add(ps);
}
document.newPage();
// Create an anchor for the addendum
Chunk chunkAddendum = new Chunk(
"This is some extra information about something", bold10);
chunkAddendum.setLocalDestination(anchorName);
Paragraph p1 = new Paragraph();
p1.add(chunkAddendum);
document.add(p1);
document.close();
}
}
Met vriendelijke groet,
Carl van Denzen
Consultant
ma-do 09:00-17:30
[cid:***@9e3b06fa.4fa3c6ca]
E ***@PinkRoccade.nl<mailto:***@PinkRoccade.nl> | M +31 6 4339 2584 | T +31 88 660 3000 | W www.pinkroccade-healthcare.nl<http://www.pinkroccade-healthcare.nl/> | KvK 27322973 Apeldoorn
<Loading Image...
[cid:***@ccc9d6c3.418c7d4b]<http://www.pinkroccade-healthcare.nl/>banner
Disclaimer | The information transmitted is intended only for use by the addressee and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of it, or the taking of any action in reliance upon this information by persons and/or entities other than the intended recipient is prohibited. If you received this in error, please inform the sender and/or addressee immediately and delete the material. Thank you.
[cid:***@7866dfca.4a85bced]Please consider the environment before printing this email