2022-04-14

add itext WaterMark to android java pdf

I need to add an itext watermark to my pdf android java pdf file my sample current code for creating the pdf file:

   File docsFolder = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS), "osary/estmara");
         String currentDate = new SimpleDateFormat("dd-MM-yyyy_hh_mm_aaa", Locale.getDefault()).format(new Date());
            String pdfname = myEstmaraa.getTalabId() + "_" + currentDate + ".pdf";
            pdfFile = new File(docsFolder, pdfname);
            OutputStream output = new FileOutputStream(pdfFile);
            Document document = new Document(PageSize.A4);
            document.setMargins(5, 5, 5, 5);
            PdfWriter PdfWriters = PdfWriter.getInstance(document, output);
            PdfWriters.createXmpMetadata();
            PdfWriters.setTagged();
            document.open();
            //todo 2 preparing Header and directions and margin row 0
            PdfPTable tableForRowZero = new PdfPTable(new float[]{1});
            tableForRowZero.setSpacingBefore(5);
            tableForRowZero.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
            tableForRowZero.getDefaultCell().setFixedHeight(34);
            tableForRowZero.setTotalWidth(PageSize.A4.getWidth());
            tableForRowZero.setWidthPercentage(100);
            tableForRowZero.setHeaderRows(0);
            tableForRowZero.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
            cellss = new PdfPCell(new Phrase("رقم الاستمارة", FONT2));
            cellss.setHorizontalAlignment(Element.ALIGN_CENTER);
            cellss.setVerticalAlignment(Element.ALIGN_CENTER);
            cellss.setBackgroundColor(BaseColor.GRAY);
            tableForRowZero.addCell(cellss);
            tableForRowZero.setHeaderRows(0);
            cellss = new PdfPCell(new Phrase(String.valueOf(myEstmaraa.getTalabId()), FONT2));
            cellss.setHorizontalAlignment(Element.ALIGN_CENTER);
            cellss.setVerticalAlignment(Element.ALIGN_CENTER);
            tableForRowZero.addCell(cellss);
            document.add(tableForRowZero);
            document.close();

i have tried this code but it for desktop java: enter link description here

and the problem that i faced is this line:

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

when i change it to android i face a problem, i tried it like:

        com.itextpdf.kernel.pdf.PdfDocument pdfDoc = new com.itextpdf.kernel.pdf.PdfDocument(new PdfReader(pdfFile.getPath()),new PdfWriter(pdfFile.getPath()));

it gives me this hint:

Cannot resolve constructor 'PdfWriter(java.lang.String)'

the code i tried to make watermark:

   com.itextpdf.kernel.pdf.PdfDocument pdfDoc = new com.itextpdf.kernel.pdf.PdfDocument(new PdfReader(pdfFile.getPath()),new PdfWriter(pdfFile.getPath()));
           PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);
            PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(StandardFonts.HELVETICA));
            com.itextpdf.layout.element.Paragraph paragraph2 = new com.itextpdf.layout.element.Paragraph("This watermark is added UNDER the existing content")
                    .setFont(font)
          .setFontSize(15);

            Canvas canvasWatermark1 = new Canvas(under, pdfDoc.getDefaultPageSize())
                    .showTextAligned(paragraph2, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            canvasWatermark1.close();
            PdfCanvas over = new PdfCanvas(pdfDoc.getFirstPage());
            over.setFillColor(ColorConstants.BLACK);
            paragraph2 = new com.itextpdf.layout.element.Paragraph("This watermark is added ON TOP OF the existing content")
                    .setFont(font)
                    .setFontSize(15);

            Canvas canvasWatermark2 = new Canvas(over, pdfDoc.getDefaultPageSize())
                    .showTextAligned(paragraph2, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            canvasWatermark2.close();
            paragraph2 = new com.itextpdf.layout.element.Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
                    .setFont(font)
                    .setFontSize(15);
            over.saveState();

            // Creating a dictionary that maps resource names to graphics state parameter dictionaries
            PdfExtGState gs1 = new PdfExtGState();
            gs1.setFillOpacity(0.5f);
            over.setExtGState(gs1);
            Canvas canvasWatermark3 = new Canvas(over, pdfDoc.getDefaultPageSize())
                    .showTextAligned(paragraph2, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            canvasWatermark3.close();
            over.restoreState();

i tried also to add png image and set its opacity but it cover the content

      try {


                com.itextpdf.text.Image image1_emp_osary = null;
                Drawable d_emp_osary = getResources().getDrawable(R.drawable.ketm_estmara);
                BitmapDrawable bitDw_emp_osary = ((BitmapDrawable) d_emp_osary);
                Bitmap bmp_emp_osary = bitDw_emp_osary.getBitmap();
                ByteArrayOutputStream stream_emp_osary = new ByteArrayOutputStream();
                bmp_emp_osary.compress(Bitmap.CompressFormat.PNG, 100, stream_emp_osary);
                image1_emp_osary = com.itextpdf.text.Image.getInstance(stream_emp_osary.toByteArray());
                image1_emp_osary.scaleToFit(200, 200);
                image1_emp_osary.setAlignment(Element.ALIGN_BOTTOM);
                image1_emp_osary.setSpacingBefore(200);
                image1_emp_osary.setTransparency(new int[]{5, 5});
                document.add(new Chunk(image1_emp_osary, 0, 200));
                PdfContentByte canvas = PdfWriters.getDirectContentUnder();
                PdfGState state = new PdfGState();
                state.setFillOpacity(0.6f);
                canvas.setGState(state);
                canvas.addImage(image1_emp_osary);
                canvas.restoreState();
            } catch (Exception e) {
                e.printStackTrace();
            }


No comments:

Post a Comment