Replace mendix generated PDF with the compressed PDF File in Java Action
0
Hi , I am using this code to compress pdf files with this code It currently saves the compressed PDF file directly to the desktop using "PdfStamper" But I need a way so that I can replace the original pdf from mendix with the compressed one from my code or at least a way so I can get the compressed file as a return value. Any help will be appreciated. public class Java_actionGA extends CustomJavaAction<java.lang.Void> { private IMendixObject __Parameter; private system.proxies.FileDocument Parameter; private java.lang.String file; public Java_actionGA(IContext context, IMendixObject Parameter, java.lang.String file) { super(context); this.__Parameter = Parameter; this.file = file; } @java.lang.Override public java.lang.Void executeAction() throws Exception { this.Parameter = __Parameter == null ? null : system.proxies.FileDocument.initialize(getContext(), __Parameter); // BEGIN USER CODE try { float FACTOR = 0.5f; PdfName key = new PdfName("ITX_IdTRY"); PdfName value = new PdfName("TryPDF"); // Read the file PdfReader reader = new PdfReader(Core.getFileDocumentContent(this.getContext(), __Parameter)); int n = reader.getXrefSize(); PdfObject object; PRStream stream; // Look for image and manipulate image stream for (int i = 0; i < n; i++) { object = reader.getPdfObject(i); if (object == null || !object.isStream()) continue; stream = (PRStream) object; // if (value.equals(stream.get(key))) { PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE); // System.out.println(stream.type()); if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) { PdfImageObject image = new PdfImageObject(stream); BufferedImage bi = image.getBufferedImage(); if (bi == null) continue; int width = (int) (bi.getWidth() * FACTOR); int height = (int) (bi.getHeight() * FACTOR); BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); AffineTransform at = AffineTransform.getScaleInstance(FACTOR, FACTOR); Graphics2D g = img.createGraphics(); g.drawRenderedImage(bi, at); ByteArrayOutputStream imgBytes = new ByteArrayOutputStream(); ImageIO.write(img, "JPG", imgBytes); stream.clear(); stream.setData(imgBytes.toByteArray(), false, PRStream.BEST_COMPRESSION); stream.put(PdfName.TYPE, PdfName.XOBJECT); stream.put(PdfName.SUBTYPE, PdfName.IMAGE); stream.put(key, value); stream.put(PdfName.FILTER, PdfName.DCTDECODE); stream.put(PdfName.WIDTH, new PdfNumber(width)); stream.put(PdfName.HEIGHT, new PdfNumber(height)); stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8)); stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB); } } // Save altered PDF PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:\\Users\\Desktop\\CompressedFile.pdf")); stamper.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); Core.getLogger("Compress Image").info(e.getMessage()); }
asked
Ahmed Al-Toiama
2 answers
1
To store a FileDocument in Mendix from Java you need to use the StoreFileDocumentContent method in the Core API.