我有以下示例,我遇到Exception in thread "main" com.itextpdf.text.pdf.PdfAConformanceException: Annotation of type /Link should have Contents key.
Exception
。
我正在尝试使用外部链接在PDF
文档中添加一些文本作为链接(锚点)。
我添加链接(锚点)的方法有什么问题吗?
我错过了什么吗?
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Anchor;
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.List;
import com.itextpdf.text.ListItem;
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 com.itextpdf.text.xml.xmp.DublinCoreProperties;
import com.itextpdf.text.xml.xmp.PdfProperties;
import com.itextpdf.text.xml.xmp.XmpWriter;
import com.itextpdf.xmp.XMPException;
public class SquareBullet {
public static void main(String[] args) throws IOException, DocumentException, XMPException {
Document document = new Document();
PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream("list.pdf"), PdfAConformanceLevel.PDF_A_1A);
writer.setViewerPreferences(PdfAWriter.PageModeUseOutlines);
writer.setRunDirection(PdfAWriter.RUN_DIRECTION_LTR);
writer.setTagged(PdfAWriter.markAll);
writer.createXmpMetadata();
XmpWriter xmp = writer.getXmpWriter();
DublinCoreProperties.addSubject(xmp.getXmpMeta(), "Subject");
DublinCoreProperties.setTitle(xmp.getXmpMeta(), "Title", "en_US", "en_US");
DublinCoreProperties.setDescription(xmp.getXmpMeta(), "Description", "en_US", "en_US");
PdfProperties.setKeywords(xmp.getXmpMeta(), "Keywords");
PdfProperties.setVersion(xmp.getXmpMeta(), "1.4");
document.addLanguage("en_US");
document.open();
Font font = FontFactory.getFont("ZapfDingbats.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 8);
Font font1 = FontFactory.getFont("ROsanswebtextregular.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
ICC_Profile icc = ICC_Profile.getInstance(new FileInputStream("sRGB Color Space Profile.icm"));
writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
List list = new List(10);
Chunk c = new Chunk(String.valueOf((char)110), font);
list.setListSymbol(c);
list.add(new ListItem(new Chunk("Test 1", font1)));
list.add(new ListItem(new Chunk("Test 2", font1)));
list.add(new ListItem(new Chunk("Test 3", font1)));
document.add(list);
Paragraph anchorP = new Paragraph();
Anchor anchor = new Anchor("Google", font);
anchor.setReference("https://google.com");
anchor.setName("Google");
anchorP.add(anchor);
document.add(anchorP);
document.close();
}
}
答案 0 :(得分:0)
我使用下面的代码解决了这个问题,希望这有助于某人!!
public class SquareBullet {
public static void main(String[] args) throws IOException, DocumentException, XMPException {
Document document = new Document();
PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream("list.pdf"), PdfAConformanceLevel.PDF_A_1A);
writer.setViewerPreferences(PdfAWriter.PageModeUseOutlines);
writer.setRunDirection(PdfAWriter.RUN_DIRECTION_LTR);
writer.setTagged(PdfAWriter.markAll);
writer.createXmpMetadata();
XmpWriter xmp = writer.getXmpWriter();
DublinCoreProperties.addSubject(xmp.getXmpMeta(), "Subject");
DublinCoreProperties.setTitle(xmp.getXmpMeta(), "Title", "en_US", "en_US");
DublinCoreProperties.setDescription(xmp.getXmpMeta(), "Description", "en_US", "en_US");
PdfProperties.setKeywords(xmp.getXmpMeta(), "Keywords");
PdfProperties.setVersion(xmp.getXmpMeta(), "1.4");
document.addLanguage("en_US");
document.open();
Font font1 = FontFactory.getFont("ROsanswebtextregular.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
ICC_Profile icc = ICC_Profile.getInstance(new FileInputStream("sRGB Color Space Profile.icm"));
writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
Chunk c = new Chunk("Google", font1);
PdfAction action = new PdfAction("https://google.com");
PdfAnnotation annotation = PdfAnnotation.createLink(
writer, new Rectangle(0 ,0), PdfName.HIGHLIGHT,
action);
annotation.put(PdfName.CONTENTS, PdfName.TEXT);
annotation.setBorderStyle(new PdfBorderDictionary(0, PdfBorderDictionary.STYLE_BEVELED));
c.setAnnotation(annotation);
document.add(c);
document.close();
}
}