0

I am creating a PDF file, but have stumbled upon a problem. Below are the iText commands that are causing it.

document.Header = header;
document.Open();
PdfContentByte cb = writer.DirectContentUnder; (getDirectContentUnder() for java)

document is of type Document; writer is a PdfWriter and header is of type HeaderFooter.

iText have same conditions for order of those commands and those are:

  • PdfWriter cannot be used before opening the document
  • header of the PDF will not be shown at all, if I open the document before I add the header to it

That would be fine and I would write it in the order just like in the snippet above. But I need the PdfWriter for creating the header. And now I am in a loop:

  • to create the header, I need to use PdfWriter
  • to use PdfWriter, I need to open the document first
  • for the header to be visible, I would need to create it before opening the document

Also, I found sources both for recommending the header to be created as the last thing before opening the document, as well as for recommending the header to be created after the document has been opened. Which is right and how to solve my problem?

Update: Code

BaseFont bf16 = BaseFont.CreateFont( "c:\\windows\\fonts\\Arial.ttf", BaseFont.CP1250, false );
Font fnt10Bold = new Font( bf16, 10, Font.BOLD, new Color( 100, 100, 100 ) );
System.Drawing.Bitmap headerBitmap = Properties.Resources.doc_header;
Image headerImg = Image.GetInstance( headerBitmap, Color.BLACK );

Document document = new Document( PageSize.A4, 36, 40, 20, 20 );
PdfWriter writer = PdfWriter.GetInstance( document, new FileStream( path, FileMode.Create ) );

for ( int i = 0; i < DocList.Count; i++ )
{
    PdfPTable tblHeader = new PdfPTable( new float[] { 40, 60 } );
    tblHeader.WidthPercentage = 100;

    PdfContentByte cb = writer.DirectContentUnder;
    Image img = getWatermarkedImage( cb, headerImg, "Watermark", font10Bold );

    PdfPCell cellHeader = new PdfPCell( img, true ) { Colspan = 2
      HorizontalAlignment = Element.ALIGN_LEFT,
      VerticalAlignment = Element.ALIGN_BOTTOM };
    tblHeader.AddCell( cellHeader );

    Phrase headerPhrase = new Phrase();
    headerPhrase.Add( tblHeader );

    document.Header = new HeaderFooter( headerPhrase, false );
    if ( i == 0 )
        document.Open();
    else
        document.NewPage();
}

document.Close();

getWatermarkedImage is the method from this answer by Bruno Lowagie.

Adder
  • 165
  • 1
  • 14
  • Java or C# ? I don't think your question is about both of them... – F0XS Nov 15 '17 at 14:48
  • I use it on c#, but I wrote the syntax for java as well, since the solution should be the same for both, `iTextSharp` is just port of the `iText` from java. – Adder Nov 15 '17 at 14:51
  • 2
    And why the down vote, if I may ask? – Adder Nov 15 '17 at 14:54
  • There is no `HeaderFooter` class in any of the official versions of iText. Maybe you are using a third party class with that name; if so, show us that class. Maybe you're not using an official version of iText; in that case, I fear that the author of that unofficial version won't bother answering. In any case: I don't see a reason why you'd need an instance of `PdfWriter` to create a header, hence I can't help you. You should provide more information. – Bruno Lowagie Nov 15 '17 at 15:02
  • I use it for creating watermarked image, followed by your answer on [this](https://stackoverflow.com/questions/26814958) question. And I use [this](https://www.nuget.org/packages/iTextSharp-LGPL/4.1.6) version of `iTextSharp`. – Adder Nov 15 '17 at 15:08
  • Can you show some of your code, maybe there is a way to resolve the issue differently. – mkl Nov 15 '17 at 16:00
  • I updated the answer with some code. – Adder Nov 17 '17 at 06:56
  • I don't find a `Header` property in `Document`. You appear not to use a current iText 5 version (i.e. a 5.5.x) (nor do you use iText 7). One nowadays usually adds headers and footers with iText 5 by means of an instance of a `IPdfPageEvent` implementation (usually via the `PdfPageEventHelper` adapter) assigned to the `PageEvent` property of `PdfWriter`. The advantage in your case would be that the callback methods in that interface allow you to use the `PdfWriter` just-in-time, e.g. when the `OnEndPage` event is triggered. Thus, your hen-egg issue does not occur anymore. – mkl Nov 17 '17 at 08:44
  • Yes, I use 4.1.6 version, since that is the last version that can be used commercially, without open-sourcing your project. – Adder Nov 17 '17 at 09:17
  • Maybe there is a way of creating `PdfTemplate` object without the `PdfWriter`? – Adder Nov 17 '17 at 09:18
  • *"4.1.6 version, since that is the last version that can be used commercially, without open-sourcing your project"* - that is not entirely true, 5.x can be used in closed source projects if you buy a commercial license; only if you don't want to pay for the PDF expertise in that library, you are restricted by the AGPL. – mkl Nov 17 '17 at 09:23
  • That been said, as far as I remember 4.1.6 already supports page events. Thus, the solution for your issue is to use page events instead of those `HeaderFooter` classes. – mkl Nov 17 '17 at 09:24

0 Answers0