I’ve tried adding a rectangle to a PDF file using Aspose.Pdf. I’ve tried multiple approaches [using floating box, graph etc.], but the rectanle is never drawn on the document and there are no error messages either.
Here are various code snippets I used, all of them either from the API documentation or support answers from this forum:
Using isoStream As IsolatedStorageFileStream = IsoStore.GetStream(“some path”)
Dim pdf As New Aspose.Pdf.Document(isoStream)
Dim pdfPage As Aspose.Pdf.Page = pdf.Pages(page)
’ with graph
Dim graph As New Aspose.Pdf.Drawing.Graph(CSng(pdfPage.PageInfo.Width), CSng(pdfPage.PageInfo.Height))
pdfPage.Paragraphs.Add(graph)
Dim rectangle As New Aspose.Pdf.Drawing.Rectangle(rect.X, CSng(pdfPage.PageInfo.Height - rect.Y), rect.Width, rect.Height)
rectangle.GraphInfo.FillColor = Aspose.Pdf.Color.Red
graph.Shapes.Add(rectangle)
’ with floating box
Dim box As New Aspose.Pdf.FloatingBox
box.Top = rect.Top
box.Left = rect.Left
pdfPage.Paragraphs.Add(box)
box.Width = rect.Width
box.Height = rect.Height
box.Border = New Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1, Aspose.Pdf.Color.Black)
box.Paragraphs.Add(New TextFragment(“test”))
pdf.Save()
End Using
Hi Sorin,
It happens with all PDF documents I tried. attached is the last example I used.
Hi,
Hi Sorin,
I downloaded the latest version, but the problem still persists. I’m using Visual Studio 2013 on Windows 7 and I’m developing with .NET 4.0.
Hi,
Dim pdf As New Aspose.Pdf.Document(“c:/pdftest/DB+BAHN±+Verbindungen±+Ihre+Auskunft.pdf_stamp…pdf”)<o:p></o:p>
Dim pdfPage As Aspose.Pdf.Page = pdf.Pages(1)
' with graph
Dim graph As New Aspose.Pdf.Drawing.Graph(CSng(pdfPage.PageInfo.Width), CSng(pdfPage.PageInfo.Height))
pdfPage.Paragraphs.Add(graph)
Dim rectangle As New Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 200) 'rect.X, CSng(pdfPage.PageInfo.Height - rect.Y), rect.Width, rect.Height)
rectangle.GraphInfo.FillColor = Aspose.Pdf.Color.Red
graph.Shapes.Add(rectangle)
' with floating box
Dim box As New Aspose.Pdf.FloatingBox
box.Top = 210 'rect.Top
box.Left = 210 'rect.Left
pdfPage.Paragraphs.Add(box)
box.Width = 100 'rect.Width
box.Height = 100 'ect.Height
box.Border = New Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1, Aspose.Pdf.Color.Black)
box.Paragraphs.Add(New Aspose.Pdf.Text.TextFragment("test"))
pdf.Save("c:/pdftest/DB+BAHN+-+Verbindungen+-+Ihre+Auskunft.pdf_stamp_FloatingBox_Added.pdf")
Thanks for your answer.
I tried the code in a test project. Now there is a rectangle drawn, just like in your example PDF. However, the location is wrong. I tried using (0, 0) as coordinates, but there are always margin on the top/left side. This happens regardless of whether I use a Rectangle or FloatingBox, although the size of the margins seems to differ.
I have after some testing now realised the reason of it not working was the save operation. As you can see from my code, I open the file as a stream and then simply call Save() on the PDF file. This does not work. The document with the rectangle drawn on it is only saved if I save it in a new location, given as a path.
This is not the way my program operates, as I save all files in the isolated storage and thus can only access them via file streams.
Hi,AzraelSeraphin:I have after some testing now realised the reason of it not working was the save operation. As you can see from my code, I open the file as a stream and then simply call Save() on the PDF file. This does not work. The document with the rectangle drawn on it is only saved if I save it in a new location, given as a path.This is not the way my program operates, as I save all files in the isolated storage and thus can only access them via file streams.
Thanks.
Do you have any suggestion on how to handle the coordinate issue? I’ve done some testing and it doesn’t seem to be a static value I can simply add/substract, so any documentation on how this works is appreciated.
Hi,AzraelSeraphin:Do you have any suggestion on how to handle the coordinate issue? I've done some testing and it doesn't seem to be a static value I can simply add/substract, so any documentation on how this works is appreciated.
This is the code I use to draw the rectangle:
Dim pdfPage As Aspose.Pdf.Page = pdf.Pages(page)
Dim graph As New Aspose.Pdf.Drawing.Graph(CSng(pdfPage.PageInfo.Width), CSng(pdfPage.PageInfo.Height))
pdfPage.Paragraphs.Add(graph)
Dim rectangle As New Aspose.Pdf.Drawing.Rectangle(rect.X, CSng(pdfPage.PageInfo.Height - rect.Y), rect.Width, rect.Height)
The coordinates and size of rect are determined by mouseclicks on the page.
The
rectangle, however, is never drawn in the locations I clicked. Even
when I click a corner of the page and the coordinates are (0, 0), the
rectangle will be drawn somewhere towards the middle of the page.
In
the attached example file, I have marked roughly the whole page and
passed those coordinates to the function [(0, 6) with a size of
590x825]. As you can clearly see, the rectangle is drawn in a completely
different location. And it doesn’t seem to be simply mirrored or upside down, either.
Hi,AzraelSeraphin:This is the code I use to draw the rectangle:
Dim pdfPage As Aspose.Pdf.Page = pdf.Pages(page)
Dim graph As New Aspose.Pdf.Drawing.Graph(CSng(pdfPage.PageInfo.Width), CSng(pdfPage.PageInfo.Height))
pdfPage.Paragraphs.Add(graph)
Dim rectangle As New Aspose.Pdf.Drawing.Rectangle(rect.X, CSng(pdfPage.PageInfo.Height - rect.Y), rect.Width, rect.Height)
The coordinates and size of rect are determined by mouseclicks on the page.
The rectangle, however, is never drawn in the locations I clicked. Even when I click a corner of the page and the coordinates are (0, 0), the rectangle will be drawn somewhere towards the middle of the page.
In the attached example file, I have marked roughly the whole page and passed those coordinates to the function [(0, 6) with a size of 590x825]. As you can clearly see, the rectangle is drawn in a completely different location. And it doesn't seem to be simply mirrored or upside down, either.