Free Support Forum - aspose.com

Add graph to non empty pdf

Hello,

I have problems to add a graph to a non empty pdf file.

I’ve started from the code that instantiates a Pdf object with the empty constructor, or with the FileStream of a file that is not previosly existing, and it works well:

 //Create a file stream to create the PDF document
FileStream fs = new FileStream(“e:/temp/SingleSeg-d.pdf”,FileMode.Create);


//Instantiate the Pdf instance and pass the file stream object to its constructor
Pdf pdf = new Pdf(fs);


//Add a section to the PDF document
Section sec1 = pdf.Sections.Add();


//Add 1000 text paragraphs to the section
for(int i = 0; i < 1000; i++)
{
Text t = new Text("hello world hello world hello " + i.ToString());
sec1.AddParagraph(t);
}


//Close the Pdf. This method is used only for direct file mode
pdf.Close();





Then, I modified it so the Pdf object is instantiated from the opened filestream
of an existing pdf file. If I do that, I get an exception when the Pdf object
either invokes close() or saves(outputFileStream). Next is the code:

string inFile = “…\Example_Raw_Multipage_PDF\MOBIL-01-031-20070904.pdf”;
string outFile = “WithRectangle_1.pdf”;

FileStream inStream = new FileStream(inFile, FileMode.Open);
FileStream outputStream = new FileStream(outFile, FileMode.Create);

// —> INSTANTIATE FROM FILESTREAM OF EXISTING PDF FILE
Pdf pdf1 = new Pdf(inStream);


Section sec1 = pdf1.Sections.Add();

Graph graph1 = new Graph(sec1,100,100);
sec1.Paragraphs.Add(graph1);

Aspose.Pdf.Rectangle rect1 = new Aspose.Pdf.Rectangle(graph1,50,10,100,50);
rect1.GraphInfo.FillColor = new Color(“Red”);
rect1.GraphInfo.IsFilled = true;


rect1.GraphInfo.FillRule = “winding”; //can be “winding” or “evenodd”

Aspose.Pdf.Rectangle rect2 = new Aspose.Pdf.Rectangle(graph1, 60, 20, 50, 25);

graph1.Shapes.Add(rect1);
graph1.Shapes.Add(rect2);


//—> I GET AN ERROR IF LAST LINE IS ANY OF THESE ONES:

pdf1.Save(outputStream);
//pdf1.Close();


Can I get some help?

Thanks in advance.




Hi,

I am sorry but you can only create new pdf files with Aspose.Pdf. Modification of existing files with Aspose.Pdf is not supported.

For processing existing files you can use Aspose.Pdf.Kit.

Thanks.

Ok,

In that case, is it possible to add graphs to existing pdf files?

It seems like you add graphs with Aspose.Pdf, which you could only use for newly created files. At the same time, if I use Aspose.Pdf.Kit to modify an existing file, I can’t find the way to add graphs on it.

Thanks for the previous clarification and quick response.

Hi,

I am sorry but this feature is not supported. You can however convert your graph to images and add that to the pdf. For more information please consult http://www.aspose.com/documentation/file-format-components/aspose.pdf.kit-for-.net-and-java/adhere-image.html

Thanks.