Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想使用java在PDF文档中添加一行_Java_Pdf_Pdfbox_Pdftotext - Fatal编程技术网

我想使用java在PDF文档中添加一行

我想使用java在PDF文档中添加一行,java,pdf,pdfbox,pdftotext,Java,Pdf,Pdfbox,Pdftotext,我目前正在使用PDFBox并从文件夹1中的.pdf中读取 我首先列出在文件夹中找到的所有Pdf文件。 然后我检查每个文件的页数。 现在,我想在页脚下方的文件末尾添加一个图像,打印机可以识别该图像来装订页面,因为它会意识到它已到达文件末尾 我已经到了,直到拿到文件清单和页数 我用什么命令转到最后一页的末尾并在那里写 我应该将.pdf文件转换为文本还是 我应该能够使用PDPageContentStream吗 这是我目前正在使用的代码,我正在尝试测试,看看AAA字符串是否会插入到我的pdf文件的最后一

我目前正在使用PDFBox并从文件夹1中的.pdf中读取

我首先列出在文件夹中找到的所有Pdf文件。 然后我检查每个文件的页数。 现在,我想在页脚下方的文件末尾添加一个图像,打印机可以识别该图像来装订页面,因为它会意识到它已到达文件末尾

我已经到了,直到拿到文件清单和页数

我用什么命令转到最后一页的末尾并在那里写

我应该将.pdf文件转换为文本还是 我应该能够使用PDPageContentStream吗

这是我目前正在使用的代码,我正在尝试测试,看看AAA字符串是否会插入到我的pdf文件的最后一页中。项目正在执行,没有错误,但由于某种原因,它没有插入到pdf中

package pdfviewer;

import java.io.*;
import java.util.*;
import java.util.List;
import java.io.IOException;

import org.apache.pdfbox.PDFReader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;





public class Main {

    /**
     * @param args the command line arguments
     */

         public static List flist()
       {
        List listfile = new ArrayList();
        String path = "C:/1";
        String files;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile())
            {
                files = listOfFiles[i].getName();
                if (files.endsWith(".pdf") || files.endsWith(".PDF"))
                {
                   listfile.add(listOfFiles[i]);


                }
            }
        }
        System.out.println(listfile);
        return listfile;

    }

public static void CheckPages(List a)
    {
        String dir = null;

        Object[] arraydir = a.toArray(new Object[0]);

        for (int i=0; i< arraydir.length; i++)
        {
            int pages = 0;
            PDFont font = PDType1Font.HELVETICA_BOLD;
            float fontSize = 12.0f;
            dir = arraydir[i].toString();
            System.out.println(dir);

            try {

                    PDDocument pdoc = PDDocument.load(dir);
                    List allPages = pdoc.getDocumentCatalog().getAllPages();

                    pages = pdoc.getNumberOfPages();
                    System.out.println(allPages);
                    int f = pages;
                    System.out.println(pages);

                    PDPage page = (PDPage) allPages.get(i);
                    //System.out.println(page);
                    PDRectangle pageSize = page.findMediaBox();
                    float stringWidth = font.getStringWidth( "AAA" );
                    float centeredPosition = (pageSize.getWidth() - (stringWidth*fontSize)/1000f)/2f;

                    PDPageContentStream contentStream = new PDPageContentStream(pdoc,page,true,true);
                    //System.out.println(contentStream);

                    contentStream.beginText();
                    contentStream.setFont( font, fontSize );
                    contentStream.moveTextPositionByAmount( centeredPosition, 30 );
                    contentStream.drawString( "AAA" );
                    contentStream.endText();
                    contentStream.close();


                    pdoc.close();

                  }
            catch (Exception e)
                    {
                        System.err.println("An exception occured in parsing the PDF Document."+ e.getMessage());
                    }
        }

}
    public static void main(String[] args)
    {
        List l = new ArrayList();
        l = pdfviewer.Main.flist();
        pdfviewer.Main.CheckPages(l);


    }

}
package-pdfviewer;
导入java.io.*;
导入java.util.*;
导入java.util.List;
导入java.io.IOException;
导入org.apache.pdfbox.PDFReader;
导入org.apache.pdfbox.pdmodel.PDDocument;
导入org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
导入org.apache.pdfbox.pdmodel.PDPage;
导入org.apache.pdfbox.pdmodel.common.PDRectangle;
导入org.apache.pdfbox.pdmodel.font.PDFont;
导入org.apache.pdfbox.pdmodel.font.PDType1Font;
公共班机{
/**
*@param指定命令行参数
*/
公共静态列表flist()
{
List listfile=new ArrayList();
字符串路径=“C:/1”;
字符串文件;
文件夹=新文件(路径);
File[]listOfFiles=folder.listFiles();
for(int i=0;i
谢谢你的关注


我上面使用的代码是正确的。 问题是生成的PDF文件是1.2版,这就是我不被允许编辑PDF文档的原因


有人知道如果我使用的是1.2版,我应该怎么做,因为我无法真正升级它。

您可以查看随库提供的示例。 您感兴趣的文件有两个:

1-AddImageToPDF.java

2-AddMessageToEachPage.java

第二个选项会在每个页面中添加一条消息,但您可以修改它,使其仅用于最后一个页面。根据PDFBox用户指南文档,应该在以下文件夹中找到它们:src/main/java/org/apache/PDFBox/examples 我在谷歌代码搜索上添加了链接,以防你在查找文件时遇到问题

我没有使用过这个库,也没有尝试过这些示例,我确信您需要稍微修改代码,以满足您对添加的行/图像位置的需要。 在任何情况下,如果这有助于您获得有效的解决方案,您可以添加该解决方案,以便其他人可以从中受益

编辑: 在看到问题作者发布的代码后,我添加了一个修改以使其正常工作。 为了清晰起见,我也允许自己做一些修改

import java.io.File;
import java.io.FileFilter;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static final FileFilter pdfFileFilter = new FileFilter() {

        public boolean accept(File file) {
            return file.isFile() && file.getName().toLowerCase().endsWith(".pdf");
        }
    };

    public static void closeQuietly(PDDocument doc) {
        if (doc != null) {
            try {
                doc.close();
            } catch (Exception exception) {
                //do something here if you wish like logging 
            }
        }
    }

    public static void CheckPages(File[] sourcePdfFiles,String textToInsert, String prefix) {

        for (File sourcePdfFile : sourcePdfFiles) {
            PDFont font = PDType1Font.HELVETICA_BOLD;
            float fontSize = 12.0f;
            PDDocument pdoc = null;
            try {

                pdoc = PDDocument.load(sourcePdfFile);
                List allPages = pdoc.getDocumentCatalog().getAllPages();
                PDPage lastPage = (PDPage) allPages.get(allPages.size() - 1);
                PDRectangle pageSize = lastPage.findMediaBox();
                float stringWidth = font.getStringWidth(textToInsert);
                float centeredPosition = (pageSize.getWidth() - (stringWidth * fontSize) / 1000f) / 2f;

                PDPageContentStream contentStream = new PDPageContentStream(pdoc, lastPage, true, true);

                contentStream.beginText();
                contentStream.setFont(font, fontSize);
                contentStream.moveTextPositionByAmount(centeredPosition, 30);
                contentStream.drawString(textToInsert);
                contentStream.endText();
                contentStream.close();

                File resultFile = new File(sourcePdfFile.getParentFile(), prefix + sourcePdfFile.getName());
                pdoc.save(resultFile.getAbsolutePath());


            } catch (Exception e) {
                System.err.println("An exception occured in parsing the PDF Document." + e.getMessage());
            } finally {
                closeQuietly(pdoc);
            }
        }
    }

    public static void main(String[] args) {
        File pdfFilesFolder = new File("C:\\1");
        File[] pdfFiles = pdfFilesFolder.listFiles(pdfFileFilter);
        //when a file is processed, the result will be saved in a new file having the location of the source file 
        //and the same name of source file prefixed with this
        String modifiedFilePrefix = "modified-";
        CheckPages(pdfFiles,"AAA", modifiedFilePrefix);
    }
}

您可以查看随库提供的示例。 您感兴趣的文件有两个:

1-AddImageToPDF.java

2-AddMessageToEachPage.java

第二个选项会在每个页面中添加一条消息,但您可以修改它,使其仅用于最后一个页面。根据PDFBox用户指南文档,应该在以下文件夹中找到它们:src/main/java/org/apache/PDFBox/examples 我在谷歌代码搜索上添加了链接,以防你在查找文件时遇到问题

我没有与图书馆合作过,也没有尝试过这些例子,我很确定你需要修改