You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2017/11/10 17:57:58 UTC

svn commit: r1814892 - in /pdfbox/trunk: examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowTextWithPositioning.java pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java

Author: tilman
Date: Fri Nov 10 17:57:58 2017
New Revision: 1814892

URL: http://svn.apache.org/viewvc?rev=1814892&view=rev
Log:
PDFBOX-3992: show in example why calling setWordSpacing() is not always the solution; improve javadoc

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowTextWithPositioning.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowTextWithPositioning.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowTextWithPositioning.java?rev=1814892&r1=1814891&r2=1814892&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowTextWithPositioning.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowTextWithPositioning.java Fri Nov 10 17:57:58 2017
@@ -26,7 +26,9 @@ import org.apache.pdfbox.pdmodel.PDPageC
 import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
 import org.apache.pdfbox.pdmodel.font.PDType0Font;
+import org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding;
 import org.apache.pdfbox.util.Matrix;
 
 /**
@@ -122,7 +124,40 @@ public class ShowTextWithPositioning
                     text.add(String.valueOf(Character.toChars(message.codePointAt(i))));
                 }
                 contentStream.showTextWithPositioning(text.toArray());
-                
+
+                // PDF specification about word spacing:
+                // "Word spacing shall be applied to every occurrence of the single-byte character 
+                // code 32 in a string when using a simple font or a composite font that defines 
+                // code 32 as a single-byte code. It shall not apply to occurrences of the byte 
+                // value 32 in multiple-byte codes.
+                // TrueType font with no word spacing
+                contentStream.setTextMatrix(
+                        Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f * 4));
+                font = PDTrueTypeFont.load(doc, PDDocument.class.getResourceAsStream(
+                        "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"), WinAnsiEncoding.INSTANCE);
+                contentStream.setFont(font, FONT_SIZE);
+                contentStream.showText(message);
+
+                float wordSpacing = (pageSize.getWidth() * 1000f - stringWidth) / (parts.length - 1) / 1000;
+
+                // TrueType font with word spacing
+                contentStream.setTextMatrix(
+                        Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f * 5));
+                font = PDTrueTypeFont.load(doc, PDDocument.class.getResourceAsStream(
+                        "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"), WinAnsiEncoding.INSTANCE);
+                contentStream.setFont(font, FONT_SIZE);
+                contentStream.setWordSpacing(wordSpacing);
+                contentStream.showText(message);
+
+                // Type0 font with word spacing that has no effect
+                contentStream.setTextMatrix(
+                        Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f * 6));
+                font = PDType0Font.load(doc, PDDocument.class.getResourceAsStream(
+                        "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));
+                contentStream.setFont(font, FONT_SIZE);
+                contentStream.setWordSpacing(wordSpacing);
+                contentStream.showText(message);
+
                 // Finish up.
                 contentStream.endText();
             }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java?rev=1814892&r1=1814891&r2=1814892&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java Fri Nov 10 17:57:58 2017
@@ -1584,6 +1584,12 @@ public abstract class PDAbstractContentS
     /**
      * Set the word spacing. The value shall be added to the horizontal or vertical component of the
      * ASCII SPACE character, depending on the writing mode.
+     * <p>
+     * This will have an effect only with Type1 and TrueType fonts, not with Type0 fonts. The PDF
+     * specification tells why: "Word spacing shall be applied to every occurrence of the
+     * single-byte character code 32 in a string when using a simple font or a composite font that
+     * defines code 32 as a single-byte code. It shall not apply to occurrences of the byte value 32
+     * in multiple-byte codes."
      *
      * @param spacing word spacing
      * @throws IOException If the content stream could not be written.