0

このコメントのコードをテストしましたが、例外がスローされるためリンクを追加できません。

問題を再現するには、html へのリンクのみを追加します。Main.java にこの行のみを追加しました

buf.append("<a href='http://google.com'>link to google</a>");

スタイル.css

* {
    font-family: "Arial";
    font-style: normal;
}

Main.java

package com.itextpdf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.ICC_Profile;
import com.itextpdf.text.pdf.PdfAConformanceLevel;
import com.itextpdf.text.pdf.PdfAWriter;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.CssAppliers;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;

public class Main {

    public static void main(String[] args) {

        StringBuffer buf = new StringBuffer();

        String title = "Test";

        // Sample HTML content.
        buf.append("<!DOCTYPE html>");
        buf.append("<html>");
        buf.append("<head>");
        buf.append("<title>" + title + "</title>");
        buf.append("</head>");
        buf.append("<body>");
        buf.append("<p>This is a test</p>");
        buf.append("<a href='http://google.com'>link to google</a>");  //<----- Only added this line
        buf.append("</body>");
        buf.append("</html>");

        OutputStream file = null;
        Document document = null;
        PdfAWriter writer = null;

        try {

            file = new FileOutputStream(new File("C:\\Users\\amartin\\Desktop\\Test.pdf"));
            document = new Document();
            writer = PdfAWriter.getInstance(document, file, PdfAConformanceLevel.PDF_A_1B);

            // Avoid discrepances between document title and XMP metadata information.
            document.addTitle(title);

            // Create XMP metadata. It's a PDF/A requirement.
            writer.createXmpMetadata();

            document.open();

            // Set output intent. PDF/A requirement.
            ICC_Profile icc = ICC_Profile.getInstance(new FileInputStream("./src/main/resources/com/itextpdf/sRGB Color Space Profile.icm"));
            writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);

            // CSS stylesheet.
            CSSResolver cssResolver = new StyleAttrCSSResolver();
            CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream("./css/style.css"));
            cssResolver.addCss(cssFile);

            MyFontProvider fontProvider = new MyFontProvider();
            fontProvider.register("./fonts/arial.ttf");

            /* DEBUG
            System.out.println("Fonts present in " + fontProvider.getClass().getName());
            Set<String> registeredFonts = fontProvider.getRegisteredFonts();
            for (String font : registeredFonts)
                System.out.println(font);
            */

            CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
            HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
            htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

            // Pipelines.
            PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
            HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
            CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

            XMLWorker worker = new XMLWorker(css, true);
            XMLParser p = new XMLParser(worker);

            Reader reader = new StringReader(buf.toString());
            p.parse(reader);

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if (document != null && document.isOpen())
                document.close();

            try {

                if (file != null)
                    file.close();

            } catch (IOException e) {}

            if (writer != null && !writer.isCloseStream())
                writer.close();

        }

    }

}

MyFontProvider.java

package com.itextpdf;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactoryImp;

public class MyFontProvider extends FontFactoryImp {

    @Override
    public Font getFont(String fontname, String encoding, boolean embedded,
            float size, int style, BaseColor color) {

        System.out.println("=fontname: " + fontname + " =encoding: " + encoding + " =embedded : " + embedded + " =size: " + size + " =style: " + style + " =BaseColor: " + color);

        return super.getFont(fontname, encoding, embedded, size, style, color);

    }

}

例外

Exception in thread "main" com.itextpdf.text.pdf.PdfAConformanceException: An annotation dictionary shall contain the F key.
    at com.itextpdf.text.pdf.internal.PdfA1Checker.checkAnnotation(PdfA1Checker.java:422)
    at com.itextpdf.text.pdf.internal.PdfAChecker.checkPdfAConformance(PdfAChecker.java:219)
    at com.itextpdf.text.pdf.internal.PdfAConformanceImp.checkPdfIsoConformance(PdfAConformanceImp.java:71)
    at com.itextpdf.text.pdf.PdfWriter.checkPdfIsoConformance(PdfWriter.java:3426)
    at com.itextpdf.text.pdf.PdfWriter.checkPdfIsoConformance(PdfWriter.java:3422)
    at com.itextpdf.text.pdf.PdfAnnotation.toPdf(PdfAnnotation.java:999)
    at com.itextpdf.text.pdf.PdfIndirectObject.writeTo(PdfIndirectObject.java:158)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.write(PdfWriter.java:420)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:398)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:373)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:369)
    at com.itextpdf.text.pdf.PdfWriter.addToBody(PdfWriter.java:843)
    at com.itextpdf.text.pdf.internal.PdfAnnotationsImp.rotateAnnotations(PdfAnnotationsImp.java:209)
    at com.itextpdf.text.pdf.PdfDocument.newPage(PdfDocument.java:990)
    at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:865)
    at com.itextpdf.text.Document.close(Document.java:416)
    at com.itextpdf.Main.main(Main.java:113)
4

1 に答える 1

3

問題を解決する方法:

  • ブルーノが言ったように、バージョン 5.5.7 に更新すると問題は解決します。
  • ライブラリを更新できない場合は、テキスト リンクを<span></span>
于 2015-10-15T10:18:56.443 に答える