I have a pdf file (generated from scanning) and want to set a mechanism where prior to print, I am prompt to enter a field (contro开发者_如何学Pythonl number) and the field is printed in evey page of the document. How can I do this?
You can use iTextSharp to add Text to a PDF and you can use GhostScript to send the PDF to a printer.
IIRC, there's a document level pre-print event you can hook into with javaScript.
1) Place a field with the exact same name on every page of your PDF.
PdfReader reader = new PdfReader( "somePath.pdf" );
PdfStamper stamper = new PdfStamper( reader, outStream );
PdfWriter writer = stamper.getWriter();
for (int curPage = 1; curPage <= reader.getNumberOfPages(); ++curPage) {
TextField fld = new TextField ( writer, someRect, "thatFieldName" );
// might want to modify the field appearance here.
stamper.addAnnotation( fld.getTextField(), curPage );
}
2) In the pre-print event, query the user for this field value, then set that field to the value.
You need to create an additional actions dictionary in the document catalog. In Acrobat is fairly well buried. Advanced->Document Processing->Set Document Actions. You want the "Will Print" event:
var response = app.response( questionText/*, dialogTitle, defaultVal, bPassword, entryLabel*/ );
this.getField( "thatFieldName" ).value = response;
Not much to it. To do this programmatically in iText (my personal favorite, nope not biased at all;)
PdfReader reader = new PdfReader( "myFile.pdf" );
PdfStamper stamper = new PdfStamper( myReader, outStream );
PdfWriter stamperWriter = stamper.getWriter();
stamperWriter.setAdditionalAction( PdfName.WP, PdfAction.javaScript( scriptString, stamperWriter ) );
stamper.close();
An excellent option is PDFSharp - We use this for a variety of tasks, from rendering forms over graphics, embedding barcodes on PDF documents, etc. - works a treat, and I highly recommend it.
精彩评论