1

At the moment I'm trying to change the fonts used in a PDF document.

I therefore declared rectangles and extract the text using LocationTextExtractionStrategy:

System.util.RectangleJ rect = new System.util.RectangleJ(fX, fY, fWidth, fHeight);
RenderFilter[] rfArea = { new RegionTextRenderFilter(rect) };
ITextExtractionStrategy str = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), rfArea);
string s = PdfTextExtractor.GetTextFromPage(reader, 1, str);  

This works as expected, although it's quite complicated to find the right coordinates.

But how do I place the text in a new PDF document at the same starting location fX, fY so I don't break the layout?

I tried it using ColumnText but a multiline text is put one above the other:

Paragraph para = new Paragraph(fLineSpacing, s, font);
para.IndentationLeft = fLineIndentionLeft;
para.SpacingAfter = fSpacingAfter;
para.SpacingBefore = fSpacingBefore;
para.Alignment = iFontAlignment;

PdfContentByte cb = writer.DirectContent;
ColumnText ct = new ColumnText(cb);
ct.SetSimpleColumn(para, fX, fY + fHeight, fWidth + fX, fY, 0f, Element.ALIGN_LEFT);
ct.Go();

What am I missing? Or is there even something simpler? Using my approach I will have to cover the entire page defining lots of rectangles.

isHuman
  • 125
  • 9
  • Text on a page consists of a large amount of small snippets added at absolute positions. When you replace the font, how do you know the metrics of the new font will match the metrics of the old font. If they don't match (and that's very likely), you'll do an awful amount of work and end up with a really ugly document. Are you sure you want to replace the font. If so, *why* are you replacing the font. If we'd know more about the reason you want to replace the font, we may help you by explaining how to replace font resources (but that will only work if the encoding is predicable). – Bruno Lowagie May 17 '16 at 15:48
  • @BrunoLowagie Hi Bruno, yes, I need exactly that. Why? I got a source AFP document and transform it using a 3rd party tool. Unfortunately the original font gets replaced by something else - the best guess I have so far is, that the original font isn't installed on my pc and therefore gets replaced by something else. That's why I have to replace it - the resulting text is messed up - so the ugly document is already there and I need to fix it. – isHuman May 17 '16 at 16:01
  • @isHuman, see this [post here](http://stackoverflow.com/a/29602320/231316) which really does a great job of explaining the process. – Chris Haas May 17 '16 at 16:22
  • @ishuman my guess is: you have a PDF with a font that isn't embedded. Because it's not embedded, you need to find the original font program and replace the font dictionaries if the unembedded font with font dictionaries that contain the font program. If my guess is right, then your question is completely wrong (and so is your approach). – Bruno Lowagie May 17 '16 at 19:52
  • @BrunoLowagie how do I do that? I'm not the creator of the AFP document. – isHuman May 18 '16 at 06:13
  • @isHuman you shouldn't expect an answer to your question from someone who doesn't have access to your documents. – Bruno Lowagie May 18 '16 at 10:00
  • @BrunoLowagie how do I replace the font dictionaries? The only solution I'm looking for is replacing the font in a given rectangle. – isHuman May 18 '16 at 10:14
  • You keep changing your question. An example of replacing the font dictionaries can be found here: http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-16#607-embedfontpostfacto.java There is no guarantee it will work for you because you refuse to give more information about your actual requirement. – Bruno Lowagie May 18 '16 at 15:01
  • @BrunoLowagie I don't feel changing my question. I just want to replace some fonts of a pdf. If I adopt your example, then the "header" and "footer" of my pdf gets changed, but the majority of the text in between isn't detected/formatted at all. What can this be? As far as I read there is no table structure in pdf except if the document is tagged, which doesn't seem to be the case, because `PdfName.STRUCTTREEROOT` returns null. What can that be? The text is represented in boxes/rectangles or tables. – isHuman May 19 '16 at 07:46
  • This is my final comment on this question: you are trying to achieve something that can only be done by a specialist. There are specialists on StackOverflow, but you don't give the specialists sufficient information to do your job in your place. (Also: why should they do your job in your place?) Instead, you continue displaying your ignorance about PDF in a way that really annoys specialists. You may argue that you want to learn more about PDF, but the gap between what you currently know and what you need to know is too big. Teaching you PDF is outside the scope of StackOverflow. – Bruno Lowagie May 19 '16 at 07:52
  • @BrunoLowagie I don't want to be taught pdf, I want to use a library that is already there. Shouldn't the library help me to achieve my what I want in a easy way? Why do I need to know anything about the internal structure of the pdf-format? That should be done by the abstraction layer of the library. I don't want anyone to do my job, I just want to know how to change the text properly. If it is document specific, then I need to know which cases need to be considered (and that's what I'm asking). Would it be helpful to get information of my pdf using RUPS (I just found that tool)? – isHuman May 19 '16 at 08:14
  • There is no easy way to do what you want. You can't do what you want without knowing the internal structure of the PDF format: how are the fonts currently stored? which font program is needed? which encoding is used? The main problem with your question is that you have no idea what you're asking. That is extremely frustrating. – Bruno Lowagie May 19 '16 at 08:23
  • @BrunoLowagie Thanks for your time. It's as frustrating for me. I just want to change those damn fonts, without caring for the implementation details of itext(sharp) :-) And I ended up with trying this and that the last days... Isn't there any overview on how fonts can be stored (and how I can change them with itext(sharp) accordingly)? It would be nice of you if you can write two or three sentence about storing of fonts, font programs and encoding - I will accept this as answer - this may be helpful for someone later. – isHuman May 19 '16 at 08:59

0 Answers0