Telerik Forums
Telerik Document Processing Forum
1 answer
57 views

Hi,

1) i want to add and image and text in the signature field with specified size and i want to do that in the existing pdf with signature field ?
2) how to add any type of image [jpg, jpeg , png, etc.] in extsting pdf 
 static byte[] GenerateSignedPdf(Cert objCert)
 {
     // Dimensions and positions for the signature field
     int signatureFieldWidth = 200;
     int signatureFieldHeight = 50;
     int signaturePositionLeft = 10;
     int signaturePositionTop = 10;

     // Load the certificate
     X509Certificate2 certificate = new X509Certificate2(objCert.CertArray, objCert.Password);

     // Create a SignatureField and assign the digital signature to it
     SignatureField pdfSignature = new SignatureField("SignatureField");
     pdfSignature.Signature = new Telerik.Windows.Documents.Fixed.Model.DigitalSignatures.Signature(certificate);

     // Create a new form to place the signature field
     Form pdfForm = new Form();
     pdfForm.FormSource = new FormSource();
     pdfForm.FormSource.Size = new Telerik.Documents.Primitives.Size(signatureFieldWidth, signatureFieldHeight);
     FixedContentEditor editor = new FixedContentEditor(pdfForm.FormSource);

     // Draw the text with certificate holder's name and current date
     string textToDraw = $"{certificate.GetNameInfo(X509NameType.SimpleName, false)} {DateTime.Now:yyyy.MM.dd HH:mm}";
     editor.DrawText(textToDraw, new Telerik.Documents.Primitives.Size(5, 5)); // Adjust position as needed

     // Path to the image (ensure this is correct)
     string imagePath = "C:/Vaibhav/ProjectVaibhav/PdfProcessing_InsertImageInExistingPdf/image.png"; // Change path as needed

     // Ensure the image exists and can be accessed
     if (File.Exists(imagePath))
     {
         using (var imageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
         {
             var imageSource = new ImageSource(imageStream);
             editor.DrawImage(imageSource, new Telerik.Documents.Primitives.Size(100, 50)); // Adjust image size and position as needed
         }
     }
     else
     {
         throw new FileNotFoundException("Image file not found at: " + imagePath);
     }

     // Create the SignatureWidget and position it on the PDF page
     SignatureWidget signatureWidget = pdfSignature.Widgets.AddWidget();
     signatureWidget.Content.NormalContentSource = pdfForm.FormSource;
     signatureWidget.Rect = new Rect(signaturePositionLeft, signaturePositionTop, signatureFieldWidth, signatureFieldHeight);
     signatureWidget.RecalculateContent();

     // Create a RadFixedDocument and add a page
     RadFixedDocument document = new RadFixedDocument();
     RadFixedPage pdfPage = document.Pages.AddPage();
     pdfPage.Annotations.Add(signatureWidget);

     // Add content from the form to the page at the specified position
     FixedContentEditor pageEditor = new FixedContentEditor(pdfPage);
     pageEditor.Position.Translate(signaturePositionLeft, signaturePositionTop);
     pageEditor.DrawForm(pdfForm.FormSource);

     // Add the signature field to the document's AcroForm
     document.AcroForm.FormFields.Add(pdfSignature);

     // Use MemoryStream to capture the PDF output and return as a byte array
     using (MemoryStream memoryStream = new MemoryStream())
     {
         var pdfFormatProvider = new PdfFormatProvider();
         pdfFormatProvider.Export(document, memoryStream);
         return memoryStream.ToArray(); // Return the byte array of the PDF
     }
 }

1 answer
192 views
System.InvalidOperationException: 'FixedExtensibilityManager.ImagePropertiesResolver and FixedExtensibilityManager.JpegImageConverter cannot be both null. The .NET Standard does not define APIs for decoding images. That`s why in order to export images different than Jpeg and Jpeg2000 and ImageQuality different than High you will need to reference the Telerik.Documents.ImageUtils assembly/NuGet in your project and to set its basic implementation to the ImagePropertiesResolver/JpegImageConverter property or to create a custom one inheriting the ImagePropertiesResolverBase/JpegImageConverterBase class. For more information go to: https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/cross-platform/images'
Dess | Tech Support Engineer, Principal
Telerik team
 answered on 28 Nov 2024
1 answer
200 views

How can i add borders to the first page or section?

I find the property borders in Paragraph, but i cant find it in Section, how can i add the border to the full page or section?

Example attached.

 

Regard

Yoan
Telerik team
 updated answer on 28 Oct 2024
0 answers
108 views

I am trying to programmatically reproduce a header, and nothing I've tried will add a background color or shading.  How do you format a table in a header?


HtmlFormatProvider provider = new HtmlFormatProvider();
RadFlowDocument html = provider.Import(sb.ToString());
Section section = html.Sections[0];
section.PageMargins = new Telerik.Windows.Documents.Primitives.Padding(24);
ThemableColor gray = new ThemableColor(new System.Windows.Media.Color {R = 220, G = 220, B = 220});

Header header = section.Headers.Add();
Table table = header.Blocks.AddTable();
TableRow row1 = table.Rows.AddTableRow();
TableCell Title = row1.Cells.AddTableCell();
TableCell Version = row1.Cells.AddTableCell();

Title.PreferredWidth = new TableWidthUnit(384);
Title.Shading.BackgroundColor = gray;
Title.Blocks.AddParagraph().Inlines.AddRun("Executive Summary");

Version.PreferredWidth = new TableWidthUnit(384);
Version.Shading.BackgroundColor = gray;
Version.Blocks.AddParagraph().Inlines.AddRun("Version Name:");

TableRow row2 = table.Rows.AddTableRow();
TableCell Incident = row2.Cells.AddTableCell();
TableCell Period = row2.Cells.AddTableCell();
                    
Incident.Blocks.AddParagraph().Inlines.AddRun("Incident:");
Period.Blocks.AddParagraph().Inlines.AddRun("Period:");

PdfFormatProvider pdf = new PdfFormatProvider();

return pdf.Export(html);

The attached Header.png is an example of what I'm trying to reproduce.

Darwin
Top achievements
Rank 1
 asked on 22 Oct 2024
2 answers
86 views

Hi,

I have: Telerik UI for ASP.NET AJAX

I test SplitDocumentPages from PDF example. from https://github.com/telerik/document-processing-sdk/blob/master/PdfProcessing/ManipulatePages/Program.cs

net.framework: 4.8

test pdf is: https://github.com/telerik/document-processing-sdk/blob/master/PdfProcessing/ManipulatePages/InputFiles/MultipageDocument.pdf

Not working well. see picture split1.pdf, only large rectangle without any data.

Please help.

Thx

Richard
Top achievements
Rank 1
Iron
 answered on 16 Oct 2024
1 answer
69 views

Hi,

 

I have an existing xlsx file that contains graphics (drawing)

I simply open the workbook, 


var workbook = CreateWorkbook();

byte[] bytes;

using (var output = new MemoryStream())
{
    new XlsxFormatProvider().Export(workbook, output);
    bytes = output.ToArray();
}

File.WriteAllBytes("c:\\Logs\\test.xlsx", bytes);


private Workbook CreateWorkbook()
{
    Workbook workbook;
    IWorkbookFormatProvider formatProvider = new XlsxFormatProvider();
    FileStream? stream = null;
    try
    {
        stream = File.Open("AnalyseImpayeTemplate.xlsx", FileMode.Open);
        workbook = formatProvider.Import(stream);
    }
    catch (IOException ex)
    {
        throw new IOException("The file cannot be opened. It might be locked by another application.", ex);
    }
    finally
    {
        stream?.Dispose();
    }

    return workbook;
}

When I open the result file, with release 2024.3.806, I got an error. It delete drawing parts :

Partie supprimée: /xl/drawings/drawing1.xml partie.  (Forme de dessin)

With release 2024.2.426, it works fine.

Any ideas why?

Yoan
Telerik team
 answered on 08 Oct 2024
1 answer
65 views

Hi

I have a problem with row height when processing docx files and exporting to pdf.
Export does not respect the height of a table row if the height of a given row is lower than the content of cells in that row. However, if the height of the row is greater than the content, then it is ok.
During processing, a given row has its height set as Exact and a specific value in pixels, but after export, the height of a given row in the table adjusts to the content.

Maybe there is a workaround for this problem?

Yoan
Telerik team
 answered on 09 Sep 2024
2 answers
112 views

 

I have the following code that shows the bold formatting from HTML to PDF appears inconsistent.

The last table has all the cells wrapped in <b> tags, but they are not rendered bold in the PDF. There seems to be some correlation to the length of the text (short text sometimes will be bold). I have also tried <strong> tags and CSS that applies bold. It does not matter which font is chosen.

Inconclusive observation: It appears that if the bold text line wraps inside a cell, it doesn’t apply bold. Text that does not wrap in the cell appears to apply bold.

Any help appreciated.

 


using RegServices.Portal.Web.Services.Pdf;
using Telerik.Documents.ImageUtils;
using Telerik.Windows.Documents.Flow.FormatProviders.Html;
using Telerik.Windows.Documents.Flow.Model;
using Xunit.Abstractions;

namespace Portal.Web.Tests.Pdf;

public class PdfBoldRenderTests : OutputTestBase
{
    private readonly IHtmlToWordConverter _htmlToWordConverter = new HtmlToWordConverter();
    string path = "c:\\\\temp\\\\pdf\\\\";
    public PdfBoldRenderTests(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void CanRenderBold()
    {
        var html = $$"""
                   <!DOCTYPE html>
                   <html lang="en">
                      <head>
                         <title>Test</title>
                         <style>
                            body { 
                            font-family: Arial;
                            }
                            .bold {
                            font-weight: bold;
                            }
                            .font-bold {
                            font-weight: 800;
                            }
                            .font-black {
                            font-weight: 800;
                            }
                         </style>
                      </head>
                      <body>
                         <table>
                            <tr>
                               <td>
                                  The quick brow fox. Regular text <br>
                                  <b>The quick brow fox.  Bold text</b><br>
                                  <span class="bold">The quick brow fox.  Bold text via css</span><br>
                               </td>
                               <td>
                                  <table>
                                     <tr>
                                        <td>
                                           The quick brow fox. Regular text <br>
                                           <b>The quick brow fox.  Bold text</b><br>
                                           <span class="bold">The quick brow fox.  Bold text via css</span><br>
                                        </td>
                                     </tr>
                                  </table>
                               </td>
                            </tr>
                            <tr>
                               <td colspan="2">
                                  <table>
                                     <tbody>
                                        <tr>
                                           <td class="w-2-24">
                                              <b>Financial year</b>
                                           </td>
                                           <td>
                                              <b>General Services Charge (range)</b><em>(weekly)</em>
                                              <span class="font-black">General Services Charge (range)</span>
                                           </td>
                                           <td class="w-2-24">
                                              <b>Overall % change from previous year </b>
                                           </td>
                                           <td>
                                              <b>
                                              Maintenance Reserve Fund contribution (range)
                                              </b>
                                              <em>(weekly)</em>
                                           </td>
                                           <td class="w-2-24">
                                              <b>Overall % change from previous year</b>
                                              <em>(+ or -)</em>
                                           </td>
                                        </tr>
                                  </table>
                               </td>
                            </tr>
                         </table>
                         <b>The quick brow fox.  Bold text</b><br>
                         <table>
                            <tbody>
                               <tr>
                                  <td class="w-2-24">
                                     <b>Financial year</b>
                                  </td>
                                  <td>
                                     <b>Bold Text</b><em>(weekly)</em>
                                     <span class="font-black">General Services Charge (range)</span>
                                  </td>
                                  <td class="w-2-24">
                                     <b>Bold Text</b>
                                  </td>
                                  <td>
                                     <b>
                                     Bold Text
                                     </b>
                                     <em>(weekly)</em>
                                  </td>
                                  <td class="w-2-24">
                                     <b>Bold Text</b>
                                     <em>(+ or -)</em>
                                  </td>
                               </tr>
                            </tbody>
                         </table>
                         
                         <b>The quick brow fox.  Bold text</b><br>
                         <table>
                            <tr>
                               <td>
                                  <b>I am a bold text</b>
                               </td>
                               <td>
                                  <b>I am a bold text
                                  </b><em>(weekly)</em>
                               </td>
                               <td>
                                  <b>I am a bold text</b>
                               </td>
                               <td>
                                  <b>
                                  I am a bold text test test
                                  </b>
                                  <em>(weekly)</em>
                               </td>
                               <td>
                                  <b>I am a bold text test</b>
                               </td>
                            </tr>
                         </table>
                      </body>
                   </html>
                   """;

        var htmlDocument = _htmlToWordConverter.ConvertHtmlToWord(html);
        var docxProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();

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

        using var memoryStream = new MemoryStream();
        docxProvider.Export(htmlDocument, memoryStream);
        var documentBytes = memoryStream.ToArray();

        File.WriteAllBytes($"{path}Test.docx", documentBytes);

        var (pdfBytes, pdfDoc) = PdfConverter.ConvertDocxToPdf(documentBytes);
        File.WriteAllBytes($"{path}Test.pdf", pdfBytes);

        var provider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
        using Stream pdfStream = File.OpenWrite($"{path}Test2.pdf");
        provider.Export(pdfDoc, pdfStream);
    }
    
    public interface IHtmlToWordConverter
    {
        RadFlowDocument ConvertHtmlToWord(string html);
    }

    public class HtmlToWordConverter : IHtmlToWordConverter
    {
        public RadFlowDocument ConvertHtmlToWord(string html)
        {
            var htmlFormatProvider = new HtmlFormatProvider();
            return htmlFormatProvider.Import(html);
        }
    }

    public static class PdfConverter
    {
        static PdfConverter()
        {
            var defaultImagePropertiesResolver = new ImagePropertiesResolver();
            Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver =
                defaultImagePropertiesResolver;
        }

        public static (byte[] bytes, RadFlowDocument doc) ConvertDocxToPdf(byte[] docxBytes)
        {
            var docxProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
            var pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

            var document = docxProvider.Import(docxBytes);

            using var memoryStream = new MemoryStream();
            pdfProvider.Export(document, memoryStream);
            pdfProvider.ExportToFixedDocument(document);
            return (memoryStream.ToArray(), document);
        }
    }
}


Setven
Top achievements
Rank 1
Iron
Iron
 answered on 31 Aug 2024
0 answers
161 views

I tried doing the following:

using Stream stream = File.OpenRead(fileName);
RadFixedDocument pdfDocument = new PdfFormatProvider().Import(stream);
RadFixedDocument pdfNew = new();
pdfNew.Pages.Add(pdfDocument.Pages.First());

I get the following error:

System.ArgumentException: 'The document element is associated with another parent. (Parameter 'item')'

 

Is there a way to clone a page, so it can be added to another document?

Henrik
Top achievements
Rank 1
 asked on 20 Aug 2024
2 answers
245 views

I am having trouble getting some HTML converted to PDF to render Unicode checkboxes.

\u2612 ☒ and \u2610  

They appear ok in the word conversion, but are missing in PDF

Here is the same code


using Telerik.Documents.ImageUtils;
using Telerik.Windows.Documents.Flow.FormatProviders.Html;
using Telerik.Windows.Documents.Flow.Model;

namespace Web.Tests.Pdf;

public class PdfRenderTests
{
    private readonly IHtmlToWordConverter _htmlToWordConverter = new HtmlToWordConverter();
    string path = "c:\\\\temp\\\\pdf\\\\";

    [Fact]
    public void CanRenderCheckbox()
    {
        var html = "<span class=\"checkbox\">\u2612 Yes \u2610 No</span>";
        var htmlDocument = _htmlToWordConverter.ConvertHtmlToWord(html);
        var docxProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
        using var memoryStream = new MemoryStream();
        docxProvider.Export(htmlDocument, memoryStream);
        var documentBytes = memoryStream.ToArray();

        File.WriteAllBytes($"{path}Test.docx", documentBytes);

        var pdfBytes = PdfConverter.ConvertDocxToPdf(documentBytes);
        File.WriteAllBytes($"{{path}}Test.pdf", pdfBytes);
    }
}

public interface IHtmlToWordConverter
{
    RadFlowDocument ConvertHtmlToWord(string html);
}

public class HtmlToWordConverter : IHtmlToWordConverter
{
    public RadFlowDocument ConvertHtmlToWord(string html)
    {
        var htmlFormatProvider = new HtmlFormatProvider();
        return htmlFormatProvider.Import(html);
    }
}

public static class PdfConverter
{
    static PdfConverter()
    {
        var defaultImagePropertiesResolver = new ImagePropertiesResolver();
        Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver =
            defaultImagePropertiesResolver;
    }

    public static byte[] ConvertDocxToPdf(byte[] docxBytes)
    {
        var docxProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
        var pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

        var document = docxProvider.Import(docxBytes);

        using var memoryStream = new MemoryStream();
        pdfProvider.Export(document, memoryStream);
        return memoryStream.ToArray();
    }
}

 

PDF Output:

 

Word Output


Yoan
Telerik team
 answered on 20 Aug 2024
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?