org.fit.pdfdom.PathDrawer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pdf2dom Show documentation
Show all versions of pdf2dom Show documentation
Pdf2Dom is a PDF parser that converts the documents to a HTML DOM representation. The obtained DOM tree may be then serialized to a HTML file or further processed. The inline CSS definitions contained in the resulting document are used for making the HTML page as similar as possible to the PDF input. A command-line utility for converting the PDF documents to HTML is included in the distribution package. Pdf2Dom may be also used as an independent Java library with a standard DOM interface for your DOM-based applications or as an alternative parser for the CSSBox rendering engine in order to add the PDF processing capability to CSSBox.
/*
* Copyright (c) Matthew Abboud 2016
*
* Pdf2Dom is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pdf2Dom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CSSBox. If not, see .
*/
package org.fit.pdfdom;
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
import org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState;
import org.fit.pdfdom.resource.ImageResource;
import org.slf4j.Logger;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import static org.slf4j.LoggerFactory.getLogger;
public class PathDrawer
{
private static final Logger log = getLogger(PathDrawer.class);
private final PDGraphicsState state;
public PathDrawer(PDGraphicsState state)
{
this.state = state;
}
public ImageResource drawPath(List path) throws IOException
{
if (path.size() == 0)
return new ImageResource("PathImage", new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB));
Rectangle2D.Double bounds = getPathBounds(path);
if (bounds.getHeight() <= 0 || bounds.getWidth() <= 0)
{
bounds.width = bounds.height = 1;
log.info("Filled curved paths are not yet supported by Pdf2Dom.");
}
BufferedImage image = new BufferedImage((int) bounds.getWidth(), (int) bounds.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D gfx = image.createGraphics();
clearPathGraphics(bounds, gfx);
drawPathSegments(path, gfx);
gfx.dispose();
// keep track of whitespace cropped off for html element positioning
ImageResource drawnPath = new ImageResource("PathImage", image);
drawnPath.setX(bounds.getX());
drawnPath.setY(bounds.getY());
return drawnPath;
}
private void clearPathGraphics(Rectangle2D.Double bounds, Graphics2D gfx) throws IOException
{
Color transparent = new Color(255, 255, 255, 0);
gfx.setColor(transparent);
gfx.fillRect(0, 0, (int) bounds.getWidth() * 2, (int) bounds.getHeight() * 2);
Color fill = pdfColorToColor(state.getNonStrokingColor());
Color stroke = pdfColorToColor(state.getStrokingColor());
// crop off rendered path whitespace
gfx.translate(-bounds.getX(), -bounds.getY());
gfx.setPaint(stroke);
gfx.setColor(fill);
}
private void drawPathSegments(List path, Graphics2D gfx)
{
int[] xPts = new int[path.size()];
int[] yPts = new int[path.size()];
for (int i = 0; i < path.size(); i++)
{
PathSegment segmentOn = path.get(i);
xPts[i] = (int) segmentOn.getX1();
yPts[i] = (int) segmentOn.getY1();
}
gfx.fillPolygon(xPts, yPts, path.size());
}
private Rectangle2D.Double getPathBounds(List path)
{
PathSegment first = path.get(0);
int minX = (int) first.getX1(), maxX = (int) first.getX1();
int minY = (int) first.getY2(), maxY = (int) first.getY1();
for (PathSegment segmentOn : path)
{
maxX = Math.max((int) segmentOn.getX1(), maxX);
maxX = Math.max((int) segmentOn.getX2(), maxX);
maxY = Math.max((int) segmentOn.getY1(), maxY);
maxY = Math.max((int) segmentOn.getY2(), maxY);
minX = Math.min((int) segmentOn.getX1(), minX);
minX = Math.min((int) segmentOn.getX2(), minX);
minY = Math.min((int) segmentOn.getY1(), minY);
minY = Math.min((int) segmentOn.getY2(), minY);
}
int width = maxX - minX;
int height = maxY - minY;
int x = minX;
int y = minY;
return new Rectangle2D.Double(x, y, width, height);
}
private Color pdfColorToColor(PDColor color) throws IOException
{
float[] rgb = color.getColorSpace().toRGB(color.getComponents());
return new Color(rgb[0], rgb[1], rgb[2]);
}
}