wrong page size gdpicture in draw text in pdf?

Discussions about PDF management.
Post Reply
jgutierr
Posts: 1
Joined: Tue Aug 06, 2019 4:32 pm

wrong page size gdpicture in draw text in pdf?

Post by jgutierr » Tue Aug 06, 2019 5:02 pm

Hello,

I have had problems adding text bottom each page of a pdf in some it suits me and others do not, there are also problems when the page is horizontal I can not make it always put the text under each page regardless of the size it has, How can you get the correct page size values?

I attach my code in C# and image with bad and good with draw text, with notes:
public static bool GdPictureStampPdf(string[] files, string textStamp, out string message) {
bool result = false;
message = string.Empty;
try
{
foreach (var file in files)
{

using (GdPicturePDF gdPdf = new GdPicturePDF())
{
if (gdPdf.LoadFromFile(file, true) == GdPictureStatus.OK)
{
int pageCount = gdPdf.GetPageCount();
GdPictureStatus gdStatus = gdPdf.GetStat();
if (gdStatus == GdPictureStatus.OK && pageCount > 0)
{
for (int i = 1; i <= pageCount; i++)
{
//Selecting a page.
if (gdPdf.SelectPage(i) == GdPictureStatus.OK)
{
gdPdf.SetOrigin(PdfOrigin.PdfOriginTopLeft);
gdPdf.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitPoint);
gdStatus = gdPdf.NormalizePage();
if (gdStatus != GdPictureStatus.OK)
{
throw new Exception("Can't stamp text. Error: The NormalizePage() method has failed for the page number " + i.ToString() + " with the status: " + gdStatus.ToString());
}

gdPdf.SetTextSize(8);
string _fontId = gdPdf.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);

int boxMarging = 1;
string stampText = Path.GetFileNameWithoutExtension(file) + " " + textStamp;
float pageWidth = gdPdf.GetPageWidth();
float pageHeigth = gdPdf.GetPageHeight();
float textSizeW = gdPdf.GetTextWidth(_fontId, 8, stampText);
float textSizeH = gdPdf.GetTextHeight(_fontId, 8);

//// I add the next line hardcode for try calculate or put the correct size of the pages
float centerPostition = ((pageWidth - stampText.Length) / 2) - 30;
float bottomPostition = pageHeigth < 700 ? ((pageWidth - pageHeigth) + pageHeigth) - 10 : pageHeigth - 10;

gdPdf.SetFillColor(0, 0, 0); // - Black
gdStatus = gdPdf.DrawText(_fontId, centerPostition, bottomPostition, stampText);
if (gdStatus != GdPictureStatus.OK)
{
throw new Exception("Can't stamp text. Error: " + gdStatus.ToString());
}
}
}

gdStatus = gdPdf.SaveToFile(file, false);
if (gdStatus != GdPictureStatus.OK)
{
throw new Exception("Can't save file. Error: " + gdStatus.ToString());
}
gdPdf.CloseDocument();
result = true;
}
else
{
result = false;
message = string.Format("Error Can't read pages PDF: '{0}' Code: {1}", file, gdStatus);
}
}
}
}
}
catch (Exception ex)
{
result = false;
message = String.Format("Error GdPicture occured for stamp Documents Exception: {0}", ex.Message);
}
return result;
}

I hope help for this. Thanks
Attachments
3_Vertical_Bad.PNG
2_Vertical_Good.PNG
1_Horizontal.PNG

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: wrong page size gdpicture in draw text in pdf?

Post by Gabriela » Wed Aug 07, 2019 10:14 am

Hello,

I would say this computation should help you:
float centerPostition = pageWidth / 2 - textSizeW / 2;
float bottomPostition = pageHeigth - textSizeH;
If this is not what you are asking for, please send us a source document and specify in more details what you are trying to achieve. It is not quite clear what is the expected result. It always depends on the source document you have.
At the same please be sure you are using the latest published release so we are on the same version.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest