I have been working with SVG formats recently. Mainly, trying to find a decent swing component for creating and modifying SVGs. As a result of such escapade, I decided to write a PDF to SVG converter. Now, I haven't fully tested it, but with the PDFs I have tried, it converted successfully.
The libraries I used were Batik 1.7 and PDF-Renderer v1.0.5
Here is My Main
public class ConversionMain { public ConversionMain() { File pdf = new File("ss12.pdf"); PDFUtility.PDF_TO_SVG(pdf); } public static void main(String[] args) { new ConversionMain(); } }
Here is my PDFUtility function for taking PDF, reading it and then converting to SVG
public class PDFUtility { public static void PDF_TO_SVG(File pdfFile) { try { RandomAccessFile raf = new RandomAccessFile(pdfFile, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdf = new PDFFile(buf); DOMImplementation domImp = GenericDOMImplementation.getDOMImplementation(); for (int i = 1; i <= pdf.getNumPages(); i++) { PDFPage page = pdf.getPage(i); // image dimensions int width = (int) page.getWidth(); //Can be changed to whatever you like int height = (int) page.getHeight(); Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); Image image = page.getImage(width, height, rect, null, true, true); String svgNS = "http://www.w3.org/2000/svg"; Document document = domImp.createDocument(svgNS, "svg", null); SVGGraphics2D svgGenerator = new SVGGraphics2D(document); Graphics graphics = svgGenerator.create(); graphics.drawImage(image, 0, 0, null); boolean userCSS = true; FileOutputStream fos = new FileOutputStream(new File("Page " + i + ".svg")); Writer out = new OutputStreamWriter(fos); svgGenerator.stream(out, userCSS); } } catch (Exception e) { e.printStackTrace(); } } }
Try it out and let me know what you think
Thanks!
It's entirely irritating in light of the fact that on my PC I can simply download a swf player and everything is great. On my Mac, notwithstanding, I can't discover 'whatever' will play them. At the point when I attempt to play them it just opens Illustrator, which (obviously) sits idle. anyconv.com
ReplyDeleteI understand this column. I realize You put a many of struggle to found this story. I admire your process.
ReplyDelete먹튀검증
I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. cfa practice exam
ReplyDeleteAppreciate your sharing this greatest doc... https://www.vanguardngr.com/2020/12/some-popular-digital-audio-formats-you-need-to-know/
ReplyDelete