Floating box problem

Hi,


I want to insert some text in page margin. I tried the following code but it is giving some invalid results.

private static void addText(Page page) {
MarginInfo info = new MarginInfo( 20, 20, 20, 20 );
page.getPageInfo().setMargin( info );
FloatingBox box = new FloatingBox( 100, 10 );
box.setTop( 0 );
box.setLeft( 200 );
box.getParagraphs().add( new TextFragment( “Page name” ) );
box.setBackgroundColor( Color.getLightBlue() );
page.getParagraphs().add(box);
TextFragment frg = new TextFragment( “Rest of the contents of the PDF” );
page.getParagraphs().add(frg);
}

I want to append the page name at the center of to page margin.
Even though I set the position as (0,200) it is writing the floating box after the margin.
It is like the page origin(0,0) start at top left corner excluding the margin dimensions.

Now I want to write it in the margin how do I do so ?

The rest of the page contents also start after the floating box height.
How do i avoid that ?


Hi Saurabh,


Thanks for using our API’s.

I have tested the scenario and I am unable to understand the problem. For your reference, I have attached the output generated over my end, so can you please share some details regarding the problem you are facing, so that we can further look into this matter. We are sorry for this inconvenience.

[Java]

com.aspose.pdf.Document doc = new
com.aspose.pdf.Document();<o:p></o:p>

for(int counter =1; counter<10;counter++)

{

doc.getPages().add();

MarginInfo info = new MarginInfo( 20, 20, 20, 20 );

doc.getPages().get_Item(counter).getPageInfo().setMargin( info );

com.aspose.pdf.FloatingBox box = new com.aspose.pdf.FloatingBox( 100, 10 );

box.setTop( 0 );

box.setLeft( 200 );

box.getParagraphs().add( new TextFragment( "Page name" ) );

box.setBackgroundColor( com.aspose.pdf.Color.getLightBlue() );

doc.getPages().get_Item(1).getParagraphs().add(box);

TextFragment frg = new TextFragment( "Rest of the contents of the PDF" );

doc.getPages().get_Item(counter).getParagraphs().add(frg);

}

doc.save("c:/pdftest/FloatingBox.pdf");

Hi,


I have attached a Image what exactly what I am trying to achieve.

I want the page name inside the margin.

Also I want to write the text while I am generating the PDF and not stamp it later OR open the newly created PDF and then write it.

Hi Saurabh,


Thanks for sharing the details.

It appears that you need to have Header and contents in body part of document. In order to accomplish this requirement, please try using following code snippet. For your reference, I have also attached the output generated over my end.

[Java]

com.aspose.pdf.Document doc = new
com.aspose.pdf.Document();<o:p></o:p>

for(int counter =1; counter<10;counter++)

{

doc.getPages().add();

MarginInfo info = new MarginInfo( 20, 20, 20, 20 );

doc.getPages().get_Item(counter).getPageInfo().setMargin( info );

com.aspose.pdf.HeaderFooter header = new com.aspose.pdf.HeaderFooter();

header.getParagraphs().add( new TextFragment( "Page name" ) );

header.getParagraphs().get_Item(0).setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);

//com.aspose.pdf.FloatingBox box = new com.aspose.pdf.FloatingBox( 100, 10 );

//box.setTop( 0 );

// box.setLeft( 200 );

// box.getParagraphs().add( new TextFragment( "Page name" ) );

// box.setBackgroundColor( com.aspose.pdf.Color.getLightBlue() );

doc.getPages().get_Item(1).setHeader(header);

TextFragment frg = new TextFragment( "Rest of the contents of the PDF" );

doc.getPages().get_Item(counter).getParagraphs().add(frg);

}

doc.save("c:/pdftest/Header_BaseParagraph.pdf");

Hi,


Thanks for the solution, but there is a problem now.

I want to insert the name only in the first page.

How can I do this ?

Is there a way using which I can execute the code only for the first page ?

Hi Saurabh,


In above shared code snippet, the page title is only rendered on first page. In case you have different requirement, please share the details so that we can further look into this requirement.