首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iText冲压- Java

iText冲压- Java
EN

Stack Overflow用户
提问于 2015-05-14 10:00:06
回答 1查看 6.1K关注 0票数 5

我有困难的加盖PDF文件,但不使数字签名无效。

当前,我成功地在PDF上加盖了图章。但是,如果文档先前已签名,则签名不再有效。我理解为什么会出现这种情况,但是如果我使用Acrobat添加文本或使用注释对其进行标记,则签名是有效的。

我尝试添加注释或注释,但它仍然使签名无效。有没有一种方法可以在不使数字签名无效的情况下使用iText为PDF添加邮票?

下面是我用来盖章的代码片段:

代码语言:javascript
复制
        PdfReader reader = new PdfReader(inputstream);

        stamp = new PdfStamper(reader, new FileOutputStream(file));


        PdfContentByte pcb;
        BaseFont bf = BaseFont.createFont("Courier", BaseFont.CP1250,BaseFont.EMBEDDED);

        Rectangle r = reader.getPageSizeWithRotation(1);

        pcb = stamp.getOverContent(1);

        // set the font and size
        float size = 12;
        pcb.setFontAndSize(bf, size);

        float width = 90;
        float centerX = 0, startY = 0;
        centerX = r.getWidth() - (width / 2) - 20;
        startY = r.getHeight() - (15 * 2) - 145;

        pcb.beginText();
        pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, stampText, centerX, startY, 0);

        pcb.setFontAndSize(bf, 10);
        pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, date, centerX-9, startY-8, 0);
        pcb.endText();
        stamp.close();          

任何帮助都将不胜感激,谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-14 11:33:38

我做到了‘)

下面的代码用于使用iText将自定义文本添加到文档中,而不使数字签名无效。

代码语言:javascript
复制
//Read the source PDF
PdfReader reader = new PdfReader(inputstream);
//Create PdfStamp object
stamp = new PdfStamper(reader, new FileOutputStream(file), '\0', true);

//Create the proper annotation
PdfAnnotation annot = PdfAnnotation.createFreeText(stamp.getWriter(),  new Rectangle(150, 150, 200, 200), "Annotation 1", pcb);
annot.setFlags(PdfAnnotation.FLAGS_PRINT);

//Insert the annotation         
stamp.addAnnotation(annot, 1);
//Close the stamp
stamp.close();  

编辑:

为了在不使数字签名无效的情况下向文档插入图像印章,我使用了以下代码:

代码语言:javascript
复制
//Read the pdf 
PdfReader reader = new PdfReader(inputstream);
//Use PdfStamper in append mode
stamp = new PdfStamper(reader, new FileOutputStream(file), '\0', true); 

//Read the image
Image img = Image.getInstance(ImageIO.read(imgStream), null);

float w = img.getScaledWidth();
float h = img.getScaledHeight();
Rectangle location = new Rectangle(70, 770 - h, 70 + w, 770);

//Create stamp annotation           
PdfAnnotation stampAnnot = PdfAnnotation.createStamp(stamp.getWriter(), location, null, "ITEXT");
img.setAbsolutePosition(0, 0);
//Create new PdfContentByte from the stamp writer
//If you use cd = stamp.getOverContent(1) - you'll invalidate the signatures
PdfContentByte cb = new PdfContentByte(stamp.getWriter());
PdfAppearance app = cb.createAppearance(w, h);
app.addImage(img);
stampAnnot.setAppearance(PdfName.N, app);
stampAnnot.setFlags(PdfAnnotation.FLAGS_PRINT);

stamp.addAnnotation(stampAnnot, 1);
reader.close();
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30234407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档