首页 文章

我有一张带有复选标记和当前日期的印章 . 我需要使用itext和java将这个标记添加到pdf中的所有页面

提问于
浏览
0

我有一张带有复选标记和当前日期的印章 .
我需要使用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吗?

2 回答

  • 1

    To create 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 class

    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
    }
    }
    
  • 1
    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;
             }
    }
    

相关问题