Telerik Forums
Telerik Document Processing Forum
1 answer
209 views

I have installed the necessary nuget packages for pdf processing in an exisiting Asp.Net Core (.Net 7.0) Blazor project.

- Telerik.Windows.Documents.Core 2023.1.104
- Telerik.Windows.Documents.Fixed 2023.1.104
- Telerik.Windows.Zip 2023.1.104

In the OnClick handler of a button, the first line of code throws an exception:                   

 

       public void EditPdfDocument(MouseEventArgs args)
        {
            var provider = new PdfFormatProvider(); // exception is thrown here
            var doc = provider.Import(File.ReadAllBytes(@"path-to-file.pdf"));

            // ...
        }

Exception details:

System.TypeLoadException
  HResult=0x80131522
  Message=Could not load type 'System.Windows.Rect' from assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
  Source=Telerik.Windows.Documents.Fixed
  StackTrace:
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider..ctor()
  ...


Dimitar
Telerik team
 answered on 06 Feb 2023
1 answer
39 views

Hello,

I would like to know how can i rotate a given pdf document using Telerik Document Processing Library.

 

Thank you

Dimitar
Telerik team
 answered on 06 Feb 2023
1 answer
163 views

Hi there,

I am using the trial version at the moment to see if Telerik's document processing can replace another library we are currently using.

Here is our scenario,

We have a Word template document that has merge fields already setup and tables with borders already setup. This word template is read from a database table column into a memory stream and then we use the DocxFormatProvider to import the document to the RadFlowDocument. Below you can see a piece of the template document already setup.

When using EnumerateChildrenOfType<Table>().ElementAt(1) for example I can see that the table is selected and I can see that the border values have been picked up from the word template document. Below inspecting object for the table selected from the template document.

We then perform our mail merge and then use PdfFormatProvider to convert the merge result into a PDF document. The result is that the mail merge fields are merged correctly but all the tables have lost there borders that the original word template document had.

I have tried to after using EnumerateChildrenOfType<Table>().ElementAt(1)  to set the border again manually with new TableBorders(new Border(2, Telerik.Windows.Documents.Flow.Model.Styles.BorderStyle.Dotted, new ThemableColor(Colors.Black))); This also does not work.

Here is the PDF result produced, any suggestions would be appreciated. (Also a note, even if I take out mail merge in the process, the document converting straight from Word Template to PDF still produces the tables with no borders...)

Another note when I use your demo link and load my template and export to PDF the borders are also missing. (Telerik Export to PDF)

Vladislav
Telerik team
 answered on 23 Jan 2023
1 answer
128 views

Hi

I have been able to merge PDFs files all together into a single document by using File.OpenRead to read into a PdfFIleSource and then use the PdfStreamWriter WritePage method to concatenate them all.

But now I want to scale down the pages to be able to add page numbering.

I haven't been able to use scale down (reduce) using the previous method.

I have found another mechanism (https://www.telerik.com/forums/scaling-pdf-document) that is reading the input pages into a RadFixedDocument before adding them with WritePage to the PdfStreamWriter. This works (page are reduced as I want) but when input PDFs are annoted (textboxes or images), the annotations are not added to the scaled down output.

Do you have a sample that is doing that (merge scale down input files while keeping annotations)?

Yoan
Telerik team
 answered on 18 Jan 2023
1 answer
201 views

Hi!

How can I make a custom font work, when importing a HTML string and printing it to pdf? Im loosing the "ÄÖÜ" characters currently and know that this is a font related issue.

 

var htmlDocument = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();

// Regular Font
byte[] fontData = System.IO.File.ReadAllBytes("fonts/verdana.ttf");
Telerik.Documents.Core.Fonts.FontFamily fontFamily = new Telerik.Documents.Core.Fonts.FontFamily("CustomVerdana");
FontsRepository.RegisterFont(fontFamily, FontStyles.Normal, FontWeights.Normal, fontData);

var doc = htmlDocument.Import("<html><body style=\"font-family:CustomVerdana;\">Das ist ein test. <br> <strong>ÖOÜUÄA</strong></body></html>");

var fileName = "output.pdf";
var pdf = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
using (Stream output = System.IO.File.OpenWrite(fileName))
{
      pdf.Export(doc, output);
 }

Dimitar
Telerik team
 answered on 18 Jan 2023
1 answer
97 views

Hi ,
I cannot figure out how to add an invisible signature to an existing PDF.

My test code:


var pdfIn = "C:\\Prg\\Test\\TestTelerikPDF\\pdf\\demo.pdf";
var pdfSigned = $"{pdfIn}.sign.pdf";
var pfx = "C:\\Prg\\Test\\TestTelerikPDF\\pdf\\Cert.pfx";

using (var input = new FileStream(pdfIn, FileMode.Open, FileAccess.Read))
{
    var document = new PdfFormatProvider().Import(input);

    var certificate = new X509Certificate2(pfx, "passs");
    var signatureField = new SignatureField("Blabla");
    signatureField.Signature = new Signature(certificate);

    document.AcroForm.FormFields.Add(signatureField);

    var settings = new PdfExportSettings();
    settings.ComplianceLevel = PdfComplianceLevel.PdfA3B;

    var formatProvider = new PdfFormatProvider();
    formatProvider.ExportSettings = settings;

    using (var ms = new FileStream(pdfSigned, FileMode.Create))
        formatProvider.Export(document, ms);
}

This code create an output PDF but the signature is missing :-(

Can you help me please?

Best

Kamil 

Yoan
Telerik team
 answered on 17 Jan 2023
1 answer
101 views

Hi,

some of those are briefly mentioned in the docs, others have been subject in previous requests in this forum. Nevertheless, I could not succeed doing these things. It is really hard to describe what i want properly, but i try my best:

- I want the content to take more space of the A4 page, means i want to crop away some of the white space of the top, right, bottom and left to make more room for the content.

- I want a table to take up 100% of the page's height and then in one column have one text block to be positioned at the top and another text block at the bottom. Similar to using flexbox in css and have it "justify-content: space-between", see the "text block" elements in the attached jpeg

- I need page numbers on each page. This was requested a number of times, but it seems like it was still not implemented. Is this correct? What is the best way to do this in 2023? see the numbers at the bottom in the jpeg

- I want to add a repeating page header to each page. see the Repeating Heading element in the jpeg

I created a rough layout design, which is attached as jpeg. Imagine the layout to be a table with currently one row and two columns.

I also took the current c# code and attached it.

Yoan
Telerik team
 answered on 12 Jan 2023
3 answers
190 views

I want to import pdf document to a RadFlowWordDocument in the current Run from code.

Is it possible? I have imported pdf using RadEditor.

 

Maria
Telerik team
 answered on 05 Jan 2023
1 answer
522 views

I am trying to generate a PDF document but don't know how to control the Page size. As the printout is most likely A4, I would like to print it horizontally in order to fit my table in. The following is my code snippet. The first few rows of the table are the header.

        // ----------------------------------------------
        // Print Request Test Schedule Allocation
        // -----------------------------------------------
        private void cmdPrint_Click(object sender, EventArgs e)
        {
            FixedContentEditor editor;
            RadFixedDocument document;
            RadFixedPage page;

            document = new RadFixedDocument();
            page = document.Pages.AddPage();
            editor = new FixedContentEditor(page, new SimplePosition());           

            Table table = new Table();
            table.BorderCollapse = BorderCollapse.Separate;
            table.LayoutType = TableLayoutType.AutoFit;
            table.Margin = new System.Windows.Thickness(50);

            table = SetTableHeader(table);

            editor.DrawTable(table)

            TableRow tableRow;
            foreach (KeyValuePair<string, TDFCollection> tDFObject in T.FileList)
            {
                Block block = new Block();
                string name = tDFObject.Value.TDFName;
                string desc = tDFObject.Value.TDFDescription;
                string requester = tDFObject.Value.Requester;
                string duration = tDFObject.Value.Duration.ToString();

                tableRow = table.Rows.AddTableRow();
                tableRow.Cells.AddTableCell().PreferredWidth = 20;
                block = tableRow.Cells.AddTableCell().Blocks.AddBlock();
                block.TextProperties.FontSize = 12;
                block.InsertText(name);
                tableRow.Cells.AddTableCell().PreferredWidth = 30;
                block = tableRow.Cells.AddTableCell().Blocks.AddBlock();
                block.TextProperties.FontSize = 12;
                block.InsertText(desc);
                tableRow.Cells.AddTableCell().PreferredWidth = 35;
                block = tableRow.Cells.AddTableCell().Blocks.AddBlock();
                block.TextProperties.FontSize = 12;
                block.InsertText(requester);
                tableRow.Cells.AddTableCell().PreferredWidth = 5;
                block = tableRow.Cells.AddTableCell().Blocks.AddBlock();
                block.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
                block.TextProperties.FontSize = 12;
                block.InsertText(duration);                
            }
            editor.DrawTable(table);

            int index = 0;
            string agendaFile = null;
            while (true)
            {
                index++;
                agendaFile = T.TEST_Scheduling + "Request Allocation" + " (" + index.ToString() + ").pdf";
                if (!File.Exists(agendaFile)) break;
            }

            PdfFormatProvider provider = new PdfFormatProvider();
            try
            {
                using (Stream output = File.OpenWrite(agendaFile))
                {
                    provider.Export(document, output);
                }
            }
            catch (Exception ex)
            {
                T.WriteLine("PDF Stream Write Exception : " + ex.Message);
                MessageBox.Show(ex.Message, "PDF Output Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("Test Schedule Allocation Requests PDF is created", "Print Test Allocation Requests", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // ---------------
        //  Set PDF Header
        // ---------------
        private Table SetTableHeader(Table table)
        {
            TableRow tableRow;
            tableRow = table.Rows.AddTableRow();
            tableRow.Cells.AddTableCell().PreferredWidth = 30;
            Block block = tableRow.Cells.AddTableCell().Blocks.AddBlock();
            block.TextProperties.FontSize= 14;
            block.GraphicProperties.FillColor = new RgbColor(0, 0, 139);
            block.InsertText("TEST SCHEDULING REQUESTS");         

            tableRow = table.Rows.AddTableRow();
            block = tableRow.Cells.AddTableCell().Blocks.AddBlock();
            block.TextProperties.FontSize = 12;
            block.InsertText(" ");

            tableRow = table.Rows.AddTableRow();
            tableRow.Cells.AddTableCell().PreferredWidth = 30;
            block.TextProperties.FontSize = 12;
            block = tableRow.Cells.AddTableCell().Blocks.AddBlock();           
            block.GraphicProperties.FillColor = new RgbColor(0, 0, 139);
            block.TextProperties.UnderlineColor = new RgbColor(0, 0, 139);
            block.InsertText("Test Definition");
            block.InsertLineBreak();

            tableRow.Cells.AddTableCell().PreferredWidth = 30;
            block = tableRow.Cells.AddTableCell().Blocks.AddBlock();
            block.TextProperties.FontSize = 12;
            block.GraphicProperties.FillColor = new RgbColor(0, 0, 139);
            block.TextProperties.UnderlineColor = new RgbColor(0, 0, 139);
            block.InsertText("Description");

            tableRow.Cells.AddTableCell().PreferredWidth = 35;
            block = tableRow.Cells.AddTableCell().Blocks.AddBlock();
            block.TextProperties.FontSize = 12;
            block.GraphicProperties.FillColor = new RgbColor(0, 0, 139);
            block.TextProperties.UnderlineColor = new RgbColor(0, 0, 139);
            block.InsertText("Requester");

            tableRow.Cells.AddTableCell().PreferredWidth = 5;
            block = tableRow.Cells.AddTableCell().Blocks.AddBlock();
            block.TextProperties.FontSize = 12;
            block.GraphicProperties.FillColor = new RgbColor(0, 0, 139);
            block.TextProperties.UnderlineColor = new RgbColor(0, 0, 139);
            block.InsertText("Duration");

            block = tableRow.Cells.AddTableCell().Blocks.AddBlock();
            block.TextProperties.FontSize = 12;
            block.InsertText("    ");

            return table;
        }   
    }

                                      
Yoan
Telerik team
 answered on 14 Dec 2022
1 answer
281 views

Hi, i'm trying to use document processing to convert HTML template to PDF, but fonts are missing from resulting file, maybe even from the imported html.

How can I find out if the fonts are already missing after importing HTML?

What is the best way to convert HTML to PDF keeping images, fonts, etc. using document processing?

This is one of my attempts to get it to work.

static void Main(string[] args)
        {
            string templateFileName = @"html template.htm";
            string pdfPath = @"test html template to pdf.pdf";
            FileStream templateAsStream = new FileStream(templateFileName, FileMode.Open, FileAccess.Read);

            ///////////
            //telerik
            ///////////

            

            try
            {               
                // Register the font 
                byte[] fontDataR = File.ReadAllBytes("Raleway-VariableFont_wght.ttf");
                System.Windows.Media.FontFamily fontFamilyR = new System.Windows.Media.FontFamily("Releway");
                byte[] fontDataRL = File.ReadAllBytes("Raleway-Light.ttf");
                System.Windows.Media.FontFamily fontFamilyRL = new System.Windows.Media.FontFamily("Releway Light");

                //Telerik.Windows.Documents.Fixed.Model.Fonts.
                FontsRepository.RegisterFont(fontFamilyR, System.Windows.FontStyles.Normal, System.Windows.FontWeights.Normal, fontDataR);
                FontsRepository.RegisterFont(fontFamilyRL, System.Windows.FontStyles.Normal, System.Windows.FontWeights.Normal, fontDataRL);

                byte[] fontDataRb = File.ReadAllBytes("Raleway-Bold.ttf");
                System.Windows.Media.FontFamily fontFamilyRb = new System.Windows.Media.FontFamily("Releway");
                //Telerik.Windows.Documents.Fixed.Model.Fonts.
                FontsRepository.RegisterFont(fontFamilyR, System.Windows.FontStyles.Normal, System.Windows.FontWeights.Bold, fontDataRb);


                //import AFTER setting fonts
                //Telerik.Windows.Documents.Flow.FormatProviders.Html.
                HtmlFormatProvider htmlProvider = new HtmlFormatProvider();
                HtmlImportSettings importSettings = new HtmlImportSettings();
                HtmlExportSettings exportSettings = new HtmlExportSettings();
                var document = htmlProvider.Import(templateAsStream);                

                PdfFormatProvider pdfProvider = new PdfFormatProvider();

                //Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider();
                //Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider;

                PdfExportSettings pdfExportSettings = new PdfExportSettings();
                pdfExportSettings.ShouldEmbedFonts = true;

                pdfProvider.ExportSettings = pdfExportSettings;


                FileStream stream = File.Create("fixed pdf.pdf");
                var radFixedDocument = (new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider()).ExportToFixedDocument(document);
                var fixedFormatProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
                fixedFormatProvider.Export(radFixedDocument, stream);
                stream.Close();

                //var resultBytes = pdfProvider.Export(document);
                //File.WriteAllBytes(pdfPath, resultBytes);

            }
            catch(Exception e)
            {
                throw e;
            }

I have also attached html file that is result of importing html and then export html

 

also I get this in pdf reader both if I use embed fonts option or not

 

https://www.telerik.com/forums/radflowdocument-to-pdf---arialnarrow-font-issues

Maria
Telerik team
 answered on 12 Dec 2022
Narrow your results
Selected tags
Tags
+? more
Top users last month
Mark
Top achievements
Rank 1
Yurii
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Hon
Top achievements
Rank 1
Iron
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Mark
Top achievements
Rank 1
Yurii
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Hon
Top achievements
Rank 1
Iron
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?