We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add/Create Header/Footer in a PDF file using Aspose.PDF for Java

Hi,

I am looking for a pdf unique header solution by using aspose PDF. May I ask some questions:

  1. Is there a way to add an existing header PDF to every page of another PDF, to make the second PDF having a unique PDF header?
  2. If I cannot do the option 1, I think I can manually create a header. However I have no clue of how to calculate element positions to create the header.

Eg. To create a Rectangle as header, How do I get the positions for the header:
Rectangle rectangle = new Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);

Thanks,
J

@Jing8h

Aspose.PDF also offers a way to add PDF Page Stamp in another PDF file. You need to only specify the X and Y indent for the stamp. Please try it in your environment and in case you are unable to achieve your desired output, please share your sample PDF files with us along with an expected output. We will test the scenario in our environment and address it accordingly.

Please also check the “Manage Header Footer” article in API documentation if that can help.

Hi @asad.ali
Thanks. That looks like a plan.

I have another question
I want to draw a rectangle on a PDF. I want to draw a width 800 hight 10 rectangle on a PDG. But by doing following code, I got a width 10, hight 800 one. :rofl:
Any reason for this?
Also I try to set the position for this rectangle, using the exact same position as an existing TextParagraph. But they will show as difference position. I am really confused. Any one know why?

    private void drawTitleLine(Page page, CustomPosition customPosition) {

    Graph graph = new Graph((float) page.getPageInfo().getWidth(), (float) page.getPageInfo().getHeight());
    graph.setChangePosition(false);
    page.getParagraphs().add(graph);
    com.aspose.pdf.drawing.Rectangle rect = new com.aspose.pdf.drawing.Rectangle(customPosition.getTitleLineX(), customPosition.getTitleLineY(), 800, 10);
    rect.getGraphInfo().setFillColor(Color.getRed());
    rect.getGraphInfo().setColor(Color.getRed());


    graph.getShapes().add(rect);

}

@Jing8h

Could you please share your sample source PDF document along with another document which is your expected output PDF. We will test the scenario in our environment and address it accordingly.

Hi,

I realise one of the reason is if landscape is true, the system still use non-landscape way to calculate positions.
Another reason of width became height is still unknown.
As I can see the position calculation could be affected by the pdf’s settings and pageinfo.
I think it would be good if aspose can introduce absolute position, which is not affected by pdf page info itself and start from left top as 0 position.

public class AsposeTest {

public static void main(String[] args) {
    // Create Document instance
    Document doc = new Document();
    // Add page to pages collection of PDF file
    Page page = doc.getPages().add();

    page.getPageInfo().setLandscape(true);


    // Create Graph instance
    Graph graph = new Graph((float) page.getPageInfo().getWidth(), (float) page.getPageInfo().getHeight());

    graph.setChangePosition(false);

    // Add graph object to paragraphs collection of page instance
    page.getParagraphs().add(graph);
    // Create Rectangle instance
    Rectangle rect = new Rectangle(0, 10, 200, 120);
    // Specify fill color for Graph object
    rect.getGraphInfo().setFillColor(Color.getRed());
    // Add rectangle object to shapes collection of Graph object
    graph.getShapes().add(rect);
    // save resultant PDF file
    doc.save("Filled_Rect.pdf");
}

}

The thing very strange to me is by using this code, the width is actually 10, height is 800
com.aspose.pdf.drawing.Rectangle rect = new com.aspose.pdf.drawing.Rectangle(customPosition.getTitleLineX(), customPosition.getTitleLineY(), 800, 10);

1.png (36.0 KB)

If the code is like this,
com.aspose.pdf.drawing.Rectangle rect = new com.aspose.pdf.drawing.Rectangle(customPosition.getTitleLineX(), customPosition.getTitleLineY(), 10, 800);
The width is 800 and height is 10. It’s totally reverse of what it suppose to be.
2.png (20.3 KB)

If I create a blank pdf like above code, it will be fine. This is only happening on adding rectangle on existing pdf.
Does anyone know why?

@Jing8h

Please note that the API mimics the behavior of Adobe Reader and follows Adobe Reader specifications according to which, PDF coordinating system starts from bottom-left.

It is possible that your existing PDF has a rotation set for its pages. However, we need to investigate it in our environment and as requested earlier, would you please share your sample PDF document with us so that we cant test it and share our feedback with you accordingly.