Telerik Forums
Telerik Document Processing Forum
1 answer
389 views

Hello,

When attempting to export a manually created workbook I obtain this error message: 

Value cannot be null.
Parameter name: workbook at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Utilities.XlsxHelper.ConvertColumnPixelWidthToExcelWidth(Workbook workbook, Double columnWidthInPixels)

   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Contexts.XlsxWorksheetExportContext..ctor(XlsxWorkbookExportContext workbookContext, Worksheet worksheet, Int32 sheetNo)
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Contexts.XlsxWorkbookExportContext.InitializeWorksheetContexts()
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Contexts.XlsxWorkbookExportContext.Initialize()
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider.ExportOverride(Workbook workbook, Stream output)
   at [Redacted] Controller.cs:line 203

I Generate the workbook using this method:

public Workbook WriteExcelFile(List<Object> list)
        {

            Workbook workbook = new Workbook();
            using (Worksheet worksheet = workbook.Worksheets.Add())
            {
                CellRange range = new CellRange(1, 1, list.Count, 5);
                CellSelection selection = worksheet.Cells[range];

                if (selection.CanInsertOrRemove(range, ShiftType.Right))
                {
                    selection.Insert(InsertShiftType.Right);
                }

                for (int row = 0; row < list.Count; row++)
                {
                    worksheet.Cells[row, 0].SetValue(list[row].string1);
                    worksheet.Cells[row, 1].SetValue(list[row].string2);
                    worksheet.Cells[row, 2].SetValue(list[row].string3);
                    worksheet.Cells[row, 3].SetValue(list[row].string4);
                    worksheet.Cells[row, 4].SetValue(list[row].string5);
               }
            }

            return workbook;
        }

The attempt to write it to a memory stream (FileStream was also attempted)

using (Stream output = new MemoryStream(10000))
      formatProvider.Export(workbook, output);

 

Could you help me understand the reason behind this error?

Nikolay Demirev
Telerik team
 answered on 18 Apr 2019
17 answers
1.9K+ views

I would like to be able to read a PDF document and import the contents of tables within the PDF document.  Do you have an examples that shows how I would:

  • Open an existing PDF document
  • Read the document line at a time from top to bottom
  • Find tables in the document
  • Read the cell contents of those tables

    ?

 

Tanya
Telerik team
 answered on 16 Apr 2019
3 answers
2.0K+ views

I'm just trying to create a table in PDF with this:

https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/table

In general a nice thing.

1) The only way to set the Font of the (entire) table I found, is to set the Font for each singular block in each singular cell, and this before inserting the text.

That means I cannot just do this: AddTableCell().Blocks.AddBlock().InsertText("cell11");

Is there a simpler way, to have some Default for the entire Table ? I found this DefaultCellProperties thing, but this concerns only background and borders

2) There is this property

table.LayoutType = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.TableLayoutType.FixedWidth;

The automatic works quiet well if you have text in the table, having f.e. an empty line, it shrinks the height of this line.

So I thought I changed to 'fixedwidth' and set it manually.

But than, I haven't found anything to set width oder height of Rows or Cells.

So what is this FixedWidth good for,  how do I set the height of a Row or a ColumnWidth, although the concept of a column is kind of missing in this class.

 

Georgi
Telerik team
 answered on 11 Apr 2019
1 answer
550 views

Fails to open simple excel

 

var assembly = Assembly.GetEntryAssembly();
var resourceStream = assembly.GetManifestResourceStream("Dispatcher.Resources.someexcel.xlsx");
resourceStream.Position = 0;

XlsxFormatProvider formatProvider = new XlsxFormatProvider();
Workbook workbook = formatProvider.Import(resourceStream);

 

I cannot attach excel file here 

 

stack trace

System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Model.Elements.Worksheets.DataValidationElement.CopyPropertiesToSingleArgumentContext(SingleArgumentDataValidationRuleContext context)
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Model.Elements.Worksheets.DataValidationElement.CopyPropertiesToDataValidationRuleContext(DataValidationRuleContextBase context)
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Model.Elements.Worksheets.DataValidationElement.ListRuleFactory(IXlsxWorksheetImportContext context)
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Model.Elements.Worksheets.DataValidationElement.OnAfterRead(IXlsxWorksheetImportContext context)
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Model.Elements.Worksheets.WorksheetElementBase.OnAfterRead(IXlsxWorkbookImportContext context)
   at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Elements.OpenXmlElementBase`3.OnAfterRead(IOpenXmlImportContext context)
   at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Elements.OpenXmlElementBase.Read(IOpenXmlReader reader, IOpenXmlImportContext context)
   at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Elements.OpenXmlElementBase.ReadChildElements(IOpenXmlReader reader, IOpenXmlImportContext context)
   at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Elements.OpenXmlElementBase.Read(IOpenXmlReader reader, IOpenXmlImportContext context)
   at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Elements.OpenXmlElementBase.ReadChildElements(IOpenXmlReader reader, IOpenXmlImportContext context)
   at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Elements.OpenXmlElementBase.Read(IOpenXmlReader reader, IOpenXmlImportContext context)
   at Telerik.Windows.Documents.FormatProviders.OpenXml.Model.Parts.OpenXmlPartBase.Import(IOpenXmlReader reader, IOpenXmlImportContext context)
   at Telerik.Windows.Documents.FormatProviders.OpenXml.OpenXmlImporter`1.ImportXlsxPartFromArchive(ZipArchiveEntry zipEntry, OpenXmlPartBase part, IOpenXmlImportContext context)
   at Telerik.Windows.Documents.FormatProviders.OpenXml.OpenXmlImporter`1.Import(Stream input, IOpenXmlImportContext context)
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider.ImportOverride(Stream input)
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.WorkbookFormatProviderBase.Import(Stream input)
   at Dispatcher.Controllers.ClientsController.<GenerateAccurals>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at lambda_method(Closure , Object )
   at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilter>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()
Exception thrown: 'System.NullReferenceException' in Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.dll
Object reference not set to an instance of an object.

Tanya
Telerik team
 answered on 28 Mar 2019
1 answer
715 views

Hi 

I coding below . I can convert file but document file inside textbox , these textbox can't convert to pdf file and document file inside picture alignment fix positing on page but can't convert to pdf file. I am using below code and I got empty page after convert pdf file. Because my doc file inside picture and textbox.

I try other document file include pic (in line with text ) and not include textbox. These file can get correct format with pdf file.

 

My Coding.....
            Dim fileFormatProvider As IFormatProvider(Of Flow.Model.RadFlowDocument) = New Flow.FormatProviders.Docx.DocxFormatProvider()
            Dim documentToConvert As Flow.Model.RadFlowDocument = New Flow.Model.RadFlowDocument()

            Using input As FileStream = New FileStream("H:\\Files\\Testing.docx", FileMode.Open)
                documentToConvert = fileFormatProvider.Import(input)
            End Using

            fileFormatProvider = New Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider()

            Using output As Stream = New FileStream(pathpdf, FileMode.OpenOrCreate)
                fileFormatProvider.Export(documentToConvert, output)
            End Using

 

 

Thanks

Moe

 

Tanya
Telerik team
 answered on 26 Mar 2019
3 answers
1.3K+ views
Hello.

I am trying to convert docx-files to pdf-files using DocxFormatProvider to import the files, and using PdfFormatProvider to export the files. This works fine in most cases, but it seems that images that are not inline are missing from the generated pdf-file.

Example:
If I have a word-file with 2 images and some text. One image is inline, another is not (lets say it has the text-wrap setting to "square"). I import the docx-file to a RadFlowDocument. The inline image gets detected in the RadFlowDocument as ImageInLine, while the image with text-wrap gets detected as FloatingImage. In the end I export as PDF and save it locally. When I open the PDF, only text and the inline image is showing, the floating (text-wrapped) image is gone.

Is there a way to keep all images when converting from docx to pdf?

Here is a snippet of my code:
 
var providerDocx = new DocxFormatProvider();
var document = providerDocx.Import(inStream); //inStream is read from a docx file earlier
 
var providerPdf = new PdfFormatProvider();
 
Stream outStream = new MemoryStream();
providerPdf.Export(document, outStream);
 
//Test the conversion:
var fileStream = File.Create("PdfTest.pdf");
outStream.Seek(0, SeekOrigin.Begin);
outStream.CopyTo(fileStream);
fileStream.Close();
Tanya
Telerik team
 answered on 26 Mar 2019
1 answer
140 views

Hello, I had an application that used the excel interop, but I have changed it and now, to create the excels I use telerik, but some of my clients that use Excel 2003 have problems to open the excel created, they appear rare characters. Any ideas please? My code is something like that

string nombre= ruta + "\\excel.xls";
Workbook workbook = new Workbook();
workbook.Sheets.Add(SheetType.Worksheet);
Worksheet xlsSheet = xlsBook.ActiveWorksheet;
xlsSheet.Cells[1, 1].SetValue("prueba");
...
...
//Guardamos y abrimos la excel.
IWorkbookFormatProvider formatProvider = WorkbookFormatProvidersManager.GetProviderByName("XlsxFormatProvider");
using (Stream output = new FileStream(nombre, FileMode.Create))
{
    formatProvider.Export(workbook, output);
}

Thank you!

Tanya
Telerik team
 answered on 26 Mar 2019
3 answers
1.9K+ views
We have a requirement to convert a HTML document to PDF. When we try to import the HTML using HtmlFormatProvider the CSS styles are not imported and we also loose the table of contents hyperlink in the document. In turn the PDF which is exported does not seem to have the styles applied. Please could you provide your suggestion on how to achieve HTML to PDF conversion along with CSS styles.
Peshito
Telerik team
 answered on 26 Mar 2019
1 answer
142 views

Good day,

I'm importing an Excel file and I need to validate all the cells an then import the result in our database.

When I have and error I change the Cell color. For small file I don't fell a performance issue. But on big file it's not possible. It take about half a second to change the color.

I turn off the History mode on the Woorkbook.  wb.History.IsEnabled = false;

This is my code with cellSelection.SetFill(SolidErrorFill); commented to have better speed. How can I achieve a better speed ?

Thanks!

01.public override IExcelRuleResult Evaluate(Worksheet ws)
02.       {
03.           var result = new ExcelRuleResult();
04.           result.ExcelIsValid = true;
05.           CellRange cr = ws.UsedCellRange;
06.           if (cr != null)
07.           {
08.               for (int rowIndex = 1; rowIndex <= cr.ToIndex.RowIndex; rowIndex++)
09.               {
10.                   // Read Row 0 for header name and validate if all the cells have the same format
11.                   var sale = new Sale();
12.                   for (int columnIndex = cr.FromIndex.ColumnIndex; columnIndex <= cr.ToIndex.ColumnIndex; columnIndex++)
13.                   {
14.                       RangePropertyValue<ICellValue> rpv = null;
15.                       CellSelection cellSelection = null;
16.                       //var rpvName = ws.Cells[0, columnIndex].GetValue();
17.                       var columnName = (layout.Layouts[columnIndex] as XlsxLayoutBase)?.ColumnName;
18.                       var columnLayout = layout.Layouts[columnIndex] as XlsxLayoutBase;
19.                       string value = null;
20.                       if (columnLayout != null)
21.                       {
22.                           try
23.                           {
24. 
25.                               cellSelection = ws.Cells[rowIndex, columnIndex];
26.                               rpv = cellSelection.GetValue();
27.                               if (rpv.Value is FormulaCellValue fcv)
28.                               {
29.                                   value = fcv.GetResultValueAsString(new CellValueFormat(columnLayout.FormatString));
30.                               }
31.                               else
32.                               {
33.                                   value = rpv.Value.RawValue;
34.                               }
35. 
36.                               if (columnLayout.DataType == EnumDataType.Date)
37.                               {
38.                                   // Assembly: ServiceStack.Text .ToDoubleInvariant()
39.                                   value = FormatHelper.ConvertDoubleToDateTime(value.ToDoubleInvariant())?.Date.ToString("O");
40.                               }
41. 
42.                               if (columnLayout.Mandatory && string.IsNullOrWhiteSpace(value))
43.                               {
44.                                   //cellSelection.SetFill(SolidErrorFill);
45.                                   // We have {rowIndex+1} so it's the same line excel, 0 Base array in c# and Excel start at 1
46.                                   result.Messages.Add($"Mandatory Value: ({value}) for ColumnName ({columnName}) at Row {rowIndex + 1}");
47.                                   result.ExcelIsValid = false;
48.                               }
49.                               else
50.                               {
51.                                   //MapperHelper.AssignFieldValue(value, sale, columnName);
52.                               }
53.                           }
54.                           catch (Exception ex)
55.                           {
56.                               //cellSelection?.SetFill(SolidErrorFill);
57.                               result.ExcelIsValid = false;
58.                               // We have {rowIndex+1} so it's the same line excel, 0 Base array in c# and Excel start at 1
59.                               result.Messages.Add($"Exception Column Value: ({value}) for ColumnName ({columnName}) at Row {rowIndex + 1}  {ex.Message}");
60.                           }
61.                       }
62.                   }
63.                   Sales.Add(sale);
64.                   Console.WriteLine(rowIndex);
65.               }
66.           }
67.           return result;
68.       }
Nikolay Demirev
Telerik team
 answered on 25 Mar 2019
1 answer
94 views

HI,

I have an application which is generating a PDF report using FIxedContentEditor, some of the report must contain a dump of the contents of various RadPanel controls, my current implementation first creates a bitmap of the control using control.DrawToBitmap and then converts this to a BitmapSource and then an ImageSource.

Its not particularly fast and takes around 2.5 seconds for a single 3000x2000 full page image to be added to the PDF, is there a better / faster / more efficient way this can be achieved ?

Regards

Toby

Georgi
Telerik team
 answered on 21 Mar 2019
Narrow your results
Selected tags
Tags
+? more
Top users last month
Rob
Top achievements
Rank 3
Iron
Iron
Iron
Atul
Top achievements
Rank 1
Iron
Iron
Alexander
Top achievements
Rank 1
Veteran
Iron
Serkan
Top achievements
Rank 1
Iron
Shawn
Top achievements
Rank 1
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Rob
Top achievements
Rank 3
Iron
Iron
Iron
Atul
Top achievements
Rank 1
Iron
Iron
Alexander
Top achievements
Rank 1
Veteran
Iron
Serkan
Top achievements
Rank 1
Iron
Shawn
Top achievements
Rank 1
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?