java 未找到 PDF 标题签名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33074328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:08:53  来源:igfitidea点击:

PDF header signature not found

javaitext

提问by Nitesh Virani

I have below iTextpdf creation code, basically I need PdfStamperin my later part of code to render html content but it is throwing exception InvalidPdfExceptionwhile creating PdfStamper:

我有以下iTextpdf 创建代码,基本上我需要PdfStamper在我后面的代码部分来呈现 html 内容,但它InvalidPdfException在创建时抛出异常PdfStamper

public static void main(String[] args) throws IOException, DocumentException {

        String TEMP_PDF = "temp.pdf";
        String RESULT = "output1.pdf";
        OutputStream osTemp = null;
        OutputStream osResult = null;
        PdfWriter writer = null;
        PdfReader reader=null;
        PdfStamper stamper=null;
        Document document = new Document(PageSize.LETTER);

        try {
            osTemp = new FileOutputStream(TEMP_PDF);
            osResult = new FileOutputStream(RESULT);
            writer = PdfWriter.getInstance(document, osTemp);
            reader = new PdfReader(TEMP_PDF);
            stamper = new PdfStamper(reader, osResult);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            osTemp.close();
            osResult.close();
            writer.close();
            reader.close();
            stamper.close();
        }
    }

EDIT:

编辑:

public void createPdf(String file) throws DocumentException, IOException {
        Document document = new Document(PageSize.LETTER);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        int[] coords = new int[] {1, 50, 50, 100, 100} ;
        PdfContentByte canvas = pdfWriter.getDirectContent();
        ColumnText columnText = new ColumnText(canvas);
        String css = "";
        //llx, lly, urx, ury
        columnText.setSimpleColumn(coords[1], coords[2], coords[3], coords[4]);
        ElementList elements = XMLWorkerHelper.parseToElementList("<html><body><b>Bold text</b></body></html>", css);
        for (Element element : elements) {
            columnText.addElement(element);
        }
        columnText.go();
        document.close();
    }

回答by Bruno Lowagie

You have two problems:

你有两个问题:

Problem 1:

问题1:

PdfReadercan only read real PDF files. Those are files that start with %PDF-1and end with %%EOF. In your case, you don't have such a file. You are reading TEMP_PDFwhich is a file with 0 bytes. In iText, you create a PDF file in 5 steps. You only have step 1 (creating the document) and step 2 (creating the writer) of the creating process. You are missing steps 3 (opening the document), 4 (adding content) and 5 (closing the document).

PdfReader只能阅读真正的PDF文件。这些是以 . 开头%PDF-1和结尾的文件%%EOF。在您的情况下,您没有这样的文件。您正在阅读TEMP_PDF这是一个 0 字节的文件。在 iText 中,您可以通过 5 个步骤创建 PDF 文件。您只有创建过程的第 1 步(创建文档)和第 2 步(创建作者)。您缺少步骤 3(打开文档)、4(添加内容)和 5(关闭文档)。

You only have a complete PDF document after step 5. It is normal that you get an InvalidPdfExceptionas you aren't reading a complete PDF.

在第 5 步之后,您只有一个完整的 PDF 文档。InvalidPdfException因为您没有阅读完整的 PDF ,所以您得到一个是正常的。

Problem 2:

问题2:

You write: I need PdfStamperin my later part of code to render HTML content.

你写:我需要PdfStamper在我后面的代码部分呈现 HTML 内容。

This is wrong. PdfStamperis a class that can be used to stamp new content (a watermark, form field values, a header, a footer) on an existing PDF document. In no way does PdfStamperconvert PDF to HTML or render PDF as HTML.

这是错误的。PdfStamper是一个类,可用于在现有 PDF 文档上标记新内容(水印、表单字段值、页眉、页脚)。绝不PdfStamper会将 PDF 转换为 HTML 或将 PDF 呈现为 HTML。