Return-Path: X-Original-To: apmail-pdfbox-users-archive@www.apache.org Delivered-To: apmail-pdfbox-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 315FA17927 for ; Wed, 2 Sep 2015 15:23:26 +0000 (UTC) Received: (qmail 36879 invoked by uid 500); 2 Sep 2015 15:23:25 -0000 Delivered-To: apmail-pdfbox-users-archive@pdfbox.apache.org Received: (qmail 36859 invoked by uid 500); 2 Sep 2015 15:23:25 -0000 Mailing-List: contact users-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@pdfbox.apache.org Delivered-To: mailing list users@pdfbox.apache.org Received: (qmail 36435 invoked by uid 99); 2 Sep 2015 15:23:25 -0000 Received: from Unknown (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Sep 2015 15:23:25 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 0A9DB18210F for ; Wed, 2 Sep 2015 15:23:25 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.993 X-Spam-Level: X-Spam-Status: No, score=0.993 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H2=-0.001, RP_MATCHES_RCVD=-0.006] autolearn=disabled Received: from mx1-eu-west.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id Sf218vYZtbo9 for ; Wed, 2 Sep 2015 15:23:23 +0000 (UTC) Received: from mailout07.t-online.de (mailout07.t-online.de [194.25.134.83]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with ESMTPS id 7521A20D7B for ; Wed, 2 Sep 2015 15:23:23 +0000 (UTC) Received: from fwd27.aul.t-online.de (fwd27.aul.t-online.de [172.20.26.132]) by mailout07.t-online.de (Postfix) with SMTP id 26FD7622EE3 for ; Wed, 2 Sep 2015 17:23:17 +0200 (CEST) Received: from [192.168.2.102] (TtwlhcZHrhAfe9YmYtZmY1VyM83ITRhbaxTOW08HiCQNHb0d1v6W-9CCWmMOx6JQMD@[217.231.166.225]) by fwd27.t-online.de with (TLSv1.2:ECDHE-RSA-AES256-SHA encrypted) esmtp id 1ZX9sO-4bZI000; Wed, 2 Sep 2015 17:23:04 +0200 Subject: Re: Replacing images in PDFs. To: users@pdfbox.apache.org References: <55E5D2C5.7050405@t-online.de> From: Tilman Hausherr Message-ID: <55E71459.3020408@t-online.de> Date: Wed, 2 Sep 2015 17:23:05 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-ID: TtwlhcZHrhAfe9YmYtZmY1VyM83ITRhbaxTOW08HiCQNHb0d1v6W-9CCWmMOx6JQMD X-TOI-MSGID: 1c1703df-be2e-47d3-bb2c-581928620f8d Am 02.09.2015 um 10:28 schrieb Lachezar Dobrev: > In-line > > 2015-09-01 19:31 GMT+03:00 Tilman Hausherr : > >> Am 01.09.2015 um 11:26 schrieb Lachezar Dobrev: >> >>> Hello all. >>> I'm tasked with providing a service to generate PDFs from template PDFs >>> by replacing text place holders and image place holders with data from a >>> database. >>> ​ For replacing text we decided to use Form Field​s to keep minimal >>> effect >>> on the page layout, and to avoid problems with texts being split into >>> parts >>> by the editor. >>> Replacing images is also achievable using >>> COSStream.replaceWithStream(COSStream). >>> >>> Q1: The replaceWithStream method is being deprecated. What can I use to >>> do the same thing in PDFBox 2? I tried stream-copying, but I get a >>> "WARNING: DCTFilter#encode is not implemented yet, skipping this stream." >>> and the image disappears from the output file. >>> >> If you want to copy a JPEG, you should use createRawInputStream(). >> >> The JPEG should be of the same size and same colorspace than the original >> image. >> >> Alternatively - just put nothing in the templare, and simply put an image >> of whatever size you want with traditional methods (see image handling in >> PDPageContentStream) >> > ​ The image in the template PDF is needed to tell what to put where with > what size. > Some time after I asked the question I found out I can create a COSStream > using LosslessFactory.createFromImage(doc, img).getCOSStream()​ > ​. Afterwards I called createOutputStream(COSBase filter) with the filter > of the new stream, and copied from one to the other. The replacing image is > already the same size, but the colour profile may differ (Templates > commonly contain a blank black-and-white all-white image as a place holder). > > Q2: Is there any way to identify which image is what? Form fields have >>> Mapping Name that I can use to detect which field needs to contain what, >>> but I can not find a way to get some user-controllable identifier for an >>> image. Any thoughts? >>> >> Images do have a name in the resources. So it should work if you do the >> template yourself. >> > ​ I could not find how to obtain that. I don't see a getName() in > PDImageXObject. Can you point me to where I should look for that?​ To get the XObject names, do this: page.getResources().getXObjectNames() to get an image with a specific name: PDImageXObject img = (PDImageXObject) res.getXObject(COSName.getPDFName("TheImageName")); The images do not know their own names. To replace an image in the resource table, do res.put(COSName.getPDFName("TheImageName"), img); Note that you're bypassing the cache (if I've understood the source correctly), so you won't be able to get that updated image with the getXObject() method. Tilman > > Tilman > ​ Thank you for the patience.​ > > ​ Lachezar​ > --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org For additional commands, e-mail: users-help@pdfbox.apache.org