繁体   English   中英

用Java编辑现有的pdf

[英]Editing existing pdf in Java

我正在测试Java库以编辑现有的pdf,但是问题是无法加载我现有的pdf。 我使用iText和pdfbox得到了相同的结果,我可以在此处加载数据的文件(pdf称重ko),但是创建的pdf为空(不显示任何内容)。

我正在一个App Engine服务器上执行此操作,有两个库,我可以创建pdf并将其显示在具有servlet或webservice的浏览器中。

我完全迷失了,尝试大量代码,但结果始终相同!

带importedPage的iText:

    Document document = new Document();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter docWriter = PdfWriter.getInstance(document, baos);
    document.open();

    // Load existing PDF
    PdfReader reader = new PdfReader("WEB-INF/pdf.pdf");
    document.newPage();
    PdfImportedPage page = docWriter.getImportedPage(reader, 1);
    PdfPTable table = new PdfPTable(2);
    table.addCell(Image.getInstance(page));
    document.add(table);

    document.close();
    docWriter.close();

pdfbox:

     PDDocument document = new PDDocument();
     PDDocument sourceDocument = PDDocument.load("WEB-INF/pdf.pdf");
     PDPage templatePdfPage = (PDPage)sourceDocument.getDocumentCatalog().getAllPages().get(0);
     document.addPage(templatePdfPage);
     document.save(output);

首先获取路径使用ServletContext Servlet并使用PDFBOx读取pdf文件并将pdf文件保存在/WEB-INF/savedpdffiles/文件夹中。

注意:在WEB-INF文件夹下创建文件夹savedpdffiles

请参阅JRE类白名单-Java App Engine应用程序对Java标准库(Java运行时环境,或JRE)中的类的访问仅限于以下类:

在Google AppEngine中阅读并保存PDF文件。

码:

    PrintWriter printWriter = response.getWriter();
    try {
        ServletContext context = request.getSession().getServletContext();
        String pdffiles = context.getRealPath("/WEB-INF/");

        File readPath = new File(pdffiles);
        if (readPath.exists()) {
            String pdfFile = "04-Request-Headers.pdf"; // read this file to save in savedpdffiles folder
            File savedPath = new File(readPath.getAbsolutePath() +"/savedpdffiles/"); // create savedpdffiles folder under WEB-INF folder

            File readFullPath = new File(readPath.getAbsolutePath() + File.separatorChar + pdfFile);
            if (readFullPath.isFile()) {
                if(!savedPath.exists()) {
                    savedPath.createNewFile();// create new pdf file if not exits
                    printWriter.println( savedPath.getName() +" File created in -> "+ savedPath.getAbsolutePath());
                }

                PDDocument document = new PDDocument();
                PDDocument sourceDocument = PDDocument.load(readFullPath.getAbsolutePath()); // read the pdf file by PDDocument
                PDPage templatePdfPage = (PDPage) sourceDocument.getDocumentCatalog().getAllPages().get(0); // only first page is read out of 13 pages and save the first page.
                document.addPage(templatePdfPage);
                document.save(savedPath + "/" + pdfFile);
                document.close();
                sourceDocument.close();
                printWriter.print(pdfFile + " File saved to this location-> "+ savedPath.getAbsolutePath() + File.separatorChar + pdfFile);
            } else {
                printWriter.println(readFullPath.getName() + " File not exits in -> "+ readFullPath.getAbsolutePath());
            }
        } else {
            printWriter.println("Path not exists -> "+ readPath.getAbsolutePath());
        }
    } catch (Exception e) {
        printWriter.print("Type of Error occured while saving the PDF file -> "+ e.getMessage());
        e.printStackTrace();
    }

您将得到以下错误

Caused by:
          java.lang.NoClassDefFoundError: java.awt.Color is a restricted class. Please see the Google  App Engine developer's guide for more details.
    Powered by Jetty://

我发现了问题,我正在使用Maven构建我的应用程序。 Maven通过编码文件(我认为是UTF-8)来破坏我的pdf。 我发现这是因为我得到了.p12文件,而当我读什么书时,我说这是损坏的文件。

现在的问题是尝试避免这种情况,但不起作用。 (目前我在构建后替换文件)

我尝试将其添加到我的pom.xml中:

<resources>
        <resource>
            <directory>src/main/webapp/WEB-INF</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/*.pdf</exclude>
            </excludes>
        </resource>
    </resources>

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <nonFilteredFileExtensions>
                    <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
                </nonFilteredFileExtensions>
            </configuration>
        </plugin>

在我的pom.xml中有些模棱两可的东西,我不是不是这样做的:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <archiveClasses>true</archiveClasses>
                <webResources>
                    in order to interpolate version from pom into appengine-web.xml
                    <resource>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF</targetPath>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

暂无
暂无

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

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