We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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 PageInfo property of HtmlLoadOptions object to set page Margin of resultant PDF document. Please check following code snippet for the purpose, it will help you to accomplish the task.

var loadOptions = new
Aspose.Pdf.HtmlLoadOptions();<o:p></o:p>

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 footer to each page in the generated pdf document ?


You can add header/footer in a PDF document as following, 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);

Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();

Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter();

FileStream fs = new FileStream(myDir + "logo.png", FileMode.Open, FileAccess.Read);

Aspose.Pdf.Image image1 = new Aspose.Pdf.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

Aspose.Pdf.Text.TextFragment fTxt = new Aspose.Pdf.Text.TextFragment("$p / $P ");

fTxt.TextState.Font = FontRepository.FindFont("Arial");

fTxt.TextState.FontSize = 16;

fTxt.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;

footer.Paragraphs.Add(fTxt);

foreach (Aspose.Pdf.Page page in doc.Pages)

{

page.Header = header;

page.Footer = footer;

}

// Save PDF file

doc.Save("htmltopdf.pdf");


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,