How to manage borders when converting Html to Pdf

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?




var loadOptions = new Aspose.Pdf.HtmlLoadOptions();
var doc = new Aspose.Pdf.Document(sourceDoc.Stream, loadOptions); doc.Save(outputStream, Aspose.Pdf.SaveFormat.Pdf);


Attached is the generated file.

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.


Under the same context, please help me with the below questions

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?

2. How do I add footer to each page in the generated pdf document ?

3. How do I enable page numbers in the generated pdf document ?
Hi there,

Thanks for your inquiry.

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?

Please check following documentation link for changing PDF file information, but I am afraid by design you can not change the Application and PDF Producer name.


K.Rachamalla:
3. How do I enable page numbers in the generated pdf document ?

You can add page number stamp in PDF document by following this documentation link.


Best Regards,

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.


I am adding the header logo to the pdf document using the below code. But the header is always added towards the left of the document.

I am generating the pdf document with the below margins.
MarginInfo { Left = 30, Right = 40, Top = 60, Bottom = 20 }


How can I get the header logo added towards the extreme right of the document.

private HeaderFooter GetHeaderWithLogo(byte[] headerLogo)
{
var logoStream = new MemoryStream(headerLogo);
var header = new HeaderFooter();

var image1 = new Image();
image1.ImageStream = logoStream;
header.Margin.Top = 15;
header.Margin.Bottom = 0;
image1.HorizontalAlignment = HorizontalAlignment.Right;

//Add the image into paragraphs collection of the section
header.Paragraphs.Add(image1);
return header;
}

Hi there,


Thanks for your inquriy. I have tested the scenario with Aspose.Pdf for .NET 11.2.0 and unable to notice the issue. If you are using some old version then please download and try latest version of Aspose.Pdf for .NET, it will resolve the issue. However if the issue persist then please share your sample document and image here, we will look into it and guide you accordingly.

Best Regards,

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,


Thanks for your feedback. I have noticed the issue in 10.2.0 but it has been fixed in later version.

Please note It is recommended to use latest version of Aspose.Pdf, as we maintain a single code base of Aspose APIs. All the fixes and improvements are made in latest version of the API, so we can not provide a hotfix in old version.

We are sorry for the inconvenience caused.

Best Regards,