ralfstuckert / pdfbox-layout

MIT License
155 stars 74 forks source link

Underline, Strikethrough not working in pdfboxlayout2 while using content stream #56

Open bijoybaby opened 6 years ago

bijoybaby commented 6 years ago

While using pdfbox pagecontentstream, the markups like underline and strikethrough is not working

try (PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true, true)) { Paragraph paragraph = new Paragraph(); String str = "You can alternate the position\n and thickness of an underline, \n so you may also use this \nto {0.25:}strike through \n\n" paragraph.drawText(contentStream, position, txtAlignment, null); contentStream.close(); }

arnthom commented 6 years ago

Try paragraph.addMarkup to add the string to the paragraph. Where do you add the text to the paragraph? How do you do this?

ralfstuckert commented 6 years ago

As arnthom said, first thing is to use addMarkup(). Alas, some decoration - as underline etc. - need extra handling when using the low level API. See LowLevelText.java for a complete example.

bijoybaby commented 6 years ago

Thanks @arnthom @ralfstuckert. What I did is, I override the drawn() method from AnnotationDrawListener to a custom class and create a DrawContext object for DrawListener. Now it is working...

Please see the code snippets /***/ paragraph.addMarkup(str, fontSize, rFont, bFont, iFont, biFont); Position position = new Position(bottomX, topY);

    DrawContext  context = new DrawContext() {
        @Override
        public PDDocument getPdDocument() {
            return document;
        }

        @Override
        public PDPageContentStream getCurrentPageContentStream() {
            return contentStream;
        }

        @Override
        public PDPage getCurrentPage() {
            return page;
        }
    };
    CustomAnnotationDrawListner listner = new CustomAnnotationDrawListner(context);
    paragraph.drawText(contentStream, position, txtAlignment, listner);
    listner.afterPage();
    contentStream.close();

/**/

/****CustomAnnotationDrawListner.java****/

public class CustomAnnotationDrawListner extends AnnotationDrawListener {

private static final Logger LOGGER = LoggerFactory.getLogger(CustomAnnotationDrawListner.class);

private final DrawContext drawContext;
private final Iterable<AnnotationProcessor> annotationProcessors;

 public CustomAnnotationDrawListner(final DrawContext drawContext) {
     super(drawContext);
        this.drawContext = drawContext;
        annotationProcessors = AnnotationProcessorFactory
            .createAnnotationProcessors();
        }

public void afterPage() throws IOException {
for (AnnotationProcessor annotationProcessor : annotationProcessors) {
    try {
    annotationProcessor.afterPage(drawContext);
    } catch (IOException e) {
        LOGGER.error("Exception in afterPage ", e);
    }
}
}

@Override
public void drawn(Object drawnObject, Position upperLeft, float width,
    float height) {
if (!(drawnObject instanceof Annotated)) {
    return;
}
for (AnnotationProcessor annotationProcessor : annotationProcessors) {
    try {
    annotationProcessor.annotatedObjectDrawn(
        (Annotated) drawnObject, drawContext, upperLeft, width,
        height);
    } catch (IOException e) {
        LOGGER.error("Exception in drawn ", e);
    }
}
}

}

/****/