그래픽 텍스트 효과가 있는 텍스트 요소 (Text element with graphical text effects)


문제 설명

그래픽 텍스트 효과가 있는 텍스트 요소 (Text element with graphical text effects)

예를 들어 그림자를 추가하거나 어떤 종류의 추가와 같은 다른 종류의 추가 그래픽 효과를 사용하여 Text 요소(com.itextpdf.layout.element.Text)를 만들고 싶습니다. 에 텍스처의 집합입니다. (DropShadow 일부 3D 효과) 이것을 달성하는 가장 좋은 방법은 무엇입니까?

지금까지 내가 가진 최고의 아이디어는 클리핑 텍스트를 사용하는 것입니다 렌더링 모드. (PDF 32000‑1 9.3.6; com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants.TextRenderingMode에서 정의됨). 텍스트를 클리핑 경계로 그리고 일종의 텍스처를 적용하거나 추가 그림자 "레이어"를 그립니다. 그러나 클리핑 경로는 com.itextpdf.layout.renderer.TextRender#drawcanvas.restoreState()를 사용하여 텍스트 그리기 이전 상태로 복원됩니다. >. 이것을 사용자 정의 TextRenderer로 확장하면 작동할 수 있지만 그리기 기능은 TextRenderer의 전용 기능에 대한 일부 호출이 있는 큰 기능입니다.

다른 가능한 방법은 무엇입니까?


참조 솔루션

방법 1:

I think in general customization of that level will require quite come code anyway. Completely overriding draw may indeed not work because some private implementation details are not exposed to the public. One option is of course to duplicate those implementation details into your custom renderer.

Another idea is to plug into the PdfCanvas which does low‑level drawing. You can create your own wrapper like the following one and delegate all operations to the PdfCanvas instance you wrap around except a couple of "interesting" operations where you will customize the logic and apply some styling:

private static class PdfCanvasWrapper extends PdfCanvas {
    private PdfCanvas delegate;
    public PdfCanvasWrapper(PdfCanvas wrapped) {
        super(wrapped.getContentStream(), wrapped.getResources(), wrapped.getDocument());
        this.delegate = wrapped;
    }

    // "Interesting" methods
    @Override
    public PdfCanvas endText() {
        delegate.endText();
        delegate.setFillColor(ColorConstants.BLACK);
        delegate.rectangle(10, 10, 300, 300);
        delegate.fill();
        return this;
    }

    // "Boring" methods ‑ just delegate the implementation to the wrapped instance
    @Override
    public PdfCanvas beginVariableText() {
        delegate.beginVariableText();
        return this;
    }

    @Override
    public PdfCanvas endVariableText() {
        delegate.endVariableText();
        return this;
    }

    // Override all other members like above
}

In this case your custom text renderer will only plug in the right DrawContext but use the default draw operation:

private static class CustomTextRenderer extends TextRenderer {
    public CustomTextRenderer(Text textElement) {
        super(textElement);
    }

    @Override
    public void draw(DrawContext drawContext) {
        DrawContext newContext = new DrawContext(drawContext.getDocument(), new PdfCanvasWrapper(drawContext.getCanvas()));
        super.draw(newContext);
    }

    @Override
    public CustomTextRenderer getNextRenderer() {
        return new CustomTextRenderer((Text) modelElement);
    }
}

Main could could look like this:

Paragraph p = new Paragraph();
Text text = new Text("Hello");
text.setTextRenderingMode(TextRenderingMode.CLIP);
text.setNextRenderer(new CustomTextRenderer(text));
p.add(text);

In general this approach is also hacky and of course depends on the implementation details as much as the initial approach you suggested. The approach you suggested is a more stable one but requires more code and probably more tuning when you update to the new version of the library. The approach I described above is more hacky but it results in less business logic copy‑pasting and maybe easier to maintain.

(by Tobias WohlfarthAlexey Subach)

참조 문서

  1. Text element with graphical text effects (CC BY‑SA 2.5/3.0/4.0)

#itext #itext7






관련 질문

iText로 가변 페이지 PDF를 생성하려고 할 때 예외 (Exception when attempting to generate variable-page PDF with iText)

Icepdf 특수 문자 렌더링 문제 (Icepdf special character rendering issue)

JAVA에서 PDF 양식 테이블 채우기 (Filling PDF form tables in JAVA)

itext를 사용하여 poi에서 차트 내보내기 (Exporting charts from poi using itext)

표 셀에 사각형을 그리는 방법은 무엇입니까? (How to draw rectangles in a Table cell?)

동적 컨트롤로 PDF 양식 템플릿 수정 (Modify PDF Form Template with dynamic controls)

itext와 같은 라이브러리로 Java 응용 프로그램을 배포하는 방법은 무엇입니까? (how to distribute a java-application with libraries such as itext?)

종이 Itext7에서 첫 번째 줄과 마지막 줄 속성을 제거합니다. (Remove the first and last lines properties in the paper Itext7)

Java에서 iText 5를 사용하여 여러 셀을 만들고 각 셀에 각 문자를 설정하는 방법 (How to create multiple cells and set each character into each cell using iText 5 in Java)

iTextSharp(또는 iText 5)를 사용하여 다른 PdfImportedPage의 배경으로 전체 PdfImportedPage를 추가하는 방법 (How to add a full PdfImportedPage as background for another PdfImportedPage with iTextSharp (or iText 5))

그래픽 텍스트 효과가 있는 텍스트 요소 (Text element with graphical text effects)

iText7을 사용하여 PDFButtonFormField에 이미지를 추가할 때 종횡비를 유지하는 방법 (How to Maintain Aspect Ratio when Adding an Image to a PDFButtonFormField using iText7)







코멘트