Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.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 PdfBox Android添加页面文本覆盖_Java_Android_Pdfbox - Fatal编程技术网

Java PdfBox Android添加页面文本覆盖

Java PdfBox Android添加页面文本覆盖,java,android,pdfbox,Java,Android,Pdfbox,坚持使用PDFBox Android,PDFBox Android:1.8.9.0 我加载pdf文件的第一页,在其中写入文本,并将此页导入最终文档的新页 问题是,当创建新页面时,它使用包含以前文本的最后一页… 所以,第一页是可以的,但是下一页有重叠的文本 private File writeReport() { File fileSource = new File(getActivity().getApplicationContext().getExternalCacheDir(), "

坚持使用PDFBox Android,PDFBox Android:1.8.9.0

我加载pdf文件的第一页,在其中写入文本,并将此页导入最终文档的新页


问题是,当创建新页面时,它使用包含以前文本的最后一页…
所以,第一页是可以的,但是下一页有重叠的文本

private File writeReport() {
    File fileSource = new File(getActivity().getApplicationContext().getExternalCacheDir(), "fileSource.pdf");

    // get file model in assets
    InputStream inputAsset = null;
    try {
        inputAsset = getActivity().getApplicationContext().getResources().getAssets().open("file_model.pdf");
    } catch (IOException e) {
        e.printStackTrace();
    }

    // copy file model in fileSource
    try {
        OutputStream outputStream = new FileOutputStream(file);
        byte buffer[] = new byte[1024];
        int length = 0;

        while ((length = inputAsset.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        outputStream.close();
        inputAsset.close();
    } catch (IOException e) {
        System.out.print("Copy assets : IOException" + e.getMessage());
    }

    File fileTarget = new File(getActivity().getApplicationContext().getCacheDir(), "fileTarget.pdf");


    try {
        PDFBoxResourceLoader.init(getActivity().getApplicationContext());       // init lib

        PDDocument documentSource = PDDocument.load(fileSource);
        PDDocument documentTarget = new PDDocument();

        // iteration == a new page
        for(int i=0 ; i < 3 ; i++)
        {
            PDPage page = documentSource.getDocumentCatalog().getPages().get(0);
            PDPageContentStream contentStream = new PDPageContentStream(documentSource, page, true, true);

            PDFont font1 = PDType1Font.HELVETICA;

            float startY = page.getMediaBox().getUpperRightY();
            float factor = 2.83f;

            page.getStream();

            // add text
            contentStream.beginText();
            contentStream.setFont(font1, 10);
            contentStream.newLineAtOffset(factor * 60, startY - (factor * 53));
            contentStream.showText("test text" + i);
            contentStream.endText();

            contentStream.close();

            // import source page to output file->  Problem here ! new page contain old overlay text... 
            documentTarget.importPage(page);
        }
        documentTarget.save(fileTarget);
        documentTarget.close();
        documentSource.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileTarget;
}
private File writeReport(){
File fileSource=新文件(getActivity().getApplicationContext().getExternalCacheDir(),“fileSource.pdf”);
//获取资产中的文件模型
InputStream inputAsset=null;
试一试{
inputAsset=getActivity().getApplicationContext().getResources().getAssets().open(“file_model.pdf”);
}捕获(IOE异常){
e、 printStackTrace();
}
//复制文件源中的文件模型
试一试{
OutputStream OutputStream=新文件OutputStream(文件);
字节缓冲区[]=新字节[1024];
整数长度=0;
而((长度=inputAsset.read(缓冲区))>0){
写入(缓冲区,0,长度);
}
outputStream.close();
inputAsset.close();
}捕获(IOE异常){
System.out.print(“复制资产:IOException”+e.getMessage());
}
File fileTarget=新文件(getActivity().getApplicationContext().getCacheDir(),“fileTarget.pdf”);
试一试{
PDFBoxResourceLoader.init(getActivity().getApplicationContext());//init lib
PDDocument documentSource=PDDocument.load(fileSource);
PDDocument documentTarget=新PDDocument();
//迭代==新页面
对于(int i=0;i<3;i++)
{
PDPage page=documentSource.getDocumentCatalog().getPages().get(0);
PDPageContentStream contentStream=新的PDPageContentStream(documentSource,page,true,true);
PDFont font1=PDType1Font.HELVETICA;
float startY=page.getMediaBox().getUpperRightY();
浮动系数=2.83f;
page.getStream();
//添加文本
contentStream.beginText();
setFont(font1,10);
contentStream.newlineatofset(因子*60,startY-(因子*53));
contentStream.showText(“测试文本”+i);
contentStream.endText();
contentStream.close();
//将源页面导入到输出文件->此处有问题!新页面包含旧的覆盖文本。。。
documentTarget.importPage(第页);
}
documentTarget.save(fileTarget);
documentTarget.close();
documentSource.close();
}捕获(IOE异常){
e、 printStackTrace();
}
返回fileTarget;
}
有没有办法在每次迭代中都有新的页面?
谢谢

问题在于您重用了相同的PDPage对象,这导致它包含上一次迭代的文本。解决方案:使用

documentTarget.importPage(page)
和那个一起工作。因为这是一个新的PDPage对象,所有内容都已克隆。因此,您的新代码如下所示:

    // iteration == a new page
    for(int i=0 ; i < 3 ; i++)
    {
        PDPage page = documentSource.getDocumentCatalog().getPages().get(0);
        page = documentTarget.importPage(page); // this is now a new PDPage object
        PDPageContentStream contentStream = new PDPageContentStream(documentSource, page, true, true);

        PDFont font1 = PDType1Font.HELVETICA;

        float startY = page.getMediaBox().getUpperRightY();
        float factor = 2.83f;

        page.getStream();

        // add text
        contentStream.beginText();
        contentStream.setFont(font1, 10);
        contentStream.newLineAtOffset(factor * 60, startY - (factor * 53));
        contentStream.showText("test text" + i);
        contentStream.endText();

        contentStream.close();            
    }
//迭代==新页面
对于(int i=0;i<3;i++)
{
PDPage page=documentSource.getDocumentCatalog().getPages().get(0);
page=documentTarget.importPage(page);//这现在是一个新的PDPage对象
PDPageContentStream contentStream=新的PDPageContentStream(documentSource,page,true,true);
PDFont font1=PDType1Font.HELVETICA;
float startY=page.getMediaBox().getUpperRightY();
浮动系数=2.83f;
page.getStream();
//添加文本
contentStream.beginText();
setFont(font1,10);
contentStream.newlineatofset(因子*60,startY-(因子*53));
contentStream.showText(“测试文本”+i);
contentStream.endText();
contentStream.close();
}

问题在于您重用了相同的PDPage对象,这导致它包含上一次迭代的文本。解决方案:使用

documentTarget.importPage(page)
和那个一起工作。因为这是一个新的PDPage对象,所有内容都已克隆。因此,您的新代码如下所示:

    // iteration == a new page
    for(int i=0 ; i < 3 ; i++)
    {
        PDPage page = documentSource.getDocumentCatalog().getPages().get(0);
        page = documentTarget.importPage(page); // this is now a new PDPage object
        PDPageContentStream contentStream = new PDPageContentStream(documentSource, page, true, true);

        PDFont font1 = PDType1Font.HELVETICA;

        float startY = page.getMediaBox().getUpperRightY();
        float factor = 2.83f;

        page.getStream();

        // add text
        contentStream.beginText();
        contentStream.setFont(font1, 10);
        contentStream.newLineAtOffset(factor * 60, startY - (factor * 53));
        contentStream.showText("test text" + i);
        contentStream.endText();

        contentStream.close();            
    }
//迭代==新页面
对于(int i=0;i<3;i++)
{
PDPage page=documentSource.getDocumentCatalog().getPages().get(0);
page=documentTarget.importPage(page);//这现在是一个新的PDPage对象
PDPageContentStream contentStream=新的PDPageContentStream(documentSource,page,true,true);
PDFont font1=PDType1Font.HELVETICA;
float startY=page.getMediaBox().getUpperRightY();
浮动系数=2.83f;
page.getStream();
//添加文本
contentStream.beginText();
setFont(font1,10);
contentStream.newlineatofset(因子*60,startY-(因子*53));
contentStream.showText(“测试文本”+i);
contentStream.endText();
contentStream.close();
}

“它使用包含先前文本的最后一页”源页面的最后一页还是目标页面的最后一页?“前文”是什么?上一次迭代的结果是什么?如果是这样的话,那么它就是你想要的,因为你正在修改相同的页面PDPage对象。更好的解决方案(请先测试)是首先导入页面,获取返回的PDPage(它现在是一个新对象),然后使用该对象作为参数添加额外的内容流。谢谢你的回复它使用包含上一次迭代的文本_的最后一个目标页面。我认为PDPage是在每次迭代中新创建的,因为循环中有声明…-我已经尝试了你的解决方案,但没有成功:结果相同…好吧,那么你应该尝试的是:将“documentTarget.importPage(page);”移到“getDocumentCatalog”行下面,并将其更改为“page=documentTarget.importPage(page);”然后再次测试你的应用程序。呵呵!!这不是我需要的最终结果,但没有更多的叠加!!我现在必须试着去理解!谢谢!“它使用包含上一页的最后一页