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

Free Support Forum - aspose.com

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: https://reference.aspose.com/java/pdf/com.aspose.pdf.drawing/line


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:


// 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);

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.


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:

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

Yes, we are sorry for the inconvenience caused.

Best Regards,
Imran Rafique