Aspose.Pdf.TextStamp overlaps with the previously added text. How is it possible to prevent this?
Here is the code I am using to prepare a document:
StringBuilder htmlPage = new StringBuilder();
//put here the html from https://pastebin.com/c6Hu4nV5
//instead of the `text foo`
htmlPage.Append("text foo");
byte[] bytes = Encoding.UTF8.GetBytes(htmlPage.ToString());
var streamHtml = new MemoryStream(bytes)
var objLoadOptions = new Aspose.Pdf.HtmlLoadOptions(0, 50, 0, 0);
var doc = new Aspose.Pdf.Document(streamHtml, objLoadOptions);
Here is the code I am using for the page numbers (which are stamps in my context) addition:
foreach (var page in doc.Pages)
{
Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText(idAndVersion + page.Number + "-" + doc.Pages.Count,
Color.Black, Aspose.Pdf.Facades.FontStyle.Helvetica, Aspose.Pdf.Facades.EncodingType.Identity_h,
true, 12F);
var textStamp = new Aspose.Pdf.TextStamp(formattedText)
{
VerticalAlignment = Aspose.Pdf.VerticalAlignment.Bottom,
HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left,
LeftMargin = 50,
BottomMargin = 20
};
page.AddStamp(textStamp);
}
I am using the stamps as a way of adding page numbers. Maybe there is a better way to do it?
I tried to find a better way over here, but with no success.
After I add the page numbers, here is what I do:
using (var streamPdf = new MemoryStream())
{
doc.Save(streamPdf, Aspose.Pdf.SaveFormat.Pdf);
return streamPdf.GetBuffer();
}
You may please set PDF Page Margins by using HtmlLoadOptions.PageInfo.Margin
Property. Following code snippet generates a PDF document that has page margins set and text stamp does not overlap the remaining content of the page.
StringBuilder htmlPage = new StringBuilder();
htmlPage.Append(File.ReadAllText(dataDir + "foo.html"));
byte[] bytes = Encoding.UTF8.GetBytes(htmlPage.ToString());
var streamHtml = new MemoryStream(bytes);
var objLoadOptions = new Aspose.Pdf.HtmlLoadOptions();
// Set Page Margins
objLoadOptions.PageInfo.Margin = new MarginInfo(50, 50, 50, 50);
// You can also set Page Height/Widht
//objLoadOptions.PageInfo.Height = 898;
//objLoadOptions.PageInfo.Widht = 550;
var doc = new Aspose.Pdf.Document(streamHtml, objLoadOptions);
foreach (var page in doc.Pages)
{
Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText(page.Number + "-" + doc.Pages.Count,
new Aspose.Pdf.Facades.FontColor(0, 0, 0) , Aspose.Pdf.Facades.FontStyle.Helvetica, Aspose.Pdf.Facades.EncodingType.Identity_h,
true, 12F);
var textStamp = new Aspose.Pdf.TextStamp(formattedText)
{
VerticalAlignment = Aspose.Pdf.VerticalAlignment.Bottom,
HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left,
LeftMargin = 50,
BottomMargin = 20
};
page.AddStamp(textStamp);
}
doc.Save(dataDir + "out20.1.pdf");
You can also specify Page height/width in above code snippet as per your requirements. The related code lines are commented which can be noticed. In case you still face any issue, please feel free to let us know.