How to insert a horizontal line in pdf (java)

Hi, I would like to know how to add a horizontal line across an existing pdf.

I started with this example in the docs: https://docs.aspose.com/display/pdfjava/Add+Line+Object+to+a+PDF+file

However the resulting pdf does not display the expected shape. It looks like an aspose bug?

Do you have an example of how to add a horizontal line in an existing pdf? I plan on using this line to separate a pseudo-header section of the doc.

I was looking for api documentation on how to use com.aspose.pdf.drawing.Line class but there is really not much documentation in the public api docs: Line | Aspose.PDF for Java API Reference

Thanks

@andrhahn1,
Please try the following code and let us know how that goes into your environment. If this does not help, then send us your source PDF file. We will investigate and share our findings with you:

[Java]

// load an existing PDF
Document doc = new Document("C:\\Pdf\\test158\\EmptyPdf.pdf");
// get page by index
com.aspose.pdf.Page page = doc.getPages().get_Item(1);
// initialize a text fragment 
TextFragment text = new TextFragment("test text with line");
com.aspose.pdf.drawing.Graph graph1 = new
com.aspose.pdf.drawing.Graph((float)page.getRect().getWidth(), 2);
// Add the line to paraphraphs collection of section object
//specify the coordinates for the line
float[] posArr = new float[] { 0, 2, (float)page.getRect().getWidth(), 2 };
com.aspose.pdf.drawing.Line line = new	com.aspose.pdf.drawing.Line(posArr);
line.getGraphInfo().setColor(com.aspose.pdf.Color.getRed());
graph1.getShapes().add(line);
page.getParagraphs().add(text);
page.getParagraphs().add(graph1);
doc.save("C:\\Pdf\\test158\\LineAdded.pdf");

Best Regards,
Imran Rafique

I ran the example but its not exactly what I am looking for and I cant figure out how to adjus the margins, etc.

I have attached the result I am seeing when running the code example.LineAdded.pdf (80.2 KB)

Is that how your resulting doc looks?

Can you tell me what the description are for the method args for the position array in the Line class?

The documentation is so scarce on the topic of drawing lines that its very difficult to figure out how this is supposed to work.

Thanks

@andrhahn1,
Well, the Line class constructor takes a position array like:
float[] posArr = new float[] { leftposition, bottomposition, width, height };

Furthermore, you can set page margins as below:
[Java]

// get page by index
com.aspose.pdf.Page page = doc.getPages().get_Item(1);
page.getPageInfo().getMargin().setLeft(0);

Yes, we are sorry for the inconvenience caused.

Best Regards,
Imran Rafique