使用PDFBoxUtil生成的PDF在第一次打印命令时给出错误

时间:2014-02-12 08:01:19

标签: pdf-generation

The code used is:
public static PDDocument getPDFDocument(InputStream pdfLocation, String xfdf) {
    FDFDocument xfdfDocument = null;
    PDDocument pdfDocument;
    try {
        if (pdfLocation != null && xfdf != null
                && xfdf.trim().length() != 0) {

            pdfDocument = PDDocument.load(pdfLocation);
            xfdfDocument = FDFDocument.loadXFDF(new   ByteArrayInputStream(
                    xfdf.getBytes("UTF-8")));
            PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
            PDAcroForm acroForm = docCatalog.getAcroForm();
            acroForm.setCacheFields(true);
            acroForm.importFDF(xfdfDocument);

            try {
                // plog 96545 edit font size and spacing
                PDDocument tempDoc = acroForm.getDocument();
                PDAcroForm tempForm = tempDoc.getDocumentCatalog()
                        .getAcroForm();

                for (int i = 0; i < tempForm.getFields().size(); i++) {
                    PDField field = (PDField) tempForm.getFields().get(i);
                    if (field.getValue() != null) {
                        setAppearanceValue(field, field.getValue());
                    }

                }

                acroForm = tempForm;
            } catch (Exception e1) {
                _log
                        .error("Error occured while changing font size of form - "
                                + e1.getMessage());
            }
            // end plog 96545

            PDDocument mergedDocument = acroForm.getDocument();
            return mergedDocument;

        } else {
            return null;
        }
    } catch (IOException e) {
        throw new bnsException(e.getMessage());
    } finally {
        if (xfdfDocument != null) {
            try {
                xfdfDocument.close();
            } catch (IOException e) {
                _log.warn(e);
            }
        }
    }
}


public static void setAppearanceValue(PDField field, String apValue)
        throws IOException {
    _log.debug("Inside setAppearanceValue method ");
    float fontSize = 9;
    float lowerLeftMargin = -4;
    PDAnnotationWidget widget = field.getWidget();

    PDAppearanceDictionary appearance = widget.getAppearance();
    if (appearance == null) {
        appearance = new PDAppearanceDictionary();
        widget.setAppearance(appearance);
    }

    Map normalAppearance = appearance.getNormalAppearance();
    PDAppearanceStream appearanceStream = (PDAppearanceStream) normalAppearance
            .get("default");

    if (appearanceStream != null) {
        appearanceStream.getBoundingBox().setLowerLeftY(-4);
        List tokens = getStreamTokens(appearanceStream);

        if (tokens != null) {

            ByteArrayOutputStream output = new ByteArrayOutputStream();
            ContentStreamWriter writer = new ContentStreamWriter(output);

            boolean foundString = false;
            for (int i = 0; i < tokens.size(); i++) {
                if (tokens.get(i) instanceof COSString) {
                    foundString = true;
                    COSString drawnString = ((COSString) tokens.get(i));
                    drawnString.reset();
                    drawnString.append(apValue.getBytes("ISO-8859-1"));
                }
            }

            int setFontIndex = tokens
                    .indexOf(PDFOperator.getOperator("Tf"));
            /*
             * COSName cosFontName = (COSName)tokens.get( setFontIndex-1 );
             * _log.debug("cosFontName is "+cosFontName);
             */
            tokens.set(setFontIndex - 1, new COSFloat(fontSize));

            if (foundString) {

                writer.writeTokens(tokens);
            }

            writeToStream(output.toByteArray(), appearanceStream);
            // added
            output.close();

        } else {

        }

    }

}

第一次发出打印命令时,我正在下面。

“此页面上存在错误.Acrobat可能无法正确显示页面。请联系创建pdf文档的人员以解决问题”。

打印输出非常好,第二次打印命令没有出错。

实际上在我的应用程序中,我有PDF模板,我们在其上填充数据。只有在填充数据而不是空模板时才会出现上述问题。我使用的是Adobe Reader XI(11.0.01)。如果您需要更具体的信息,请告诉我。任何帮助都将受到高度赞赏。谢谢。

0 个答案:

没有答案