3

I need to be able to add this code ... I want to modify to this code to add/edit/remove headers and footers from the PDF.

public void onEndPage(PdfWriter writer, Document document)
{
    PdfContentByte cb = writer.getDirectContent();
    if (document.getPageNumber() > 1)
    {
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, header, (document.right() - document
                                   .left())
                                   / 2 + document.leftMargin(), document.top() + 10, 0);
    }
    footer.writeSelectedRows(0, -1, (document.right() - document.left() - 300) / 2
                             + document.leftMargin(), document.bottom() - 10, cb);
}

Any suggestion please?

iText Pdf Header Removal for particular page

This link shows how to use HeaderFooter Class. Please share Itext2.X.jar file

Community
  • 1
  • 1
Pavan
  • 1,219
  • 13
  • 15
  • iText won't help you edit or remove headers and footers (and content in general) from an existing PDF file. It'll only be useful to add content to an existing PDF file, or to create a PDF file from scratch. – Alexis Pigeon Nov 27 '12 at 09:25
  • The code you quote is for automatic addition of headers and footers during document creation. You on the other hand seem to want to remove headers and footers from a pre-existing document, don't you? Or do I misinterpret your question? – mkl Nov 27 '12 at 09:45
  • Additionally please be more specific, "add/edit/remove headers and footers" is a very broad use case. Text in PDFs generally does not have a tag saying "I am a footer", so your code somehow has to find out or know in advance, thus being dependent on your PDFs. What kind of content is in your headers/footers? Only text? Also images? Form fields or other annotations? Then again does it suffice to cover the existing header/footer with something or do they really have to be removed? Have the headers and footers of your PDFs been added in a similar manner? Then maybe they can be found and removed. – mkl Nov 27 '12 at 10:00
  • Actually I wanted to remove the header and footer from PDF.. – Pavan Nov 27 '12 at 11:10
  • Per Alexis,Itext will only be useful to add content to an existing PDF file, or to create a PDF file from scratch. Then is there any jar or library to work on header and footer in PDF.. Please suggest – Pavan Nov 27 '12 at 11:12
  • @Pavan if you want to "remove the header and footer from PDF", please add the required information, see the prior comments. – mkl Nov 27 '12 at 11:39
  • Presently I have PDF document. My content is pure Text, I wanted to remove the text (inside footer)..Some how I have able to read the PDF remove the footer. – Pavan Nov 27 '12 at 11:51
  • This may not be possible, unless: 1. is your PDF tagged? 2. how were the header and footer added? – Bruno Lowagie Nov 27 '12 at 12:14
  • @Pavan As mentioned above, there is nothing in generic PDFs that labels the glyphs in the header as header glyphs. Unless you can somehow supply information on how to recognize the header, you're out of luck. A good start would be to supply a representative sample PDF. – mkl Nov 27 '12 at 13:45
  • Now I am getting some idea .. You meant that, try to extract the data based on x,y co-ordinate and define youe outline to strip the part of the text. Correct? – Pavan Nov 27 '12 at 15:57
  • @Pavan Well, actually I meant: supply a representative sample PDF so we can tell you whether it is plausible that the footer can be recognized and easily removed. – mkl Nov 27 '12 at 16:34
  • See I want to remove the text from lower position , Now I think we handle this using Co-ordintes (by reducing the size of the page).. So please send the sample code to remove the text in lower part of the page... – Pavan Nov 27 '12 at 16:39
  • Sorry to give the PDF file ... Please share your thoughts :) – Pavan Nov 27 '12 at 16:44
  • 'PdfContentByte cb = writer.getDirectContent(); cb.addTemplate(page, 0, -50);' This Code help to move the rectangle by reducing the y-co-ordinate thus removing lower portion(footer) of the PDF – Pavan Nov 27 '12 at 17:03
  • @BrunoLowagie: I am using Itext5.3.4 lib to work on PDF. Can u please help me to how to in following things:- 1. Search and Replace a String in PDF 2. Using Itext, can we convert PDF to DOC? 3. Can I delete a page and get the result another PDF. Thanks in Advance – Pavan Nov 28 '12 at 05:27
  • Go to http://manning.com/lowagie2 and download sample chapter 6. Read the intro to understand why you question 1 makes an assumption that is wrong. Question 2: you can extract text from a PDF and XML from a tagged PDF using iText, but we don't do OCR or conversion to Word. Question 3: yes, that's explained in sample chapter 6. – Bruno Lowagie Nov 28 '12 at 07:43
  • @Pavan Concerning your page-size-reducing solution: Be aware that the footer is still present in the PDF, it merely is not visible anymore. That being said, you might want to look at [iText - how to move down the current contents in a pdf](http://stackoverflow.com/questions/12798583/itext-how-to-move-down-the-current-contents-in-a-pdf/12813721#12813721) --- the solution used there to change the visible page area is preferable to adding pages as templates because interactive features are not removed. And do have a look at the download Bruno referred to. – mkl Nov 28 '12 at 07:55
  • @BrunoLowagie, Please provide the code snippet to read the PDF and search for the String then replace with newString. Presently I dont have much time to do this.method logic is more than enough , Highly appreciated. Thanks, Pavan – Pavan Nov 29 '12 at 07:25
  • @mkl:Please provide the code snippet to read the PDF and search for the String then replace with newString. Presently I dont have much time to do this.method logic is more than enough , Highly appreciated. – Pavan Nov 29 '12 at 15:30
  • @Pavan If you read the comments before, you would know that the task requires a solution that is specific to the PDF in question. Thus, there is no code snippet at hand. If you provide the PDF (or representative PDFs if multiple documents are to be changed) we may be able to give you a hint how you can proceed. – mkl Nov 29 '12 at 15:42
  • @mkl how to share the pdf in stackoverflow.com? – Pavan Nov 29 '12 at 18:17
  • @Pavan As far as I know SO does not supply a mechanism for sharing generic files, and PDFs uploaded as image are automatically converted to PNG files. But there are so many file sharing sites around. But please don't use one which requires registration to download the files, and furthermore the download link should not be totally hidden beneath ads. – mkl Nov 30 '12 at 10:12
  • I have a text say "Pavan" at some x and y co-ordinates in a page. Now I want to get the exact x and y co-ordinates of the String text="Pavan"; which is in the page. Please suggest ASAP. – Pavan Dec 01 '12 at 19:52
  • @Pavan I would suggest you take a look at [iText in Action — 2nd Edition](http://itextpdf.com/book/index.php), especially section 15.3 *Parsing PDF*; you find the samples from that chapter [here](http://itextpdf.com/book/chapter.php?id=15). Then look at the SO question [Get the exact Stringposition in PDF](http://stackoverflow.com/questions/13632541/get-the-exact-stringposition-in-pdf). – mkl Dec 01 '12 at 22:16
  • @pavan Did you solved the removing header and footer from existing pdf file ? – amar Jan 04 '15 at 15:56

0 Answers0