本文整理匯總了Java中com.itextpdf.text.pdf.PdfWriter.setPageEvent方法的典型用法代碼示例。如果您正苦於以下問題:Java PdfWriter.setPageEvent方法的具體用法?Java PdfWriter.setPageEvent怎麽用?Java PdfWriter.setPageEvent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.itextpdf.text.pdf.PdfWriter
的用法示例。
在下文中一共展示了PdfWriter.setPageEvent方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createPDF
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public void createPDF(String outputFile, ArrayList<Question> qlist, boolean showCorrectAnswer,
PageCounter pagecounter, int maximumPageNumber, String inputFolder) throws DocumentException, IOException
{
_inputFolder = inputFolder;
Document document = new Document(PageSize.A4, 50, 50, 70, 50);
PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
pdfwriter.setBoxSize("art", new Rectangle(36, 54, 559, 788));
pdfwriter.setPageEvent(new HeaderFooter(maximumPageNumber));
if (pagecounter != null)
{
pdfwriter.setPageEvent(pagecounter);
}
document.open();
Paragraph p = new Paragraph();
// p.setSpacingBefore(SPACING);
p.setSpacingAfter(SPACING);
p.setIndentationLeft(INDENTATION);
writeQuestions(p, document, showCorrectAnswer, qlist);
document.close();
}
示例2: testParagraphBackgroundEventListener
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
@Test
public void testParagraphBackgroundEventListener() throws DocumentException, FileNotFoundException
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "document-with-paragraph-backgrounds.pdf")));
ParagraphBackground border = new ParagraphBackground();
writer.setPageEvent(border);
document.open();
document.add(new Paragraph("Hello,"));
document.add(new Paragraph("In this document, we'll add several paragraphs that will trigger page events. As long as the event isn't activated, nothing special happens, but let's make the event active and see what happens:"));
border.setActive(true);
document.add(new Paragraph("This paragraph now has a background. Isn't that fantastic? By changing the event, we can even draw a border, change the line width of the border and many other things. Now let's deactivate the event."));
border.setActive(false);
document.add(new Paragraph("This paragraph no longer has a background."));
document.close();
}
示例3: main
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
/**
* Generates a document with a header containing Page x of y and with a Watermark on every page.
* @param args no arguments needed
*/
public static void main(String args[]) {
try {
// step 1: creating the document
Document doc = new Document(PageSize.A4, 50, 50, 100, 72);
// step 2: creating the writer
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("pageNumbersWatermark.pdf"));
// step 3: initialisations + opening the document
writer.setPageEvent(new PageNumbersWatermark());
doc.open();
// step 4: adding content
String text = "some padding text ";
for (int k = 0; k < 10; ++k)
text += text;
Paragraph p = new Paragraph(text);
p.setAlignment(Element.ALIGN_JUSTIFIED);
doc.add(p);
// step 5: closing the document
doc.close();
}
catch ( Exception e ) {
e.printStackTrace();
}
}
示例4: createDocument
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public Document createDocument(ReportTitle reportTitle, OutputStream out) throws Exception {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, out);
if (reportTitle.isShowPageNo()) {
PdfReportPageNumber event = new PdfReportPageNumber(chineseFont);
writer.setPageEvent(event);
}
doc.open();
Paragraph paragraph = this.createReportTitle(reportTitle);
doc.add(paragraph);
return doc;
}
示例5: render
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
public void render( PdfWriter writer, Document document ) throws PdfRenderException{
writer.setPageEvent(page);
if( page.getBlocks() == null || page.getBlocks().length == 0 ){
writer.setPageEmpty( false );
writer.newPage();
}else{
document.newPage();
PdfContentByte cb = writer.getDirectContent();
for( BaseElement block : page.getBlocks() ){
block.onRender( cb );
}
}
}
示例6: setFooter
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
private void setFooter(PdfWriter writer) throws DocumentException,
IOException {
//HeaderFooter headerFooter = new HeaderFooter("海南國保", 10, PageSize.A4);
// 更改事件,瞬間變身 第幾頁/共幾頁 模式。
HeaderFooter headerFooter = new HeaderFooter();// 就是上麵那個類
writer.setBoxSize("art", PageSize.A4);
writer.setPageEvent(headerFooter);
}
示例7: generarPdf
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
protected File generarPdf(String author, String creator, String subject, String title, String contenido, String ruta, boolean concat) {
Document document = new Document(PageSize.A4, 35, 30, 70, 50);
FileOutputStream fileO;
File file = new File(ruta);
if (!file.exists()) {
try {
if (concat) {
fileO = new FileOutputStream(new File(ruta));
} else {
fileO = new FileOutputStream(ruta);
}
PdfWriter writer = PdfWriter.getInstance(document, fileO);
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
HeaderFooter event = new HeaderFooter();
writer.setPageEvent(event);
document.open();
if (!concat) {
document.addAuthor(author);
document.addCreator(creator);
document.addSubject(subject);
document.addCreationDate();
document.addTitle(title);
}
contenido = procesarHtml(contenido);
HTMLWorker htmlWorker = new HTMLWorker(document);
if (concat) {
htmlWorker.newPage();
}
htmlWorker.parse(new StringReader(contenido));
document.close();
File file1 = new File(ruta);
return file1;
} catch (Exception e) {
return null;
}
}
return file;
}
示例8: serialise
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
/**
* Serialises the evaluated parse tree as a PDF to the output stream
* @param pOutputStream The output stream for the serialised PDF
*/
public void serialise(OutputStream pOutputStream) {
// TODO WH: Is there actually a possibility this could be null at runtime, or should this be an assertion instead? (copied from HTML serialiser)
if (pOutputStream == null) {
throw new ExInternal("Output stream must not be null");
}
// PDF serialisation is done in two passes, the first pass bytes are streamed first, the second pass will then
// stream the final document to pOutputStream
ByteArrayOutputStream lFirstPassOutput = new ByteArrayOutputStream();
// Opening a writer with the first pass output stream will allow the document to be opened and written to the stream
PdfWriter lWriter = openWriter(mDocument, lFirstPassOutput);
// Set the document metadata (title, author etc.) provided to the serialiser
setDocumentMetadata();
// Page manager will handle document page events (i.e. page end)
lWriter.setPageEvent(mPageManager);
// Start with the default element attributes, start a document container and render from the root buffer of the
// evaluated parse tree
pushElementAttributes(DefaultElementAttributes.getDefaultAttributes());
startContainer(ElementContainerFactory.getContainer(mDocument));
mEvalParseTree.getRootBuffer().render(mEvalParseTree, this);
endContainer();
popElementAttributes();
// Post conditions to ensure that there are no lingering element attributes, containers, or page templates open
if (mElementAttributeManager.hasAttributes()) {
throw new ExInternal("Document processing has finished but not all element attributes have been popped");
}
if (mElementContainerManager.hasContainer()) {
throw new ExInternal("Document processing has finished but not all containers have been ended");
}
if (mPageManager.hasPageTemplate()) {
throw new ExInternal("Document processing has finished but not all page templates have been ended");
}
// Check that any content was actually serialised (document is opened only when content is first serialised)
if (!mDocument.isOpen()) {
throw new ExInternal("The document was not opened because no content was serialised. Ensure your PDF buffer has content.");
}
// Close the document - this completes the first pass output
mDocument.close();
// Apply second pass processing, currently the only post processing required is headers/footers
PostProcessingManager lPostProcessingManager = new PostProcessingManager();
lPostProcessingManager.addPostProcessingOperation(new HeaderFooterPostProcessingOperation(mPageManager));
lPostProcessingManager.process(pOutputStream, lFirstPassOutput);
}
示例9: createPdf
import com.itextpdf.text.pdf.PdfWriter; //導入方法依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/43610868/how-to-add-dynamic-variable-to-footer-without-calling-document-newpage-in-itex">
* How to add dynamic variable to footer without calling document.newPage() in iText 5
* </a>
* <p>
* generator method of the OP
* </p>
* @see #testDynamicFooterLikeAyZagen()
*/
public static void createPdf(ArrayList<String> htmlStrings, FooterTable footerEvt, String destinationPath)
throws IOException, DocumentException {
Document document = new Document(PageSize.A4);
document.setMargins(68, 85, 75, 85);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destinationPath));
if (footerEvt != null)
writer.setPageEvent(footerEvt);
document.open();
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper
.getCSS(new ByteArrayInputStream(/*readCSS("resources/content.min.css").getBytes()*/ "".getBytes()));
cssResolver.addCss(cssFile);
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register(/*"resources/ARIAL.TTF"*/ "c:/Windows/Fonts/arial.ttf");
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
int i = 0;
for (String htmlfile : htmlStrings) {
i++;
footerEvt.setTitleIndex("" + i);//or FooterTable.setTitleIndex("" + i);
ByteArrayInputStream stream = new ByteArrayInputStream(htmlfile.getBytes("UTF-8"));
p.parse(stream, Charset.forName("UTF-8"));
}
document.close();
}