itext或itextsharp-在现有PDF中移动文本 [英] itext or itextsharp - move text in an existing PDF

查看:102
本文介绍了itext或itextsharp-在现有PDF中移动文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在特定矩形区域内的PDF中移动文本.在stackoverflow上有一个现有的Item,这已经使我接近实现这一目标: iText或iTextSharp基本文本编辑

但是,我将回到非常基本的水平上操作PDF.是否有机会使用itext的高级功能来更改文本的位置?恐怕没有简单的解决方案,但我很乐意得到一些建议.

PS:还请记住,我只想在矩形区域内移动文本,即,与原始PDF中的区域匹配的文本(块)应在其x和y坐标中进行一些移动.我对PDF的创建方式没有任何影响,我可以接受只能模糊地工作的解决方案.

解决方案

我以为我理解了您的问题,但是您对我的反问的回答令人困惑,所以让我举一个例子,在我解释它时回答您的问题

假设您有这样的文字:

我还有一个矩形的坐标:new Rectangle(100, 500, 200, 600);和一个偏移量:将矩形中的所有内容向左移动10个点,向底部移动2个点,如下所示:

这很容易实现.看看 CutAndPaste 示例:

public void manipulatePdf(String src, String dest)
    throws IOException, DocumentException {
    // Creating a reader
    PdfReader reader = new PdfReader(src);
    // step 1
    Rectangle pageSize = reader.getPageSize(1);
    Rectangle toMove = new Rectangle(100, 500, 200, 600);
    Document document = new Document(pageSize);
    // step 2
    PdfWriter writer
        = PdfWriter.getInstance(document, new FileOutputStream(dest));
    // step 3
    document.open();
    // step 4
    PdfImportedPage page = writer.getImportedPage(reader, 1);
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate template1 = cb.createTemplate(pageSize.getWidth(), pageSize.getHeight());
    template1.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
    template1.rectangle(toMove.getLeft(), toMove.getBottom(),
            toMove.getWidth(), toMove.getHeight());
    template1.eoClip();
    template1.newPath();
    template1.addTemplate(page, 0, 0);
    PdfTemplate template2 = cb.createTemplate(pageSize.getWidth(), pageSize.getHeight());
    template2.rectangle(toMove.getLeft(), toMove.getBottom(),
            toMove.getWidth(), toMove.getHeight());
    template2.clip();
    template2.newPath();
    template2.addTemplate(page, 0, 0);
    cb.addTemplate(template1, 0, 0);
    cb.addTemplate(template2, -20, -2);
    // step 4
    document.close();
    reader.close();
}

如果这不是您想要的.如果您想检测实际单词并移动这些单词,那么您会遇到问题.在那种情况下,我们正在谈论的一个项目可能很容易花费数月的时间才能正确完成,而您的简短问题在很大程度上无法知道在可以想象的许多极端情况下该怎么做. >

my goal is to move text in a PDF around, which is within a certain rectangular area. There is an existing Item on stackoverflow, which already got me close to achiving this: iText or iTextSharp rudimentary text edit

However, I would be back at manipulating the PDF on a very basic level. Is there a chance to use higher level facilities of itext to alter the position of the text? I'm afraid there is no simple solution, but I'd be happy to get some suggestions.

PS: Also keep in mind that I want to move around only text within a rectangular area, i.e. text(chunks) matching the area in the original PDF should be shifted some in their x and y coordinates. I have no influence on how the PDF are created and I can accept a solution that only vaguely works.

解决方案

I thought I understood your question, but your response to my counter-question is confusing, so let me give you an example that answers your question as I interpret it.

Suppose that you have a text like this:

I also have the coordinates of a rectangle: new Rectangle(100, 500, 200, 600); and an offset: move everything in that rectangle 10 points to the left and 2 points to the bottom, like this:

That is fairly easy to achieve. Take a look at the CutAndPaste example:

public void manipulatePdf(String src, String dest)
    throws IOException, DocumentException {
    // Creating a reader
    PdfReader reader = new PdfReader(src);
    // step 1
    Rectangle pageSize = reader.getPageSize(1);
    Rectangle toMove = new Rectangle(100, 500, 200, 600);
    Document document = new Document(pageSize);
    // step 2
    PdfWriter writer
        = PdfWriter.getInstance(document, new FileOutputStream(dest));
    // step 3
    document.open();
    // step 4
    PdfImportedPage page = writer.getImportedPage(reader, 1);
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate template1 = cb.createTemplate(pageSize.getWidth(), pageSize.getHeight());
    template1.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
    template1.rectangle(toMove.getLeft(), toMove.getBottom(),
            toMove.getWidth(), toMove.getHeight());
    template1.eoClip();
    template1.newPath();
    template1.addTemplate(page, 0, 0);
    PdfTemplate template2 = cb.createTemplate(pageSize.getWidth(), pageSize.getHeight());
    template2.rectangle(toMove.getLeft(), toMove.getBottom(),
            toMove.getWidth(), toMove.getHeight());
    template2.clip();
    template2.newPath();
    template2.addTemplate(page, 0, 0);
    cb.addTemplate(template1, 0, 0);
    cb.addTemplate(template2, -20, -2);
    // step 4
    document.close();
    reader.close();
}

If this is not what you want. If you want to detect actual words and move those words, then you have a problem. In that case, we are talking about a project that could easily cost a couple of months of work to do it correctly, and your short question would be largely insufficient to know what to do in the many edge cases that can be imagined.

这篇关于itext或itextsharp-在现有PDF中移动文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆