代码在下面。它是从标准itext 7示例改编而成的,以添加PdfRedactAnnotation。链接注解有效,但图章和修订不起作用。我正在使用的PDF链接为here。有人知道为什么这行不通吗?与PDF有关吗?
我的最终目标是在文本的各个部分上放置一些白色矩形,以便在打印时不会显示文本。
import java.io.File;
import java.nio.file.Paths;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfRedactAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfStampAnnotation;
public class AddRotatedAnnotation {
static final String SRC = Paths.get("calendar_2018-08-04_2018-08-19.pdf").toString();
static final String DEST = Paths.get("calendar_clean.pdf").toString();
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddRotatedAnnotation().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
PdfAction action = PdfAction.createURI("http://pages.itextpdf.com/ebook-stackoverflow-questions.html");
Rectangle linkLocation1 = new Rectangle(30, 770, 90, 30);
PdfAnnotation link1 = new PdfLinkAnnotation(linkLocation1)
.setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
.setAction(action)
.setColor(ColorConstants.RED.getColorValue());
pdfDoc.getFirstPage().addAnnotation(link1);
Rectangle linkLocation2 = new Rectangle(30, 670, 30, 90);
PdfAnnotation link2 = new PdfRedactAnnotation(linkLocation2)
.setColor(ColorConstants.BLACK.getColorValue());
pdfDoc.getFirstPage().addAnnotation(link2);
Rectangle linkLocation3 = new Rectangle(150, 770, 90, 30);
PdfAnnotation stamp1 = new PdfStampAnnotation(linkLocation3)
.setStampName(new PdfName("Confidential"))
.setContents("Landscape").setColor(ColorConstants.BLACK.getColorValue());
pdfDoc.getFirstPage().addAnnotation(stamp1);
Rectangle linkLocation4 = new Rectangle(150, 670, 90, 90);
PdfAnnotation stamp2 = new PdfStampAnnotation(linkLocation4)
.setStampName(new PdfName("Confidential"))
.setContents("Portrait")
.put(PdfName.Rotate, new PdfNumber(90));
pdfDoc.getFirstPage().addAnnotation(stamp2);
Rectangle linkLocation5 = new Rectangle(250, 670, 90, 90);
PdfAnnotation stamp3 = new PdfStampAnnotation(linkLocation5)
.setStampName(new PdfName("Confidential"))
.setContents("Portrait")
.put(PdfName.Rotate, new PdfNumber(45));
pdfDoc.getFirstPage().addAnnotation(stamp3);
pdfDoc.close();
}
}
答案 0 :(得分:1)
通过运行OP的代码,问题显而易见:
这些问题可以单独解决:
我相应地将您的注释link2
和stamp1
分别增强为link2x
和stamp1x
:
Rectangle linkLocation2x = new Rectangle(150, 470, 30, 90);
PdfAnnotation link2x = new PdfRedactAnnotation(linkLocation2x)
.setColor(ColorConstants.BLACK.getColorValue());
PdfFormXObject formN = new PdfFormXObject(linkLocation2x);
PdfCanvas canvasN = new PdfCanvas(formN, pdfDoc);
canvasN.setFillColorGray(1)
.rectangle(linkLocation2x.getX(), linkLocation2x.getY(), linkLocation2x.getWidth(), linkLocation2x.getHeight())
.fill();
link2x.setNormalAppearance(formN.getPdfObject());
link2x.setFlag(PdfAnnotation.PRINT);
pdfDoc.getFirstPage().addAnnotation(link2x);
Rectangle linkLocation3x = new Rectangle(150, 370, 90, 30);
PdfAnnotation stamp1x = new PdfStampAnnotation(linkLocation3x)
.setStampName(new PdfName("Confidential"))
.setContents("Landscape").setColor(ColorConstants.BLACK.getColorValue());
formN = new PdfFormXObject(linkLocation3x);
canvasN = new PdfCanvas(formN, pdfDoc);
canvasN.setFillColorGray(1)
.rectangle(linkLocation3x.getX(), linkLocation3x.getY(), linkLocation3x.getWidth(), linkLocation3x.getHeight())
.fill();
stamp1x.setNormalAppearance(formN.getPdfObject());
stamp1x.setFlag(PdfAnnotation.PRINT);
pdfDoc.getFirstPage().addAnnotation(stamp1x);
如果您这样做不仅是为了打印,而且是为了分发电子PDF,则应添加修订注释,然后运行修订,,例如使用iText 7 pdfSweep add-on来删除该信息,而不仅仅是覆盖它。