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

时间:2018-02-15 16:47:03

标签: java itext websphere-liberty

我看到了类似问题的一些示例,并尝试应用推荐的修补程序,似乎没有一个工作。我很确定路径是正确的,因为当我故意使路径不正确时,它会抛出一个不同的错误说"路径不正确......"。我也在使用自由服务器。我正在使用邮递员将一些JSON数据发布到后端,使用java我试图接收该数据并尝试使用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;
            }

        }

enter image description here

1 个答案:

答案 0 :(得分:0)

实际上,这是一个简单的混乱,我不得不把整个路径从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";