Hi,
I’m trying to add text to the top of the page but the text always appears in the middle of the screen.
I’ve attached a blank PDF as a sample, I’m using the following for positioning:
tf.Position = new Position((doc.Pages[i].Rect.Width - tf.Rectangle.Width) - 10, doc.Pages[i].Rect.Height - 15);
page.pdf (1.4 KB)
@tonywalakone
Please use following code snippet to set position of the text at the top of the page:
tf.Position = new Position(0, page.Rect.Height - 15);
The text appears in the middle of the page because .Height is not returning the correct value.
@camorized
We have again tested the scenario and noticed that Page Height was returning correctly using the API i.e. 842. But, the text is appearing in the center of page when we use an existing PDF document which you have shared. If we use a blank/new document, the same code works fine:
Document doc = new Document();
Page page = doc.Pages.Add();
TextFragment tf = new TextFragment("This is simple text");
page.Paragraphs.Add(tf);
tf.Position = new Position(0, page.PageInfo.Height - 15);
doc.Save(dataDir + "TextatTop_correct.pdf");
Please note that in both cases the height of page is returned correct i.e. 842. However, we have logged an issue as PDFNET-46741 in our issue tracking system to further investigate the scenario. We will surely keep you posted with the status of issue resolution. Please spare us little time.
We are sorry for the inconvenience.
Hi @asad.ali
Thank you for looking into it. Basically I receive pdf files from different types of clients which I need to add text to - I’m looking for a solution and aspose is the way to go, but unfortunately due to this issue I may need to come back once it has been resolved.
Another thought I had is to create a copy of each PDF file and then add text, but the speed will be quite slow I presume.
@camorized
As a workaround, you can go with an alternative approach i.e. TextStamp. Please use following code snippet:
Document doc = new Document(dataDir + "page.pdf");
Page page = doc.Pages[1];
TextStamp stamp = new TextStamp("This is simple text");
stamp.HorizontalAlignment = HorizontalAlignment.Left;
stamp.VerticalAlignment = VerticalAlignment.Top;
page.AddStamp(stamp);
doc.Save(dataDir + "TextatTop_Wrong.pdf");
@asad.ali
That works great, thank you.
Is there a work around for resizing the page? I’m using PdfFileEditor.ContentsResizeParameters but it resizes incorrectly due to the PDF being odd.
@camorized
Please use following code snippet in order to resize PDF Pages:
string inputFile = dataDir + @"page.pdf";
string outFile = dataDir + @"outputExample_out.pdf";
Document doc = new Document(inputFile);
foreach (Page page in doc.Pages)
{
page.SetPageSize(PageSize.PageLetter.Width, PageSize.PageLetter.Height);
}
doc.Save(outFile);
@asad.ali
Thank you for your reply.
The output PDF after running SetPageSize is wrong, which I presume it’s the issue with the PDF. The question is, is it possible to check these weird PDFs?
Thank you.
@camorized
Would you please share which height and width you applied to resize your PDF document. We will test the scenario in our environment and address it accordingly.