Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

itext - How to move text written in Type 3 Font from one pdf to other pdf?

I have a pdf which include text written in Type 3 Font.

I want to get some text from it and write it into other pdf in exactly same shape.

I am using itext. Please give me a tip.

edit: I attached my code.

DocumentFont f = renderInfo.getFont(); 
String str = renderInfo.getText();
x = renderInfo.getBaseline().getStartPoint().get(Vector.I1);

In this code, I want to write str into x value position. In Type 3 Font, is it work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can copy parts of one page to a new one using code like this:

    InputStream resourceStream = getClass().getResourceAsStream("from.pdf");
    PdfReader reader = new PdfReader(new FileOutputStream("from.pdf"));
    Rectangle pagesize = reader.getPageSizeWithRotation(1);
    Document document = new Document(pagesize);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("areaOfFrom.pdf"));
    document.open();

    PdfContentByte content = writer.getDirectContent();
    PdfImportedPage page = writer.getImportedPage(reader, 1);

    content.saveState();
    content.rectangle(0, 350, 360, 475);
    content.clip();
    content.newPath();
    content.addTemplate(page, 0, 0);
    content.restoreState();

    document.close();
    reader.close();

This turns your

from.pdf

into

AreaOfFrom.pdf

Unfortunately, though, that hidden content is merely... hidden... but it is still there. You can especially mark the lines with that hidden text and try to copy&paste them.

If you want to completely remove that hidden text (or start out by merely copying the desired text), you have to inspect the content of the imported page and filter it. I'm afraid iText does not yet explicitly support something like that. It can be done using the iText lowlevel API but it is quite some work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...