Changing the header font in PDF document

Hi


I have a PDF document in which I want to set the font of the text in header to Arial. I could not find any relevant APIs to set the font of the header text of the PDF.

Kindly help.

Regards,
Prabhaker Kr

Hi Prabhaker,


Thanks for your inquiry. I am afraid there is no direct option to change Header text font. However you may try to search text in specific page region and change the font as following. Hopefully it will help you to accomplish the task.

//Load existing PDF Document<o:p></o:p>

Document pdf = new Document("input.pdf");

//Search text fragments and set edit option as remove unused fonts

TextFragmentAbsorber absorber = new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));

absorber.getTextSearchOptions().setLimitToPageBounds(true);

absorber.getTextSearchOptions().setRectangle(new com.aspose.pdf.Rectangle(100, 200, 250, 350));

//accept the absorber for all the pages

pdf.getPages().accept(absorber);

//traverse through all the TextFragments

TextFragmentCollection textFragmentCollection = absorber.getTextFragments();

for(Iterator iterator = textFragmentCollection.iterator(); iterator.hasNext();)

{

TextFragment textFragment = iterator.next();

String fontName = textFragment.getTextState().getFont().getFontName();

// if the font name is ArialMT, replace font name with Arial

if (fontName.equals("ArialMT"))

{

textFragment.getTextState().setFont(FontRepository.findFont("Arial"));

}

}

//Save the updated document

pdf.save("output.pdf");


Please feel free to contact us for any further assistance.


Best Regards,

Hi Tilal,


Thank you for the solution. I tried the solution. It is working but it is setting the font for all the text in the PDF document. Is there any API to set the font only for the Header ? Or is there any API to get the Header object of the PDF document ?

Thanks & Regards,
Prabhaker Kr.

Hi Prabhaker,


Thanks for your feedback. Please share your sample code and source document, so we will look into it and guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

Hi Tilal,


I have attached the document with the requirement detail. About the code that I tried is the same code that you provided in the previous conversation. Except that, I tried using the following code too:


com.aspose.pdf.TextStamp textStamp = new com.aspose.pdf.TextStamp(“Sample Stamp”);
textStamp.getTextState().setFont(new com.aspose.pdf.FontRepository().findFont(“Arial”));

for (int Page_counter =1; Page_counter<=pdfDocument.getPages().size();Page_counter++)
{
pdfDocument.getPages().get_Item(Page_counter).addStamp(textStamp);
}

But no luck. Can you suggest some other APIs to achieve the requirement.


Thanks & Regards,
Prabhaker Kr

Hi Prabhaker,


Thanks for your inquiry. As suggested above you can get text of specific location using TextFragmentAbsorber and change the font of TextFragment. Please check following code and sample input/output PDF document.

//Load existing PDF Document<o:p></o:p>

Document pdf = new Document(myDir+"HeaderFooterTest1.pdf");

//Search text fragments and set edit option as remove unused fonts

TextFragmentAbsorber absorber = new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));

absorber.getTextSearchOptions().setLimitToPageBounds(true);

absorber.getTextSearchOptions().setRectangle(new com.aspose.pdf.Rectangle(72*3, 72*10.69, 72*5, 72*10.69+20));

//accept the absorber for all the pages

pdf.getPages().accept(absorber);

//traverse through all the TextFragments

TextFragmentCollection textFragmentCollection = absorber.getTextFragments();

for(Iterator iterator = textFragmentCollection.iterator(); iterator.hasNext();)

{

TextFragment textFragment = iterator.next();

// String fontName = textFragment.getTextState().getFont().getFontName();

// if the font name is ArialMT, replace font name with Arial

// if (fontName.equals("ArialMT"))

{

textFragment.getTextState().setFont(FontRepository.findFont("Calibri"));

textFragment.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);

}

}

//Save the updated document

pdf.save(myDir+"output_fontchange.pdf");


Please feel free to contact us for any further assistance.


Best Regards,



Hi Tilal,


Thank you for the reply. I am afraid that the code snippet given here sets the font of all the content of the PDF document. But I have to change the font for the header only of the PDF document.

Is there any way to set the font of the header specifically ?


Thanks & Regards,
Prabhaker Kr.

Hi Prabhaker,


As requested above, please share your PDF input/output document along with the code used here? So we will check and provide you information accordingly. You may check my shared PDF documents, Font is only changed in header.

We are sorry for the inconvenience caused.

Best Regards,

Hi Prabhaker,


In addition to above reply. If you know the header text, then you can also search text and set its font. Please check following documentation link for details.


Please feel free to contact us for any further assistance.

Best Regards,