From e2a06f72c7e5c98b3127da835a57c8fea0931b7d Mon Sep 17 00:00:00 2001 From: Andrew Higgins Date: Wed, 27 May 2020 15:34:34 +1200 Subject: [PATCH 1/2] Added support for Apache PDFBox TilingPaint. --- .../PdfBoxGraphics2DPaintApplier.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2DPaintApplier.java b/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2DPaintApplier.java index eabc3fd..5b411dd 100644 --- a/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2DPaintApplier.java +++ b/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2DPaintApplier.java @@ -27,6 +27,7 @@ import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.IOException; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.List; import java.util.*; @@ -149,6 +150,9 @@ else if (simpleName.equals("PatternPaint")) { applyPatternPaint(paint, state); } + else if(simpleName.equals("TilingPaint")) { + applyTilingPaint(paint, state); + } else if (paint instanceof GradientPaint) { return shadingCache.makeUnqiue(buildGradientShading((GradientPaint) paint, state)); @@ -248,6 +252,15 @@ private void applyPatternPaint(Paint paint, PaintApplierState state) throws IOEx state.contentStream.setStrokingColor(patternColor); } + /* + * Apache PDFBox Tiling Paint + */ + private void applyTilingPaint(Paint paint, PaintApplierState state) throws IOException + { + TexturePaint texturePaint = getPrivateFieldValue(paint, "paint"); + applyTexturePaint(texturePaint, state); + } + private void applyComposite(PaintApplierState state) { /* @@ -828,6 +841,37 @@ protected static T getPropertyValue(Object obj, String propertyGetter) } } + /** + * Get the value of a private field from the specified object. + * + * @param obj The object containing the field. + * @param fieldName The name of the field. + * @param The field type. + * @return The private field value. + */ + @SuppressWarnings({ "unchecked", "WeakerAccess" }) + protected static T getPrivateFieldValue(Object obj, String fieldName) + { + try + { + Class c = obj.getClass(); + try + { + Field f = c.getDeclaredField(fieldName); + f.setAccessible(true); + return (T)f.get(obj); + } + catch (NoSuchFieldException ignored) + { + } + throw new NullPointerException("Field " + fieldName + " not found!"); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } + private static abstract class COSResourceCacheBase { private Map> states = new HashMap>(); From 2e97fe5229cc1894231fe1812eecb618aa6fb5f9 Mon Sep 17 00:00:00 2001 From: Andrew Higgins Date: Fri, 29 May 2020 18:47:20 +1200 Subject: [PATCH 2/2] Improvements to support for Apache PDFBox TilingPaint. --- .../PdfBoxGraphics2DPaintApplier.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2DPaintApplier.java b/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2DPaintApplier.java index 5b411dd..094ff96 100644 --- a/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2DPaintApplier.java +++ b/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2DPaintApplier.java @@ -255,10 +255,18 @@ private void applyPatternPaint(Paint paint, PaintApplierState state) throws IOEx /* * Apache PDFBox Tiling Paint */ - private void applyTilingPaint(Paint paint, PaintApplierState state) throws IOException + private void applyTilingPaint(Paint paint, PaintApplierState state) { - TexturePaint texturePaint = getPrivateFieldValue(paint, "paint"); - applyTexturePaint(texturePaint, state); + try + { + Paint tilingPaint = getPrivateFieldValue(paint, "paint"); + applyPaint(tilingPaint, state); + } + catch (Exception e) + { + System.err.println("PdfBoxGraphics2DPaintApplier error while drawing Tiling Paint"); + e.printStackTrace(); + } } private void applyComposite(PaintApplierState state) @@ -855,14 +863,18 @@ protected static T getPrivateFieldValue(Object obj, String fieldName) try { Class c = obj.getClass(); - try - { - Field f = c.getDeclaredField(fieldName); - f.setAccessible(true); - return (T)f.get(obj); - } - catch (NoSuchFieldException ignored) + while (c != null) { + try + { + Field f = c.getDeclaredField(fieldName); + f.setAccessible(true); + return (T) f.get(obj); + } + catch (NoSuchFieldException ignored) + { + } + c = c.getSuperclass(); } throw new NullPointerException("Field " + fieldName + " not found!"); }