繁体   English   中英

Java ITEXT pdf-未找到PDF作为文件或资源

[英]Java ITEXT pdf - PDF not found as file or resource

我看到了一些类似问题的示例,并尝试应用建议的修复程序,但似乎都无法正常工作。 我很确定路径是正确的,因为当我故意使路径不正确时,它会抛出一个不同的错误,提示“路径不正确...”。 我也在使用自由服务器。 我正在使用邮递员使用Java将一些JSON数据发布到后端,我正在尝试接收该数据,并尝试使用itext 5库将这些数据放入现有的pdf中。 当我像Java应用程序一样测试itext时,它也可以在当前路径下正常工作。

            public Response getOrg(PreApprovedAds ads) {

                Response response = null;
                logger.log(Level.FINE, "In Get PDF Method");
                boolean success = true;

                try {

        //          String src = "src/com/eb/anico/web/files/source.pdf";
        //          String dest = "src/com/eb/anico/web/files/Destination.pdf";

                    String src = "src/com/eb/anico/web/files/source.pdf";
                    String dest = "src/com/eb/anico/web/files/Destination.pdf";

                    totalRequestReceived++;

                    System.out.println("front end data: " + ads.getField());

                    System.out.println(ads.getField() + " " + ads.getValue());

                    PdfEditor editor = new PdfEditor();

                    String string = editor.manipulatePdf(src, dest, ads.getField());

                    // String string = editor.manipulatePdf("","",ads.getField());
                    //System.out.println(string);

                    logger.log(Level.FINE, "Out Get PDF Method");
                    response = Response.status(Response.Status.OK).entity(string).build();

                } catch (Exception e) {
                    logger.log(Level.INFO, "Error: " + e.getMessage());
                    logger.log(Level.FINE, "Error: " + CoreUtil.getStackTrace(e));
                    success = false;
                    response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                            .entity(PropertyUtil.getProperty("ERROR_MESSAGE")).build();
                } finally {
                    if (success) {
                        totalSucceedResponse++;
                    } else {
                        totalFailedResponse++;
                    }
                }

                return response;

            }

        public class PdfEditor {
            public String insertFields(String field, String value) {
                return field + " " + value;
                // System.out.println("does this work :" + field);
            }

        //  public static final String SRC = "src/resources/source.pdf";
        //  public static final String DEST = "src/resources/Destination.pdf";
        //
        //  public static void main(String[] args) throws DocumentException, IOException {
        //      File file = new File(DEST);
        //      file.getParentFile().mkdirs();
        //  }

            public String manipulatePdf(String src, String dest,String field) throws Exception {
                System.out.println("test");
                try {
                    PdfReader reader = new PdfReader(src);
                    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
                    AcroFields form = stamper.getAcroFields();
                    Item item = form.getFieldItem("Name");
                    PdfDictionary widget = item.getWidget(0);
                    PdfArray rect = widget.getAsArray(PdfName.RECT);
                    rect.set(2, new PdfNumber(rect.getAsNumber(2).floatValue() + 20f));
                    String value = field;
                    form.setField("Name", value);
                    form.setField("Company", value);
                    stamper.close();
                } catch (Exception e) {
                    System.out.println("Error in manipulate");
                    System.out.println(e.getMessage());
                    throw e;
                }
                return field;
            }

        }

在此处输入图片说明

实际上,这是一个简单的混乱,我不得不把c:\\的整个路径放到上面,问题就解决了。

String src = 
"C:/Users/JayAcer/workspace/test/src/com/eb/anico/web/files/source.pdf";

String dest = " 
C:/Users/JayAcer/workspace/test/src/com/eb/anico/web/files/Destination.pdf";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM