0

My problem is that I need to add a background to an iText PDF file. The background should fill the whole page and is an image.

Please answer quickly. Thanks for your help

Here is the code and I want to place an image in the background.

enter image description here enter image description here enter image description here

        Document document = new Document();

        PdfPTable table = new PdfPTable(2);
        PdfPCell Left = new PdfPCell();
        Left.setBorder(0);
        Left.setHorizontalAlignment(Element.ALIGN_CENTER);
        PdfPCell Right = new PdfPCell();
        Right.setBorder(0);
        Right.setHorizontalAlignment(Element.ALIGN_CENTER);

        try {
            PdfWriter.getInstance(document, new 
        FileOutputStream("C:\\Users\\jensh\\Desktop\\QuizErgebnis.pdf"));
        } catch (FileNotFoundException | DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        document.open();
        com.itextpdf.text.Font font = 
        FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);

        try {
            com.itextpdf.text.Image image = 
        com.itextpdf.text.Image.getInstance("bg.jpg");
        } catch (BadElementException | IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        
        try {
            Paragraph H1 = new Paragraph("ZERTIFIKAT", font);
            H1.setAlignment(Element.ALIGN_CENTER);
            H1.setSpacingAfter(30);
    
            document.add(H1);
            table.addCell(Left);
            table.addCell(Right);
            document.add(table);

        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        document.close();

    
Jens
  • 3
  • 2
  • if you need it quickly, here's the documentation around background: https://kb.itextpdf.com/home/it7kb/search?q=background&max=500 – André Lemos Jul 16 '20 at 14:37
  • I tried but it doesn´t work – Jens Jul 16 '20 at 15:11
  • can you add to your original post what you tried (a code sample)? – André Lemos Jul 17 '20 at 06:40
  • Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("Quiz.pdf")); } catch (FileNotFoundException | DocumentException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } document.open(); //create image... – Jens Jul 17 '20 at 07:08
  • can you update the initial question and add the whole code? I can't see on the document what attempt you did to get the background working. Also, it seems you are using iText5 (my link was for iText7). Here is the link for 5 https://kb.itextpdf.com/home/search?q=background&max=500&s=IT5KB – André Lemos Jul 17 '20 at 08:19
  • Does this answer your question? [Set a fix background image for all my pages in PDF iText ASP C#](https://stackoverflow.com/questions/26688288/set-a-fix-background-image-for-all-my-pages-in-pdf-itext-asp-c-sharp) – André Lemos Jul 17 '20 at 08:21
  • *"I tried but it doesn´t work"* - that is a problem decrypting missing too much detail. – mkl Jul 17 '20 at 08:47
  • the commands are marked red but I can't import classes etc – Jens Jul 17 '20 at 09:03
  • which jars are you using/can you share your build system configuration (maven/gradle/ant/wtv)? – André Lemos Jul 17 '20 at 09:40
  • I am using iText 5.0.1 – Jens Jul 17 '20 at 10:06
  • iText 5.0.1 is ancient. Your code should work nonetheless but you should definitively update. – mkl Jul 17 '20 at 13:36
  • please also show your import lines, there might be an issue with them. – mkl Jul 17 '20 at 13:38
  • added it to the question – Jens Jul 17 '20 at 15:33
  • There is no `import` line for the `Image` class you use. Thus, it isn't found. Simply add the applicable `Image` import statement. – mkl Jul 17 '20 at 17:57
  • The problem is that there isn´t an import statement – Jens Jul 18 '20 at 07:38
  • Your latest screen shot shows that your ide thinks `Image` is `java.awt.Image` but your code needs the `Image` class of iText which IIRC is `com.itextpdf.text.Image`. – mkl Jul 18 '20 at 11:46
  • Here is a video. It does not work https://1drv.ms/v/s!AgSdYq9Muoc0gpoDGjUfi9wmr1iOnQ?e=eFUeyb – Jens Jul 18 '20 at 12:20

0 Answers0