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

Free Support Forum - aspose.com

Cannot instantiate Artifact class

Hi

I’ve got a problem related to saving a pdf file. I have the below function to which I pass a table and two strings. I open a template pdf file and insert the table to the file and subsequently I attempt to save the file but it fails with the error: Cannot instantiate Artifact class.

I have number of other places where is use pretty much same code so I am a bit confused as to the reason for the problem.

public static void ReturnPDF(DataTable res, string reviewer, string PM)
{
Aspose.Pdf.License licensepdf = new Aspose.Pdf.License();
licensepdf.SetLicense(“Aspose.Pdf.lic”);
int totalRows = res.Rows.Count;
int totalColumns = res.Columns.Count;
string code = DateTime.Now.ToString(“yyyyMMddHHmmss”);
Document pdfDocument = new Document(root + “\Files\EURALandtemplate.pdf”);

// Get particular page
Aspose.Pdf.Page pdfPage = (Aspose.Pdf.Page)pdfDocument.Pages[1];

pdfPage.PageInfo.Margin = new Aspose.Pdf.MarginInfo();
pdfPage.PageInfo.Margin.Left = 22;
pdfPage.PageInfo.Margin.Right = 22;
pdfPage.PageInfo.Margin.Top = 72;
pdfPage.PageInfo.Margin.Bottom = 72;

Aspose.Pdf.Table table = new Aspose.Pdf.Table();
table.ColumnWidths = “30 40 30 40 50 40 40 40 60 60 40 40 40 40 45 50 55”;
// Set the table border color as LightGray
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
// Set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
table.ImportDataTable(res, true, 0, 0, totalRows, totalColumns);
pdfDocument.Pages[1].Paragraphs.Add(table);

string pdfFilePath = root + “\Files\” + code + “.pdf”;
using (FileStream pdfFileStream = File.Create(pdfFilePath))
{
// Convert the Document to a Pdf file stream
pdfDocument.Save(pdfFileStream, SaveFormat.Pdf);
}

}

Hi Hans,


Thanks for contacting support.

I have tested the scenario using following code snippet with Aspose.Pdf for .NET 17.3.0 and have managed to save the PDF document successfully without any error. I have also attached the PDF file for your reference.

C#

DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add(“First Name”);
dt.Columns.Add(“Last Name”);
dt.Columns.Add(“Company”);
dt.Columns.Add(“Marks”);
DataRow _ravi = dt.NewRow();
_ravi[“First Name”] = “Fahad”;
_ravi[“Last Name”] = “Adeel”;
_ravi[“Company”] = “Aspose”;
_ravi[“Marks”] = “500”;
dt.Rows.Add(_ravi);
ReturnPDF(dt, “”, “”,dataDir);

public static void ReturnPDF(DataTable res, string reviewer, string PM, String root)
{
int totalRows = res.Rows.Count; int totalColumns = res.Columns.Count;
string code = DateTime.Now.ToString("yyyyMMddHHmmss"); Document pdfDocument = new Document();
// Get particular page Aspose.Pdf.Page pdfPage = pdfDocument.Pages.Add(); // (Aspose.Pdf.Page)pdfDocument.Pages[1];
pdfPage.PageInfo.Margin = new Aspose.Pdf.MarginInfo(); pdfPage.PageInfo.Margin.Left = 22; pdfPage.PageInfo.Margin.Right = 22; pdfPage.PageInfo.Margin.Top = 72; pdfPage.PageInfo.Margin.Bottom = 72;
Aspose.Pdf.Table table = new Aspose.Pdf.Table(); table.ColumnWidths = "100 100 50 40"; // Set the table border color as LightGray table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); // Set the border for table cells table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); table.ImportDataTable(res, true, 0, 0, totalRows, totalColumns); pdfDocument.Pages[1].Paragraphs.Add(table);

string pdfFilePath = root + "" + code + ".pdf";
using (FileStream pdfFileStream = File.Create(pdfFilePath)) { // Convert the Document to a Pdf file stream pdfDocument.Save(pdfFileStream, SaveFormat.Pdf); }
}

If you still face issues, please share your environment details, it will help us to replicate the issue in our environment.

We are sorry for the inconvenience.

Best Regards,

Hi again

I have used pdf.generator in stead and was able to format pdf file as i wanted and hence the problem is no longer a problem :slight_smile:

Thanks for your feedback. I am glad you are able to format pdf file as your requirement.


p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000; min-height: 14.0px} span.s1 {font-kerning: none}

Please keep using our API and feel free to contact us for any question or concern, we will be more than happy to extend our support.


Best Regards,