public class PdfConv { public int startConversion(String pdfFile) { ... } public int getPages() { ... } public int nextPage(int pageNo, String outputFileName) { ... } public int endConversion() { ... } }
public static void main(String[] argv) throws Exception { for(int jjk =0; jjk <argv.length; ++jjk) { PdfConv conv = new PdfConv(); conv.startConversion(argv[jjk]); int k = conv.getPages(); for (int j = 0; j < k; ++j) { conv.nextPage(j, argv[jjk] + "_" + j + ".svg"); conv.nextPage(j, argv[jjk] + "_" + j + ".png"); conv.nextPage(j, argv[jjk] + "_" + j + ".swf"); ... } } }
private PDFFile pdf = null; private FileChannel fic = null; public int startConversion(String pdfFile) throws Exception { File fix = new File(pdfFile); FileInputStream fin = new FileInputStream(fix); fic = fin.getChannel(); MappedByteBuffer mbb = fic.map(FileChannel.MapMode.READ_ONLY, 0, fix.length()); pdf = new PDFFile(mbb); return pdf.getNumPages(); } public int getPages() throws Exception { return pdf.getNumPages(); } public int endConversion() throws Exception { if (fic != null) fic.close(); pdf = null; fic = null; return 1; } public int nextPage(int pageNo, String outputMask) { PDFPage page = pdf.getPage(pageNo + 1); if (page == null) return -1; Rectangle bounds = page.getBBox().getBounds(); DrawingCtx ctx = DrawingCtxBuilder.build( outputMask, new Dimension(bounds.x + bounds.width, bounds.y + bounds.height)); PDFRenderer rx = new PDFRenderer(page, ctx.getContext(), bounds, null, null); rx.go(); rx.waitForFinish(); ctx.saveTo(outputMask); return 1; }
abstract class DrawingCtx { protected Graphics2D g2; protected Dimension size; DrawingCtx(Dimension size) { this.size = size; } public Graphics2D getGraphics() { return g2; } public abstract void saveTo(String fileName) throws Exception; } class DrawingCtxBuilder { public static build(String fileName, Dimension size) throws Exception { String type = fileName.substring(fileName.lastIndexOf('.') + 1).toUpperCase(); if(type.equals("SVG")) return new SvgDrawingCtx(size); else if(type.equals("PNG")) return new ImageDrawingCtx(size); else if(type.equals("SWF")) return new SwfDrawingCtx(size); ... throw new Exception(type + ": unknown converter requested"); } }
class SvgDrawingCtx extends DrawingCtx { private DOMImplementation domImpl; private Document doc; private SVGGraphics2D svgGenerator; SvgDrawingCtx(Dimension size) { super(size); domImpl = SVG12DOMImplementation.getDOMImplementation(); doc = domImpl.createDocument(SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_SVG_TAG, null); svgGenerator = new SVGGraphics2D(doc); svgGenerator.getGeneratorContext().setPrecision(4); svgGenerator.getGeneratorContext().setEmbeddedFontsOn(true); g2 = svgGenerator; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.setRenderingHint( RenderingHintsKeyExt.KEY_AVOID_TILE_PAINTING, RenderingHintsKeyExt.VALUE_AVOID_TILE_PAINTING_ON); } public void saveTo(String fn) throws Exception { Element svgRoot = svgGenerator.getRoot(); OutputStream os = new FileOutputStream(fn); if (fn.endsWith(".svgz")) os = new GZIPOutputStream(os); svgGenerator.stream(svgRoot, new OutputStreamWriter(os), false /* CSS */, true /* escaped */); os.close(); } }
class ImageDrawingCtx { private BufferedImage bf = null; ImageDrawingCtx(Dimension size) { super(size); bf = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); g2 = (Graphics2D) bf.getGraphics(); } public void saveTo(String fn) throws Exception { OutputStream os = new FileOutputStream(fn); ImageIO.write(bf, "PNG", os); os.close(); g2.dispose(); bf = null; } }
class SwfDrawingCtx extends DrawingCtx { SwfDrawingCtx(Dimension size) { super(size); g2 = new SpriteGraphics2D(size.width, size.height); } public void saveTo(String fn) throws Exception { OutputStream os = new FileOutputStream(fn); flash.swf.Frame frame1; Movie m = new Movie(); m.version = 7; m.bgcolor = new SetBackgroundColor(SwfUtils.colorToInt(255, 255, 255)); m.framerate = 12; frame1 = new flash.swf.Frame(); DefineSprite tag = ((MyG2D) g2).defineSprite("swf-test"); frame1.controlTags.add(new PlaceObject(tag, 0)); m.frames = new ArrayList(1); m.frames.add(frame1); TagEncoder tagEncoder = new TagEncoder(); MovieEncoder movieEncoder = new MovieEncoder(tagEncoder); movieEncoder.export(m); tagEncoder.writeTo(os); os.close(); g2.dispose(); g2 = null; } }
class MyG2D extends SpriteGraphics2D { public MyG2D(int width, int height) { super(width, height); } public MyG2D() { super(); } @Override public boolean drawImage(Image image, AffineTransform at, ImageObserver obs) { // return super.drawImage(image, at, obs); } }
at.createTransformedShape(new Rectangle(0, 0, image.getWidth(), image.getHeight()).getBounds()
returns us a 1x1 rectangle. @Override public boolean drawImage(Image image, AffineTransform at, ImageObserver obs) { AffineTransform good = getTransform(); good.concatenate(at); return super.drawImage(image, good, obs); }
Source: https://habr.com/ru/post/153633/
All Articles