pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tretonio Tretis <treto...@gmail.com>
Subject Error Reversing Page
Date Mon, 14 Aug 2017 14:03:20 GMT
Error in my program, this I do not know if it is from FXGraphics2D or
pdfbox or something wrong in my program.

After you click "Next" the page will be inverted and then clicking "Next"
again will return to normal.

I'm using the library
Http://www.jfree.org/fxgraphics2d/

Please, put valid pdf file name, to test.
 -->> document = PDDocument.load( new File("file_example.pdf") );

Thank's

Program :



import com.sun.javafx.iio.ImageStorage;
import java.awt.Composite;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ImageObserver;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.text.AttributedCharacterIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.embed.swing.SwingNode;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import static javafx.scene.layout.Region.USE_COMPUTED_SIZE;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import org.apache.pdfbox.contentstream.PDFStreamEngine;
import org.apache.pdfbox.contentstream.operator.DrawObject;
import org.apache.pdfbox.contentstream.operator.Operator;
import org.apache.pdfbox.contentstream.operator.color.SetNonStrokingColor;
import org.apache.pdfbox.contentstream.operator.color.SetNonStrokingColorN;
import
org.apache.pdfbox.contentstream.operator.color.SetNonStrokingColorSpace;
import
org.apache.pdfbox.contentstream.operator.color.SetNonStrokingDeviceCMYKColor;
import
org.apache.pdfbox.contentstream.operator.color.SetNonStrokingDeviceGrayColor;
import
org.apache.pdfbox.contentstream.operator.color.SetNonStrokingDeviceRGBColor;
import org.apache.pdfbox.contentstream.operator.color.SetStrokingColor;
import org.apache.pdfbox.contentstream.operator.color.SetStrokingColorN;
import org.apache.pdfbox.contentstream.operator.color.SetStrokingColorSpace;
import
org.apache.pdfbox.contentstream.operator.color.SetStrokingDeviceCMYKColor;
import
org.apache.pdfbox.contentstream.operator.color.SetStrokingDeviceGrayColor;
import
org.apache.pdfbox.contentstream.operator.color.SetStrokingDeviceRGBColor;
import org.apache.pdfbox.contentstream.operator.state.Concatenate;
import org.apache.pdfbox.contentstream.operator.state.Restore;
import org.apache.pdfbox.contentstream.operator.state.Save;
import
org.apache.pdfbox.contentstream.operator.state.SetGraphicsStateParameters;
import org.apache.pdfbox.contentstream.operator.state.SetMatrix;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState;
import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;
import org.apache.pdfbox.util.Matrix;
import org.jfree.fx.FXGraphics2D;



public class PageError extends Application {

    int pagina = 1;

    Pane pane = new Pane();
    StackPane st = new StackPane();
    ScrollPane sp = new ScrollPane();

    PDDocument document = null;

    PDFRenderer renderer=null;
    PDPage page=null;

    ImageView imv = new ImageView();

    Canvas canvas = new Canvas();
    GraphicsContext gc=null;

    FXGraphics2D g2dfx = null;

    @Override
    public void start(Stage primaryStage) {

        Rectangle rec = new Rectangle();

        Button button  = new Button("Next");
        button.setOnAction((e) -> {

            if( g2dfx != null ) {
               g2dfx.clearRect(0, 0, (int)pane.getPrefWidth(),
(int)pane.getPrefHeight() );
            }

            page = document.getDocumentCatalog().getPages().get(pagina);

            pagina++;
            process();
        });

        BorderPane bp = new BorderPane();

        canvas.setWidth(500);
        canvas.setHeight(700);
        gc=canvas.getGraphicsContext2D();

        g2dfx = new FXGraphics2D(gc);

        pane.getChildren().add(canvas);
        pane.setStyle("-fx-background-color:white;-fx-effect:
dropshadow(gaussian, rgba(0, 0, 0, 0.4), 10, 0.5, 0.0, 0.0);");

        pane.setPrefWidth(500);
        pane.setPrefHeight(700);
        pane.setMaxWidth(500);
        pane.setMaxHeight(700);


        st.getChildren().add(pane);

        st.setPrefWidth(510);
        st.setPrefHeight(710);
        st.setAlignment(Pos.CENTER);
        StackPane.setAlignment(pane, Pos.CENTER);
        sp.setContent(st);

        HBox g = new HBox(10);
        g.getChildren().addAll(button);

        bp.setCenter(sp);
        bp.setBottom(g);

        Scene scene = new Scene(bp, 500, 700);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

        List<String> list = javafx.scene.text.Font.getFamilies();
        for(String s : list) {
           System.out.println(s);
        }

        try {

          document = PDDocument.load( new File("file_example.pdf") );

          process();
          pagina++;


        }catch(Exception ee) {
            ee.printStackTrace();
        }


    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    public void process() {
        try {

            float w = document.getPage(pagina).getBBox().getWidth();
            float h = document.getPage(pagina).getBBox().getHeight();

            st.setPrefWidth(w+20);
            st.setPrefHeight(h+20);

            pane.setPrefWidth(w);
            pane.setPrefHeight(h);
            pane.setMaxWidth(w);
            pane.setMaxHeight(h);

            canvas.setWidth(w);
            canvas.setHeight(h);

            renderer = new PDFRenderer(document);

            g2dfx = new FXGraphics2D(gc);
            g2dfx.setBackground(java.awt.Color.white);

            renderer.renderPageToGraphics(pagina, g2dfx, 1);

        }catch(Exception e) {
            e.printStackTrace();
        }
    }



}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message