Telerik Forums
Telerik Document Processing Forum
9 answers
1.8K+ views
Hi

According to this thread it is possible to convert a DOCX file to a PDF. The code there no longer seems to work however.

The code I'm trying to run is:

RadDocument document = null;
IDocumentFormatProvider providerDocx = (IDocumentFormatProvider) new DocxFormatProvider();
using (FileStream stream = File.Open(@"C:\Test.docx", FileMode.Open))
{
    document = providerDocx.Import(stream);
}
 
PdfFormatProvider providerPdf = new PdfFormatProvider();
using (Stream output = File.Open(@"C:\Test.pdf", FileMode.Create))
{
    providerPdf.Export(document, output);
}


The error occurs on the last line: cannot convert from 'Telerik.Windows.Documents.Model.RadDocument' to 'Telerik.Windows.Documents.Fixed.Model.RadFixedDocument'

Is it still possible to convert from docx to PDF?
Tomáš
Top achievements
Rank 1
Iron
Iron
 answered on 30 Sep 2021
1 answer
469 views

Hello!

Have an issue w/ merging specific PDFs.  One thing that we noticed is we get the error when the following version of the Telerik.Windows.Documents.Fixed package:      <PackageReference Include="Telerik.Windows.Documents.Fixed" Version="2020.2.504" />

but it works correctly with the following package from 2018:

    <PackageReference Include="Telerik.Windows.Documents.Fixed">
      <Version>2018.1.220.40</Version>

Trying to figure out what the differences are between those package versions and if there is a work around for the 2020 version so we don't have to use the 2018 package.

The Code is failing on the provider.Import:

                    using (var pdfInputStream = GetPdfDocumentStream(client, uri))
                    {
                        RadFixedDocument document = provider.Import(pdfInputStream);
                        mergedPdf.Merge(document);
                    }

Thanks!

Martin
Telerik team
 answered on 29 Sep 2021
1 answer
3.7K+ views

 

I have problem with converting HTML to PDF with c# coding. Namely, I go through next steps:

1) creating HtmlFormatProvider

2) registering event LoadStyleSheetFromUri

3) importing Stream with HTML content. Content contains reference to bootstrap.css, Bootstrap v4.3.1 (https://getbootstrap.com/)

4) creating PdfFormatProvider

5) exporting to pdf.

Complete code is following:

       public async static Task<InputSignDocument> CreateDemoDocument(string wwwPath, string fileName, string extension, string recipientName, string recipientEmail, Func<Task<Stream>> getPdfStream)
        {
            var result = new InputSignDocument()
            { 
                FileName = fileName,
                FileExtension = extension,
            };

            Stream inpuContent = await getPdfStream(); // here is reading html into stream
            var htmlProvider = new HtmlFormatProvider();

            HtmlImportSettings importSettings = new HtmlImportSettings();

            importSettings.LoadStyleSheetFromUri += (s, e) =>
            {
                  string styles = File.ReadAllText($"{wwwPath}\\{e.Uri}");

                e.SetStyleSheetContent(styles);
            };
            importSettings.DefaultStyleSheet = "body {white-space: normal;}";
            htmlProvider.ImportSettings = importSettings;

            RadFlowDocument doc;
           using (StreamReader reader = new StreamReader(inpuContent))
            {
                string htmlText = reader.ReadToEnd();
                doc = htmlProvider.Import(htmlText);
            }

            var pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

            PdfExportSettings exportSettings = new PdfExportSettings()
            {

            };
            pdfProvider.ExportSettings = exportSettings;

            result.FileContent = new MemoryStream();
            pdfProvider.Export(doc, result.FileContent);

            return result;
        }

 

PDF is created however no bootstrap classes are apllied.

In debug session I look in variable (string styles) in event, css file was read properly.

HTML contains two references:

<link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="/css/site.css" />
From event I see that both files are exported.

In HTML, namely, in razor view are used bootstrap classes: container, col-lg-6, col-md-6, col-sm-6, row.

rhey are applied this way:

<div class="container">
    <div class="row">
        <div class="col-lg-6 col-md-6 col-sm-6">
            <div class="row">
                <div class="col-lg-4 col-md-4 col-sm-4">
                    <label>Werknemer</label>
                    <div>
                        /fnm1/
                    </div>
                </div>

                <label></label>
                <div class="col-lg-4 col-md-4 col-sm-4">
                    <label>Handtekening</label>
                    <div>
                        /sn1/
                        <hr />
                    </div>
                </div>

                <div class="col-lg-4 col-md-4 col-sm-4 docusign-basetab">
                    <label>Aangetekend op</label>
                    <div>
                        /ds1/
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

It seems that this part css is not applied in PDF File.

What I am missing?

When I run the view from controller it shows me this:

 

And in pdf it looks like this:

                  
Dimitar
Telerik team
 answered on 29 Sep 2021
5 answers
2.5K+ views
Hi,

I am trying to convert a HTML body to a PDF using HtmlFormatProvider and PdfFormatProvider. 

It works well when I try to create a pdf with "normal" characters, but when I use characters like "åäö" the characters is either missing or replaced with other character. 

I have seen a similiar issue that recommended to load in the fonts, but that did not solve the issue for me. 

I have added the code in a zip as an attchment to this issue. 

 

Thanks in advance.

Dimitar
Telerik team
 answered on 24 Sep 2021
1 answer
157 views

Is it possible to create AND password protect certain sections of a docx document using Blazor WordsProcessing?

 

MM

Dimitar
Telerik team
 answered on 23 Sep 2021
3 answers
1.1K+ views

hello,

i'm working on function to export word to PDF, and the result is inconherent, i'm loosing the special caracter like "é"  , "ô". the document is in french language, also the style of wording of the document.

this is my function, any suggestion to resolve it?

regards.


public static void ConvertWordtopdf(string input, string output)
        {
            var docxProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
            var pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();


            try
            {
                //var docxBytes = File.ReadAllBytes(input);
                var docxBytes = System.IO.File.OpenRead(input);
                RadFlowDocument docx = docxProvider.Import(docxBytes);

                var pdfBytes = pdfProvider.Export(docx);
                File.WriteAllBytes(output, pdfBytes);

                docxBytes.Dispose();
                
            }
            catch (Exception ex)
            {
                ;
            }
        }

1 answer
180 views

Dear Telerik Developers,

 

Since the Xamarin Platform realize the Rich Text Editor by a webview control, I have to convert my Rtf files into html format before loading.

According to the Html Conversion document, headings ( h1 - h6 )  styles have nothing to do with the Outline Level,  and would be lost when exporting to other formats.

While in my scenario, the outline data of original Rtf document should be kept while converting back from html format to Rtf code.

My suggestion is adding a "EnableOutlineToHeadingsConversion" option to both HtmlImportSettings and  HtmlExportSettings classes, so that we can decide if the Outline level data should be converted into corresponding Headings Style ( for example, Outline Level 1 -> h1 ) and vice versa.


HtmlFormatProvider provider = new HtmlFormatProvider(); 
HtmlExportSettings exportSettings = new HtmlExportSettings(); 
  
exportSettings.BordersMinimalThickness = 1; 
exportSettings.DocumentExportLevel = DocumentExportLevel.Fragment; 
exportSettings.IndentDocument = true; 
exportSettings.EnableOutlineToHeadingsConversion = true;
 
provider.ExportSettings = exportSettings; 

 

I sincerely hope this could be taken into Telerik's developing consideration.

 

Regards,

Thank you very much. 

 

Tanya
Telerik team
 answered on 01 Sep 2021
1 answer
397 views

I'm trying to convert a DataTable to a spreadsheet.

According to the documentation:

To use the DataTableFormatProvider you need to reference the Telerik.Windows.Documents.Spreadsheet assembly.

After adding a reference to this assembly, Visual Studio still does not recognize it. Where can I find the DataTableFormatProvider?

Thanks,

Tim

Tanya
Telerik team
 answered on 31 Aug 2021
1 answer
676 views

I am using versions 2021.1.322.40 of Core.dll, Fixed.dll and Zip.dll and when exporting a RadFixedDocument to pdf it tells me that the value of RadFixedDocument cannot be converted to RadFlowDocument

What can I do to solve this problem?

I am using this :

https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/formats-and-conversion/pdf/pdfformatprovider

Tanya
Telerik team
 answered on 31 Aug 2021
1 answer
1.3K+ views
Hi there, after many wasted hours of trying to find the right documentation, I am still unable to find where or how one would add markup to a pdf. 

If possible, could you please point me in the right direction, or make a simple tutorial for us? 

I am currently using RadFixedDocument, and have been able to, through a list of data, render those items as text fields on the pdf. 

I am just unable to add a string which contains HTML, and have it render accordingly. 

It just renders as the string it is. 

Dimitar
Telerik team
 answered on 09 Aug 2021
Narrow your results
Selected tags
Tags
+? more
Top users last month
Rob
Top achievements
Rank 3
Bronze
Iron
Iron
Sergii
Top achievements
Rank 1
Iron
Iron
Dedalus
Top achievements
Rank 1
Iron
Iron
Lan
Top achievements
Rank 1
Iron
Doug
Top achievements
Rank 1
Want to show your ninja superpower to fellow developers?
Top users last month
Rob
Top achievements
Rank 3
Bronze
Iron
Iron
Sergii
Top achievements
Rank 1
Iron
Iron
Dedalus
Top achievements
Rank 1
Iron
Iron
Lan
Top achievements
Rank 1
Iron
Doug
Top achievements
Rank 1
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?