Telerik Forums
Telerik Document Processing Forum
5 answers
934 views

How do I get (or "read") a table cell's text value?

For example:

                            var row = table.Rows[i];
                            int nbCols = row.Cells.Count();
                            if (!(nbCols < 1))
                            {
                                for (int j = 1; j < nbCols; j++)
                                {
                                    Run run = new Run(row.Cells[j].Blocks[0].Document);
                                    if (run.Text == "«Signature»")
                                    {
                                        RadFlowDocumentEditor colEditor = new RadFlowDocumentEditor(row.Cells[j].Blocks[0].Document);
                                        // if it's the Signature cell then replace the content of the cell by the Image ...
                                                      // How do I do this ??
                                        // see https://www.telerik.com/forums/table-cell-width
                                    }
                                }
                            }

 

In the code above I am trying to iterate through the collection of columns to find the one that contains text «Signature» so that I can replace that text with an image. The beast I could do is use the run as shown above but it never finds the value (although the value is in the table).

Thanks

 

 

 

Martin
Telerik team
 answered on 22 Jan 2021
1 answer
994 views
Is there any support for converting PowerPoint (PPT, PPTX) files to PDF?
Martin
Telerik team
 answered on 15 Jan 2021
7 answers
407 views

Hi,

I would like to export some excel files in a zip without writing on disk.

I have seen ZipLibrary example and I would like to adapt it with excel (if possible).

The idea is to do something like this in "OnClick" event for exemple (see code bellow)

Thanks in advance for your help

using (MemoryStream stream = new MemoryStream())
{
    using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true, entryNameEncoding: null))
    {
        //I don't need TXT but just for testing ...
        using (ZipArchiveEntry entry = archive.CreateEntry("text1.txt"))
        {
            StreamWriter writer = new StreamWriter(entry.Open());
            writer.WriteLine("Hello world!");
            writer.Flush();
        }
 
        //Adding excel file
        using (ZipArchiveEntry entry = archive.CreateEntry("testxlsx.xlsx"))
        {
            //How to handle / edit XLSX file here ?
        }
    }
 
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment; filename=zipfile.zip");
    Response.ContentType = "application/zip";
    Response.BinaryWrite(stream.ToArray());
    Response.End();
}
Julien
Top achievements
Rank 1
 answered on 13 Jan 2021
1 answer
342 views

Do you have any sample code to set a form field with an image so that the image is centered in the form field but keeps it's aspect ratio without going outside the bounding box? Does the image need to be resized first to reduce filesize?

Thanks.

Dimitar
Telerik team
 answered on 12 Jan 2021
3 answers
310 views

I am placing a image in a table cell and below the image I will be adding 1 to 3 lines of text.   I have it all working, but have 2 questions.

 

1.  I would like to center the image vertically in the table cell.

 

2. I want to single space the inserted text (paragraph) 

 

Below is my code:

 using (Stream firstImage = File.OpenRead(outputFileName))
                            {
                                var inImage = table1.Rows[x].Cells[y].Blocks.AddParagraph().Inlines.AddImageInline();

                                inImage.Image.Size = new System.Windows.Size(colWidth, colWidth);

                                inImage.Image.LockAspectRatio = true;
                                inImage.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(firstImage, "jpg");
                            }
                            table1.Rows[x].Cells[y].Blocks.AddParagraph().Inlines.AddRun(Path.GetFileNameWithoutExtension(h.fileName)); // filename

Dimitar
Telerik team
 answered on 12 Jan 2021
3 answers
179 views

Hello,

 

I'm trying to work with Telerik PDF-Processing right now. But an issue appeared, that I have no clue how to solve.

In this current project, time is a very important aspect, so it's necessary for me to save milliseconds everywhere i can. So I logged the time for all of the Processes and, I don't know why, the first time, i use the methods, it needs more time, than the other later runs.

Here's a simple example out of this project:

 

 private List<RadFixedPage> GetAllAddingPages(List<string> pathList)
        {    
            List<RadFixedPage> AllPages = new List<RadFixedPage>();

            for (int i = 1; i < pathList.Count; i++)
            {
                RadFixedPage read = provider.Import(File.ReadAllBytes(pathList[i])).Pages[0];
                AllPages.Add(read);
            }
            return AllPages;
        }

 

The method's supposed to read the first pages of every document, that is filled in the "PathList." 

I've stopped the first run and it needed about 150-160ms to execute. The second run is all about 20ms. There are only 3 Files that've been loaded.

So i tested more..

If i only load a document, that has no Image inside, it needs about the same time on the first run. If afterwards a document is loaded with only an Image, it needs long time also (103ms). If I open 3 documents (Vectorgraphics, Images and Text) it needs the 150-160ms. 

 

Is this a known issue, or can somebody maybe help me with this?

This is only one example, where this problem appears. But if there is a solution for this one, i can maybe apply it to the other methods.

 

Best regards

Jakob

Georgi
Telerik team
 answered on 05 Jan 2021
4 answers
836 views

I have a table that has a variable number of columns (from 2 to 6)

I set preferredWidth  based on number of columns as follows:

 table1.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);

 

I am inserting images into each table cell, but I want specify the size of the image based on the cell width

 

How do I get the cell width?

var inImage = table1.Rows[x].Cells[y].Blocks.AddParagraph().Inlines.AddImageInline();
                        var cellWidth =??
                         inImage.Image.Size = new System.Windows.Size(cellWidth, cellWidth);

 

 

Peshito
Telerik team
 answered on 04 Jan 2021
6 answers
1.3K+ views

 Hi All,

For the PDF Processing , I have to attache a  single table row and  there will be two cells.(some thing which is simillar to the attachment).

1.First  cell  constitutes of Question.

2.Second Sells constitues of a table.

 

It his scenario possible?

 

Regards,

JP

 

Martin
Telerik team
 answered on 22 Dec 2020
1 answer
214 views

Hello!
Faced an incomprehensible problem.
When trying to open a workbook, an exception is thrown:
ArgumentOutOfRangeException: Index and count must refer to a location within the string. (Parameter 'count')
At the same time, everything works fine on the working computer during development, there is no exception.
The problem pops up when placing a project on a hosting.

The library from Syncfusion in a similar environment opens the file normally on the hosting.

Library version 2020.3.1019
.net5 Server-side project

private void LoadFile()
        {
            try
            {
                string path = Path.Combine(Path.GetPathRoot(Environment.SystemDirectory), "Templates", $"temp.xlsx");
                using FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
 
                XlsxFormatProvider formatProvider = new XlsxFormatProvider();
 
                // Getting exception there while hosted
                using Workbook workbook = formatProvider.Import(stream);
            }
            catch (Exception error)
            {
                NotificationReference.Show(new NotificationModel()
                {
                    Text = $"{error.GetType().Name}: {error.Message}",
                    ThemeColor = "primary"
                });
            }
        }
Peshito
Telerik team
 answered on 21 Dec 2020
3 answers
474 views

Attached a project to demonstrate some issues we're seeing that need resolving in order to use the component.

1. When using a font like Tahoma on a form field, the value no longer displays or contains garbled text.

2. Input file is 24KB, output is 2MB!

3. Rotated field lose their rotation after being flattened.

4. Simulate merging with other files, 100 times the file is now 150MB! It's as though it's not re-using the fonts, but keeps adding the same ones.

5. when merging, but not flattening, the form fields are all lost on pages after page 1.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Streaming;
using Telerik.Windows.Documents.Fixed.Model;
using Telerik.Windows.Documents.Fixed.Model.Annotations;
using Telerik.Windows.Documents.Fixed.Model.Editing;
using Telerik.Windows.Documents.Fixed.Model.InteractiveForms;
using Telerik.Windows.Documents.Fixed.Model.Resources;
 
namespace FlattenFormFieldsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfFormatProvider provider = new PdfFormatProvider();
             
            RadFixedDocument document = provider.Import(File.ReadAllBytes("InteractiveForms.pdf"));
 
            foreach (RadFixedPage page in document.Pages)
            {
               foreach (Annotation annotation in page.Annotations)
               {
                  if (annotation.Type == AnnotationType.Widget)
                  {
                     Widget widget = (Widget) annotation;
                     if (widget.Field is TextBoxField field)
                     {
                        field.Value = "John Doe";
                     }
                  }
               }
            }
            
            FlattenFormFields(document);
 
            string resultFile = "Flattened.pdf";
 
            if(File.Exists(resultFile))
            {
                File.Delete(resultFile);
            }
 
            File.WriteAllBytes(resultFile, provider.Export(document));
 
            MergeMultipleFiles(resultFile);
        }
 
        public static void MergeMultipleFiles(string filename)
        {
           using (PdfStreamWriter fileWriter = new PdfStreamWriter(File.OpenWrite("MergedFlattened.pdf")))
           {
              // Iterate through the files you would like to merge
              for(int i = 0; i< 100; i++)
              {
                 // Open each of the files
                 using (PdfFileSource fileToMerge = new PdfFileSource(File.OpenRead(filename)))
                 {
                    // Iterate through the pages of the current document
                    foreach (PdfPageSource pageToMerge in fileToMerge.Pages)
                    {
                       // Append the current page to the fileWriter, which holds the stream of the result file
                       fileWriter.WritePage(pageToMerge);
                    }
                 }
              }
           }
        }
 
        public static void FlattenFormFields(RadFixedDocument document)
        {
            foreach (RadFixedPage page in document.Pages)
            {
                List<Widget> widgetsToRemove = new List<Widget>();
                FixedContentEditor pageEditor = new FixedContentEditor(page);
 
                foreach (Annotation annotation in page.Annotations)
                {
                    if (annotation.Type == AnnotationType.Widget)
                    {
                        Widget widget = (Widget)annotation;
                        FlattenWidgetAppearance(pageEditor, widget);
                        widgetsToRemove.Add(widget);
                    }
                }
 
                foreach (Widget widget in widgetsToRemove)
                {
                    page.Annotations.Remove(widget);
                }
            }
 
            foreach (FormField field in document.AcroForm.FormFields.ToArray())
            {
                document.AcroForm.FormFields.Remove(field);
            }
        }
 
        private static void FlattenWidgetAppearance(FixedContentEditor pageEditor, Widget widget)
        {
            FormSource widgetAppearance = GetWidgetNormalAppearance(widget);
            if (widgetAppearance == null) return;
            pageEditor.Position.Translate(widget.Rect.Left, widget.Rect.Top);
            pageEditor.DrawForm(widgetAppearance, widget.Rect.Width, widget.Rect.Height);
        }
 
        private static FormSource GetWidgetNormalAppearance(Widget widget)
        {
            FormSource widgetAppearance;
            switch (widget.WidgetContentType)
            {
                case WidgetContentType.PushButtonContent:
                    widgetAppearance = ((PushButtonWidget)widget).Content.NormalContentSource;
                    break;
                case WidgetContentType.SignatureContent:
                    widgetAppearance = ((SignatureWidget)widget).Content.NormalContentSource;
                    break;
                case WidgetContentType.VariableContent:
                    widgetAppearance = ((VariableContentWidget)widget).Content.NormalContentSource;
                    break;
                case WidgetContentType.TwoStatesContent:
                    TwoStatesButtonWidget twoStatesWidget = (TwoStatesButtonWidget)widget;
                    widgetAppearance = GetTwoStatesWidgetNormalAppearance(twoStatesWidget);
                    break;
                default:
                    throw new NotSupportedException(string.Format("Not supported widget content type {0}", widget.WidgetContentType));
            }
 
            return widgetAppearance;
        }
 
        private static FormSource GetTwoStatesWidgetNormalAppearance(TwoStatesButtonWidget twoStatesWidget)
        {
           FormField field = twoStatesWidget.Field;
           bool isOnState;
 
           switch (field.FieldType)
           {
              case FormFieldType.CheckBox:
                 CheckBoxField checkBox = (CheckBoxField) field;
                 isOnState = checkBox.IsChecked;
                 break;
              case FormFieldType.RadioButton:
                 RadioButtonField radio = (RadioButtonField) field;
                 RadioButtonWidget radioWidget = (RadioButtonWidget) twoStatesWidget;
 
                 if (radio.ShouldUpdateRadiosInUnison)
                 {
                    isOnState = radio.Value != null && radio.Value.Value.Equals(radioWidget.Option.Value);
                 }
                 else
                 {
                    isOnState = radio.Value == radioWidget.Option;
                 }
 
                 break;
              default:
                 throw new NotSupportedException(string.Format("Not supported field type {0} for TwoStateButtonWidget", field.FieldType));
           }
 
           FormSource widgetAppearance = (isOnState ? twoStatesWidget.OnStateContent : twoStatesWidget.OffStateContent).NormalContentSource;
 
           return widgetAppearance;
        }
    }
}

 

Martin
Telerik team
 answered on 21 Dec 2020
Narrow your results
Selected tags
Tags
+? more
Top users last month
Jay
Top achievements
Rank 3
Bronze
Iron
Iron
yw
Top achievements
Rank 2
Iron
Iron
Stefan
Top achievements
Rank 2
Iron
Iron
Iron
Kao Hung
Top achievements
Rank 1
Iron
Bohdan
Top achievements
Rank 2
Iron
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Jay
Top achievements
Rank 3
Bronze
Iron
Iron
yw
Top achievements
Rank 2
Iron
Iron
Stefan
Top achievements
Rank 2
Iron
Iron
Iron
Kao Hung
Top achievements
Rank 1
Iron
Bohdan
Top achievements
Rank 2
Iron
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?