Hi Klaus,
import java.awt.geom.AffineTransform;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
import org.apache.pdfbox.util.LayerUtility;
public class CreateTester {
public static void main(String[] args) {
try {
float width = 150.0f;
float height = 400.0f;
PDRectangle sizePDF = new PDRectangle(width, height);
PDDocument aDoc = new PDDocument();
PDPage aPage = new PDPage(sizePDF);
aDoc.addPage(aPage);
PDPageContentStream aContent = new
PDPageContentStream(aDoc, aPage);
PDFont font = PDType1Font.HELVETICA_BOLD;
aContent.beginText();
aContent.setFont(font, 12);
aContent.moveTextPositionByAmount(2, 5);
aContent.drawString("Import a pdf file:");
aContent.endText();
aContent.close();
LayerUtility layerUtility = new LayerUtility(aDoc);
PDDocument toBeImported =
PDDocument.load("ImportTest.pdf");
// Get the page as a PDXObjectForm to place it
PDXObjectForm mountable = layerUtility.importPageAsForm(
toBeImported, 0);
// add compression to the stream (import deactivates
compression)
mountable.getPDStream().addCompression();
// add to the existing content stream
PDPageContentStream contentStream = new
PDPageContentStream(aDoc,
aPage, true, true);
// Store the graphics state
contentStream.appendRawCommands("q\n".getBytes("ISO-8859-1"));
// use a transformation to be able to scale and move
easily
AffineTransform transform = new AffineTransform();
// draw the PDXObjectForm
contentStream.drawXObject(mountable, transform);
// restore former graphics state
contentStream.appendRawCommands("Q\n".getBytes("ISO-8859-1"));
contentStream.close();
// close the imported document
toBeImported.close();
aDoc.save("pdftest.pdf");
aDoc.close();
} catch (Exception e) {
System.out.println(" error creating pdf file." +
e.toString());
}
}
}
Maruan Sahyoun
FileAffairs GmbH
Josef-Schappe-Straße 21
40882 Ratingen
Tel: +49 (2102) 89497 88
Fax: +49 (2102) 89497 91
[email protected]
www.fileaffairs.de
Geschäftsführer: Maruan Sahyoun
Handelsregister: AG Düsseldorf, HRB 53837
UST.-ID: DE248275827
Am 29.09.2014 um 16:04 schrieb [email protected]:
> Dear Maruan,
> thank you for your reply.
>
> The included pdf file can be found here:
> https://filexchange.lhsystems.com/fileXchange_download/index.php?file=104789de603a37ed709da477e26544c2b696cb
>
> The resulting pdf written by my program here:
> https://filexchange.lhsystems.com/fileXchange_download/index.php?file=10479081a168741db7f7b9a2f85899e19b8b3f
>
> I want to create a new document with a single page with text and a picture,
> which I import from another pdf file. The result can be one document.
>
> Klaus
>
> -----Ursprüngliche Nachricht-----
> Von: Maruan Sahyoun [mailto:[email protected]]
> Gesendet: Montag, 29. September 2014 15:41
> An: [email protected]
> Betreff: Re: importing pdf file
>
> Dear Klaus,
>
> I'm guessing from your code that
>
> a) you would like to generate a new document with a single page and some text
> and
> b) overlay another document on top of that page
>
> Is that correct? Unfortunately due to restrictions of the mailing list the
> attachments are not available. Could you upload these to a public location?
>
>
> BR
>
> Maruan Sahyoun
>
> FileAffairs GmbH
> Josef-Schappe-Straße 21
> 40882 Ratingen
>
> Tel: +49 (2102) 89497 88
> Fax: +49 (2102) 89497 91
> [email protected]
> www.fileaffairs.de
>
> Geschäftsführer: Maruan Sahyoun
> Handelsregister: AG Düsseldorf, HRB 53837
> UST.-ID: DE248275827
>
> Am 29.09.2014 um 15:05 schrieb <[email protected]>
> <[email protected]>:
>
>> Hello,
>> I'm validating the use of pdfbox 1.8.6 for the creation of a pdf file for an
>> editor. Therefore I need the possibility to include a pdf file (picture).
>>
>> I tried some coding, but it is not fully readable with Acrobat Reader. I'm
>> not sure, if I use the wrong concept for this or if it is simple a mistake
>> in pdfbox.
>>
>> Please can you give me a hint.
>>
>> My coding:
>>
>> public static void main(String[] args) {
>>
>> try
>> {
>> float width = 150.0f;
>> float height = 400.0f;
>> PDRectangle sizePDF = new PDRectangle(width,height);
>> PDDocument aDoc = new PDDocument();
>> PDPage aPage = new PDPage(sizePDF);
>> aDoc.addPage( aPage );
>> PDPageContentStream aContent = new PDPageContentStream(aDoc,
>> aPage);
>>
>> PDFont font = PDType1Font.HELVETICA_BOLD;
>> aContent.beginText();
>> aContent.setFont( font, 12 );
>> aContent.moveTextPositionByAmount( 2, 5 );
>> aContent.drawString( "Import a pdf file:" );
>> aContent.endText();
>>
>> File aFile = new File("c:\\hallo.pdf");
>> byte buffer[] = new byte[(int) aFile.length()];
>> FileInputStream In = new FileInputStream(aFile);
>> In.read(buffer);
>> In.close();
>>
>> ByteArrayInputStream in = new ByteArrayInputStream(buffer);
>> PDStream aStream = new PDStream (aDoc, in);
>> PDXObjectForm aObject = new PDXObjectForm( aStream );
>> aContent.drawXObject(aObject,2,30,150,150);
>>
>> aContent.close();
>> aDoc.save( "c:\\pdftest.pdf");
>> aDoc.close();
>> }
>> catch (Exception e)
>> {
>> System.out.println(" error creating pdf file."
>> + e.toString());
>> }
>> }
>> }
>>
>> The used data and the output is attached to the mail.
>>
>> Thanks
>> Klaus Graaf
>