-1

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?

marcus
  • 1
  • 3
  • Do you want a section of a page (which contains Type 3 font glyphs) from document A copied as is to document B or do you expect some re-flowing of the content to happen? – mkl Apr 24 '14 at 12:34
  • I want re-flowing of content written in Type 3 font. I tried RenderListener.renderText, but renderInfo.getFont(), getBaseline() get weird values. I don't know next step. – marcus Apr 24 '14 at 13:57
  • @mkl I updated question, can you guide me? – marcus Apr 25 '14 at 10:11
  • *I want re-flowing of content* - that may turn out difficult. Can you provide a representative sample document with such Type 3 font text and point out how the glyphs to copy are selected? – mkl Apr 25 '14 at 10:17
  • @mkl I want to make https://dl.dropboxusercontent.com/u/5872993/from.pdf to https://dl.dropboxusercontent.com/u/5872993/to.pdf – marcus Apr 25 '14 at 12:09
  • I'll look into it later. – mkl Apr 25 '14 at 12:22
  • Thanks a lot!!! to.pdf is best answer. I just want to know how to move one character from from.pdf to to.pdf at same position and same size. – marcus Apr 25 '14 at 12:40
  • @mkl Can you give me a rough direction? – marcus Apr 29 '14 at 11:54
  • I didn't find the time this weekend but its still on my agenda. – mkl Apr 29 '14 at 12:03
  • I just found time to view your from.pdf and to.pdf. As I see you do *not* seem to want to reflow the copied content after all but merely copy some rectangle. To do so you can simply copy the page content (e.g. as in [this answer](http://stackoverflow.com/questions/15711729/extract-area-from-pdf/15735744#15735744) which also shows how to scale and move the content) after restricting the area drawn in using a clip path. – mkl May 06 '14 at 07:51

1 Answers1

0

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.

mkl
  • 90,588
  • 15
  • 125
  • 265