pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1769162 - /pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreatePatternsPDF.java
Date Thu, 10 Nov 2016 17:14:08 GMT
Author: tilman
Date: Thu Nov 10 17:14:07 2016
New Revision: 1769162

URL: http://svn.apache.org/viewvc?rev=1769162&view=rev
Log:
PDFBOX-3563: Create sample code for creating a PDF with patterns

Added:
    pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreatePatternsPDF.java
  (with props)

Added: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreatePatternsPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreatePatternsPDF.java?rev=1769162&view=auto
==============================================================================
--- pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreatePatternsPDF.java
(added)
+++ pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreatePatternsPDF.java
Thu Nov 10 17:14:07 2016
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2016 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.examples.pdmodel;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
+import org.apache.pdfbox.pdmodel.graphics.color.PDPattern;
+import org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern;
+import org.apache.pdfbox.util.Charsets;
+
+/**
+ * This is an example of how to create a page that uses patterns to paint areas.
+ *
+ * @author Tilman Hausherr
+ */
+public class CreatePatternsPDF
+{
+    public static void main(String[] args) throws IOException
+    {
+        PDDocument doc = new PDDocument();
+        PDPage page = new PDPage();
+        doc.addPage(page);
+        page.setResources(new PDResources());
+
+        PDPageContentStream pcs = new PDPageContentStream(doc, page);
+
+        // Colored pattern, i.e. the pattern content stream will set its own color(s)
+        PDColorSpace patternCS1 = new PDPattern(null, PDDeviceRGB.INSTANCE);
+
+        // Table 75 spec
+        PDTilingPattern tilingPattern1 = new PDTilingPattern();
+        tilingPattern1.setBBox(new PDRectangle(0, 0, 10, 10));
+        tilingPattern1.setPaintType(PDTilingPattern.PAINT_COLORED);
+        tilingPattern1.setTilingType(PDTilingPattern.TILING_CONSTANT_SPACING);
+        tilingPattern1.setXStep(10);
+        tilingPattern1.setYStep(10);
+
+        COSName patternName1 = page.getResources().add(tilingPattern1);
+        //TODO 
+        // there's no way to create something like a PDPageContentStream, 
+        // so we'll do it the hard way
+        OutputStream os1 = tilingPattern1.getContentStream().createOutputStream();
+        // Set color, draw diagonal line + 2 more diagonals so that corners look good
+        os1.write("1 0 0 RG 0 0 m 10 10 l -1 9 m 1 11 l 9 -1 m 11 1 l s".getBytes(Charsets.US_ASCII));
+        os1.close();
+
+        PDColor patternColor1 = new PDColor(patternName1, patternCS1);
+
+        pcs.addRect(50, 500, 200, 200);        
+        pcs.setNonStrokingColor(patternColor1);
+        pcs.fill();
+
+        // Uncolored pattern - the color is passed later
+        PDTilingPattern tilingPattern2 = new PDTilingPattern();
+        tilingPattern2.setBBox(new PDRectangle(0, 0, 10, 10));
+        tilingPattern2.setPaintType(PDTilingPattern.PAINT_UNCOLORED);
+        tilingPattern2.setTilingType(PDTilingPattern.TILING_NO_DISTORTION);
+        tilingPattern2.setXStep(10);
+        tilingPattern2.setYStep(10);
+
+        COSName patternName2 = page.getResources().add(tilingPattern2);
+        OutputStream os2 = tilingPattern2.getContentStream().createOutputStream();
+        // draw a cross
+        os2.write("0 5 m 10 5 l 5 0 m 5 10 l s".getBytes(Charsets.US_ASCII));
+        os2.close();
+
+        // Uncolored pattern colorspace needs to know the colorspace
+        // for the color values that will be passed when painting the fill
+        PDColorSpace patternCS2 = new PDPattern(null, PDDeviceRGB.INSTANCE);
+        PDColor patternColor2green = new PDColor(
+                new float[]{0,1,0},
+                patternName2,
+                patternCS2);
+
+        pcs.addRect(300, 500, 100, 100);        
+        pcs.setNonStrokingColor(patternColor2green);
+        pcs.fill();
+
+        // same pattern again but with different color + different pattern start position
+        PDColor patternColor2blue = new PDColor(
+                new float[]{0,0,1},
+                patternName2,
+                patternCS2);
+        pcs.addRect(455, 505, 100, 100);        
+        pcs.setNonStrokingColor(patternColor2blue);
+        pcs.fill();
+
+        pcs.close();
+        doc.save("patterns.pdf");
+        doc.close();
+    }
+}

Propchange: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreatePatternsPDF.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message