Set border to PDF


#1

I have purchased Aspose PDF family and i want to convert html to pdf,
Am fine with the conversion but the i want to add the border in PDF each page.
How can i do this ?


#2

@yatheendra

Thank you for contacting support.

You may please draw border in each page of generated PDF document with Graph as in following code snippet:

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "Test.pdf");
foreach (Aspose.Pdf.Page page in doc.Pages)
{
    Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
    page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
    page.Paragraphs.Add(graph);
    Aspose.Pdf.Drawing.Line bottomline = new Aspose.Pdf.Drawing.Line(new float[] { 10, 10, (float)page.PageInfo.Width - 10, 10 });
    Aspose.Pdf.Drawing.Line topline = new Aspose.Pdf.Drawing.Line(new float[] { 10, (float)page.PageInfo.Height - 10, (float)page.PageInfo.Width - 10, (float)page.PageInfo.Height - 10 });
    Aspose.Pdf.Drawing.Line rightline = new Aspose.Pdf.Drawing.Line(new float[] { (float)page.PageInfo.Width - 10, 10, (float)page.PageInfo.Width - 10, (float)page.PageInfo.Height - 10 });
    Aspose.Pdf.Drawing.Line leftline = new Aspose.Pdf.Drawing.Line(new float[] { 10, 10, 10, (float)page.PageInfo.Height - 10 });
    graph.Shapes.Add(topline);
    graph.Shapes.Add(bottomline);
    graph.Shapes.Add(rightline);
    graph.Shapes.Add(leftline);
    graph.ZIndex = 0;
}
doc.Save(dataDir + "PageBorder.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.


#3

@Farhan.Raza
AM using java, please send the java code for the conversion.

below is my code snippet:
where should i add border code, pdf will be of single page only.


HtmlLoadOptions htmloptions = new HtmlLoadOptions();
com.aspose.pdf.Document doc = new com.aspose.pdf.Document(“a.html”, htmloptions);

doc.save(“b.pdf”);



#4

@yatheendra

Thank you for elaborating it further.

You would need to add borders to generated PDF document after it is converted from HTML. Below is equivalent Java code which you can modify further as per your requirements.

Document doc = new Document(dataDir + "Test.pdf");
for (Page page : doc.getPages())
{
    com.aspose.pdf.drawing.Graph graph = new com.aspose.pdf.drawing.Graph((float)page.getPageInfo().getWidth(), (float)page.getPageInfo().getHeight());
    page.getPageInfo().setMargin(new MarginInfo(0, 0, 0, 0));
    page.getParagraphs().add(graph);
    com.aspose.pdf.drawing.Line bottomline = new com.aspose.pdf.drawing.Line(new float[] { 10, 10, (float)page.getPageInfo().getWidth() - 10, 10 });
    com.aspose.pdf.drawing.Line topline = new com.aspose.pdf.drawing.Line(new float[] { 10, (float)page.getPageInfo().getHeight() - 10, (float)page.getPageInfo().getWidth() - 10, (float)page.getPageInfo().getHeight() - 10 });
    com.aspose.pdf.drawing.Line rightline = new com.aspose.pdf.drawing.Line(new float[] { (float)page.getPageInfo().getWidth() - 10, 10, (float)page.getPageInfo().getWidth() - 10, (float)page.getPageInfo().getHeight() - 10 });
    com.aspose.pdf.drawing.Line leftline = new com.aspose.pdf.drawing.Line(new float[] { 10, 10, 10, (float)page.getPageInfo().getHeight() - 10 });
    graph.getShapes().add(topline);
    graph.getShapes().add(bottomline);
    graph.getShapes().add(rightline);
    graph.getShapes().add(leftline);
    graph.setZIndex(0);
}
doc.save(dataDir + "PageBorder.pdf");