I want to make all the text in an existing PDF transparent.
Option 1: select all the text, find a color property and change it to "colorless"
Or, if there is no such property
Option 2: Parse the page content Stream and all Form XObjects for that page, detect text blocks (BT/ET), and set the render mode to invisble.
This seems to be a complex operation.
Here is my example file
The following code is generating PDF(example pdf file):
Document document = new Document(new Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); PdfContentByte picCanvas = null; PdfContentByte txtCanvas = null; if (isUnderPic) { txtCanvas = writer.getDirectContentUnder(); picCanvas = writer.getDirectContent(); } else { txtCanvas = writer.getDirectContent(); picCanvas = writer.getDirectContentUnder(); } BaseFont bf = null; if (null != pageList) { int[] dpi = { 0, 0 }; if (dpiType == 1) { dpi[0] = 300; dpi[1] = 300; } else if (dpiType == 2) { dpi[0] = 600; dpi[1] = 600; } for (int i = 0; i < pageList.size(); i++) { PDFPage page = pageList.get(i); Image pageImage = null; if (pdfType == 3) { pageImage = Image.getInstance(page.getBinImage()); } else { pageImage = Image.getInstance(page.getOriImage()); } if (pageImage.getWidth() > 0) { pageImage.scaleAbsolute(page.getWidth(), page.getHeight()); } pageImage.setAbsolutePosition(0, 0); picCanvas.addImage(pageImage); if (pdfType == 2 || pdfType == 3) { for (PageElement ele : page.getElementList()) { if (ele.getType().equals(PDFConstant.ElementType.PDF_ELEMENT_CHAR)) { txtCanvas.beginText(); if (isColor) { txtCanvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); txtCanvas.setColorFill(BaseColor.RED); } else { txtCanvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE); } String font = ele.getFont(); try { bf = fonts.get(font); if (null == bf) { bf = BaseFont.createFont(font, "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); fonts.put(font, bf); } } catch (Exception e) { bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); fonts.put(font, bf); } txtCanvas.setFontAndSize(bf, ele.getFontSize()); txtCanvas.setTextMatrix(ele.getPageX(), ele.getPageY(page.getRcInPage())); txtCanvas.showText(ele.getCode()); txtCanvas.endText(); } } } if (StringUtils.isNotBlank(cutPath)) { for (PageElement ele : page.getElementList()) { if (ele.getType().equals(PDFConstant.ElementType.PDF_ELEMENT_PIC) && StringUtils.isNotBlank(ele.getCutPicSrc())) { ImageTools.cutPic(ele.getRcInImage(), page.getOriImage(), ele.getCutPicSrc(), dpi); } } } if (pdfType == 3) { logger.debug("pdfType == 3"); for (PageElement ele : page.getElementList()) { if (ele.getType().equals(PDFConstant.ElementType.PDF_ELEMENT_PIC) && StringUtils.isNotBlank(ele.getCutPicSrc())) { if (new File(ele.getCutPicSrc()).exists()) { Image cutCover = Image.getInstance(ImageTools.drawImage((int) ele.getWidth(), (int) ele.getHeight())); if (cutCover.getWidth() > 0) { cutCover.scaleAbsolute(ele.getWidth(), ele.getHeight()); } cutCover.setAbsolutePosition(ele.getPageX(), ele.getPageY(page.getRcInPage())); picCanvas.addImage(cutCover); Image pic = Image.getInstance(ele.getCutPicSrc()); if (pic.getWidth() > 0) { pic.scaleAbsolute(ele.getWidth(), ele.getHeight()); } pic.setAbsolutePosition(ele.getPageX(), ele.getPageY(page.getRcInPage())); picCanvas.addImage(pic); } } } } if (i + 1 < pageList.size()) { document.setPageSize(new Rectangle(pageList.get(i + 1).getWidth(), pageList.get(i + 1).getHeight())); } else { document.setPageSize(new Rectangle(pageList.get(i).getWidth(), pageList.get(i).getHeight())); } document.newPage(); } } document.close(); 