wrong page size gdpicture in draw text in pdf?
Posted: 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, ;
//// 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
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, ;
//// 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