I am converting a html file to pdf using Aspose.Pdf. The generated Pdf document has wide borders and I want to trim them. How do I do this?
Hi there,
Thanks for your inquiry. You can use the PageInfo
property of the HtmlLoadOptions
object to set the page margin of the resulting PDF document. Please check the following code snippet for the purpose; it will help you to accomplish the task.
var loadOptions = new Aspose.Pdf.HtmlLoadOptions();
loadOptions.PageInfo.Margin.Left = 10;
loadOptions.PageInfo.Margin.Right = 10;
var doc = new Aspose.Pdf.Document(sourceDoc.Stream, loadOptions);
doc.Save(outputStream, Aspose.Pdf.SaveFormat.Pdf);
Please feel free to contact us for any further assistance.
Best Regards,
Thank you. It works.
K.Rachamalla:
1. I want to change the document properties like title, author , subject, application and pdf producer properties of the generated pdf document. See the attached image. How do I change this?
K.Rachamalla:
3. How do I enable page numbers in the generated pdf document ?
Hi there,
K.Rachamalla:
2. How do I add a footer to each page in the generated PDF document?
You can add a header/footer in a PDF document as follows. Hopefully, it will help you to accomplish the task.
HtmlLoadOptions options = new HtmlLoadOptions()
options.PageInfo.Margin = new Aspose.Pdf.MarginInfo
{
Left = 40,
Right = 40,
Top = 30,
Bottom = 20
};
// Instantiate Document object
Document doc = new Document("test.html", options);
MemoryStream ms = new MemoryStream();
doc.Save(ms);
doc = new Document(ms);
HeaderFooter header = new HeaderFooter();
HeaderFooter footer = new HeaderFooter();
FileStream fs = new FileStream(myDir + "logo.png", FileMode.Open, FileAccess.Read);
Image image1 = new Image
image1.FixWidth = 50;
image1.FixHeight = 50;
//Add the image into paragraphs collection of the section
header.Paragraphs.Add(image1);
//Set the ImageStream to a MemoryStream object
image1.ImageStream = fs;
//Add footer text
TextFragment fTxt = new TextFragment("$p / $P");
fTxt.TextState.Font = FontRepository.FindFont("Arial");
fTxt.TextState.FontSize = 16;
fTxt.HorizontalAlignment = HorizontalAlignment.Right;
footer.Paragraphs.Add(fTxt);
foreach (Page page in doc.Pages)
{
page.Header = header;
page.Footer = footer;
}
// Save PDF file
doc.Save("htmltopdf.pdf");
These code snippet outlines the entire process include how to add margin for pdf document, instantiate document, save image into memoryStream, incept image into paragraphs section, add image and text fragments into header and footer, and finally how to save document.
Please feel free to contact us for any further assistance.
Best Regards,
Thank you for your response. All of your suggestions worked like a charm expected for the below issue.
Hi there,
I am using 10.2 version. And, I cannot upgrade it due to licencing limitations. Can you please confirm whether it is an issue with this version ?
Hi there,