![](/img/trans.png)
[英]unable to put stamp using itext7 using Java lanaguage on only skia generated pdf (shows inverted stamp)
[英]i have a stamp which has a check mark and current date. i need to add this stamp to all the pages in a pdf using itext and java
我有一张上面有复选标记和当前日期的邮票。
我需要在使用itext和java的按钮上单击时,将此邮票添加到pdf中的所有页面。
印章是一种具有文本字段的表单,其中使用javascript自动输入日期。
PdfReader reader = new PdfReader(src);
PdfReader s_reader = new PdfReader(stationery);
// Create the stamper
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
// Add the stationery to each page
PdfImportedPage page = stamper.getImportedPage(s_reader, 1);
int n = reader.getNumberOfPages();
PdfContentByte background;
for (int i = 1; i <= n; i++) {
background = stamper.getUnderContent(i);
background.addTemplate(page, 0, 0);
}
// Close the stamper
stamper.close();
reader.close();
s_reader.close();
现在,如何从pdf中获取该保存的图章(窗体),为其添加当前日期,以及如何将该图章添加到Pdf文档的所有页面上?
需要JavaScript吗?
创建pdf
private static final Font headerFont = new Font(Font.FontFamily.TIMES_ROMAN, 9,
Font.BOLD, BaseColor.BLACK);
Document document = new Document(PageSize.A4, 20, 20, 120, 50);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(realPath + "/PdfTable.pdf"));
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
HeaderFooterOfPdf event = new HeaderFooterOfPdf(batchDate);
writer.setPageEvent(event);
document.open();
//to create table
PdfPTable vppTable = new PdfPTable(new float[]{3, 7, 9, 3, 5, 3, 3, 3, 4});
vppTable.setWidthPercentage(100f);
//pdf table header
vppTable.addCell(new PdfPCell(new Phrase("Your column name", headerFont)));
vppTable.addCell(new PdfPCell(new Phrase("Your column name", headerFont)));
vppTable.setHeaderRows(1);
//to add cell data
PdfPCell slNoCell = new PdfPCell(new Phrase("your data", contentFont));
slNoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
slNoCell.setPaddingLeft(15f);
vppTable.addCell(slNoCell);
document.add(vppTable);
document.close();
HeaderFooterOfPdf类
public class HeaderFooterOfPdf extends PdfPageEventHelper{
public HeaderFooterOfPdf(String batchDate){
this.batchDate = batchDate;
}
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
}
@Override
public void onStartPage(PdfWriter writer, Document document) {
//do what you want to enter on header
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
//do what you want to enter on footer
}
}
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.Set;
import java.util.TreeSet;
import java.text.*;
//itext libraries to read & write to a PDF file
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
public class TwoPasses1
{
public static final String RESULT = "K:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\RAINBOW ORDERS\\111111\\PADR Release\\Final PADR Release 1.pdf";
public static void main(String[] args) throws DocumentException, IOException
{
// ADD THE FOOTER, Create a reader
PdfReader reader = new PdfReader("K:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\RAINBOW ORDERS\\111111\\PADR Release\\Final PADR Release.pdf");
// Create a stamper
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
// Loop over the pages and add a footer to each page
int n = reader.getNumberOfPages();
for(int i = 1; i <= n; i++)
{
getFooterTable(i, n).writeSelectedRows(0, -1, 34, 80, stamper.getOverContent(i));
// getFooterTable(i, n).writeSelectedRows(0, -1, 34, 803, stamper.getOverContent(i));
}
// Close the stamper
stamper.close();
reader.close();
}
public static PdfPTable getFooterTable(int x, int y)
{
java.util.Date date = new java.util.Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
String month = sdf.format(date);
System.out.println("Month : " + month);
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(150);
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.TOP);
table.getDefaultCell().setBorder(Rectangle.LEFT);
table.getDefaultCell().setBorder(Rectangle.RIGHT);
table.getDefaultCell().setBorderColorTop(BaseColor.BLUE);
table.getDefaultCell().setBorderColorLeft(BaseColor.BLUE);
table.getDefaultCell().setBorderColorRight(BaseColor.BLUE);
table.getDefaultCell().setBorderWidthTop(2f);
table.getDefaultCell().setBorderWidthLeft(2f);
table.getDefaultCell().setBorderWidthRight(2f);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
Font font1 = new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLUE);
table.addCell(new Phrase("CONTROLLED COPY", font1));
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.LEFT);
table.getDefaultCell().setBorder(Rectangle.RIGHT);
table.getDefaultCell().setBorderColorLeft(BaseColor.BLUE);
table.getDefaultCell().setBorderColorRight(BaseColor.BLUE);
table.getDefaultCell().setBorderWidthLeft(2f);
table.getDefaultCell().setBorderWidthRight(2f);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.RED);
table.addCell(new Phrase(month, font));
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.LEFT);
table.getDefaultCell().setBorder(Rectangle.RIGHT);
table.getDefaultCell().setBorder(Rectangle.BOTTOM);
table.getDefaultCell().setBorderColorLeft(BaseColor.BLUE);
table.getDefaultCell().setBorderColorRight(BaseColor.BLUE);
table.getDefaultCell().setBorderColorBottom(BaseColor.BLUE);
table.getDefaultCell().setBorderWidthLeft(2f);
table.getDefaultCell().setBorderWidthRight(2f);
table.getDefaultCell().setBorderWidthBottom(2f);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(new Phrase("BLR DESIGN DEPT.", font1));
return table;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.