We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy paste text not working in PDF create by Aspose.html 22.2

Copy-paste text not working in PDF file created by Aspose.html version 22.2 using function
Aspose.Html.Converters.Converter.ConvertHTML(document, pdfSaveOptions, streamProvider);

The text is shown correctly in the PDF file, but in paste is shown like this :􀀂􀀃􀀂􀀄􀀃􀀂􀀄􀀃􀀂􀀄􀀃􀀄􀀂􀀃􀀃

My code:

using System;
using System.Configuration;
using System.IO;
using System.Linq;

using Aspose.Html;

{
public class PdfAsposeService : IPdfAsposeService
{

    public byte[] GenerateReceipt(string html, Receipt receipt, int pdfPageWidth, int pdfPageHeight)
    {
        return ConvertHtmlToPdfUsingAspose(html, receipt.LeftMargin, receipt.RightMargin, receipt.TopMargin, receipt.BottomMargin, pdfPageWidth, pdfPageHeight);
    }

    private static byte[] ConvertHtmlToPdfUsingAspose(string html, int leftMargin, int rightMargin, int topMargin, int bottomMargin, int pdfPageWidth, int pdfPageHeight)
    {
        try
        {
            if (pdfPageWidth == 0)
            {
                pdfPageWidth = 297;
            }
            if (pdfPageHeight == 0)
            {
                pdfPageHeight = 210;
            }
            var asposeLicense = ConfigurationManager.AppSettings["AsposeLicense"];
            if (string.IsNullOrEmpty(asposeLicense) || !File.Exists(asposeLicense))
            {
                throw new Exception($"AsposeLicense not found");
            }

            License htmlLicense = new Aspose.Html.License();
            htmlLicense.SetLicense(asposeLicense);
            using (var streamProvider = new MemoryStreamProvider())
            {
                using (var document = new HTMLDocument(html, "."))
                {
                    var pdfSaveOptions = new Aspose.Html.Saving.PdfSaveOptions();
                    pdfSaveOptions.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(Aspose.Html.Drawing.Unit.FromMillimeters(pdfPageWidth), Aspose.Html.Drawing.Unit.FromMillimeters(pdfPageHeight)),
                        new Aspose.Html.Drawing.Margin(leftMargin, rightMargin, topMargin, bottomMargin));
                    Aspose.Html.Converters.Converter.ConvertHTML(document, pdfSaveOptions, streamProvider);
                    var memory = streamProvider.Streams.First();
                    memory.Seek(0, System.IO.SeekOrigin.Begin);
                    return memory.ToArray();
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Log(EntryType.Error, ex);
            throw;
        }
    }
}

class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
{
    // List of MemoryStream objects created during the document rendering
    public System.Collections.Generic.List<MemoryStream> Streams { get; } = new System.Collections.Generic.List<MemoryStream>();
    public Stream GetStream(string name, string extension)
    {
        // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats.
        MemoryStream result = new MemoryStream();
        Streams.Add(result);
        return result;
    }
    public Stream GetStream(string name, string extension, int page)
    {
        // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
        MemoryStream result = new MemoryStream();
        Streams.Add(result);
        return result;
    }
    public void ReleaseStream(Stream stream)
    {
        //  Here you can release the stream filled with data and, for instance, flush it to the hard-drive
    }
    public void Dispose()
    {
        // Releasing resources
        foreach (var stream in Streams)
            stream.Dispose();
    }
}

}

@rezgar.rashid

Can you please share the sample HTML in .zip format with us? We will test the scenario in our environment and address it accordingly.

Hi Asad

Thanks

The HTML is: “’<p><font face=‘Arial’>ouoiuoiuoiuiouu</font></p>”
And called from this function

public void ConvertToPDF_test()
{
Receipt receipt = new Receipt();

        byte[] buffer = _signingService.GenerateReceipt("<p><font face='Arial'>ouoiuoiuoiuiouu</font></p>", receipt, 297, 210);

        string filepath = @"c:\temp\mygeneratedPDF.pdf";
        if (File.Exists(filepath))
        {
            File.Delete(filepath);
        }

        Encoding encoding = Encoding.GetEncoding("ISO-8859-1"); //Or any other Encoding


        using (FileStream fs = new FileStream(filepath, FileMode.CreateNew))
        {
            using (StreamWriter writer = new StreamWriter(fs, encoding))
            {
                fs.Write(buffer, 0, buffer.Length);
            }
        }

        //FileStream fs = new FileStream(filepath, FileMode.Create);
       
        //fs.Close();
       
    }

Hi Asad

Thanks

The HTML is: “<p><font face=‘Arial’>ouoiuoiuoiuiouu</font></p>”

And called from this function

public void ConvertToPDF_test()

{

Receipt receipt = new Receipt();

byte[] buffer = _signingService.GenerateReceipt(“

ouoiuoiuoiuiouu

”, receipt, 297, 210);

string filepath = @“c:\temp\mygeneratedPDF.pdf”;

if (File.Exists(filepath))

{

File.Delete(filepath);

}

Encoding encoding = Encoding.GetEncoding(“ISO-8859-1”); //Or any other Encoding

using (FileStream fs = new FileStream(filepath, FileMode.CreateNew))

{

using (StreamWriter writer = new StreamWriter(fs, encoding))

{

fs.Write(buffer, 0, buffer.Length);

}

}

//FileStream fs = new FileStream(filepath, FileMode.Create);

//fs.Close();

}

image002.png (4.32 KB)

@rezgar.rashid

We could not find any Aspose.HTML related code in your responses. Can you please share the actual code that you are using to generate PDF from HTML with Aspose.HTML for .NET?

Hi Asad
Codes that calling Aspose.HTML:

using System;
using System.Configuration;
using System.IO;
using System.Linq;
using Traen.TAS.Domain.TAS;
using Traen.TAS.Service.TAS.Interfaces;
using Aspose.Html;
using Traen.TAS.Common.Logging;
namespace Traen.TAS.Service.TAS
{
public class PdfAsposeService : IPdfAsposeService
{

    public byte[] GenerateReceipt(string html, Receipt receipt, int pdfPageWidth, int pdfPageHeight)
    {
        return ConvertHtmlToPdfUsingAspose(html, receipt.LeftMargin, receipt.RightMargin, receipt.TopMargin, receipt.BottomMargin, pdfPageWidth, pdfPageHeight);
    }

    private static byte[] ConvertHtmlToPdfUsingAspose(string html, int leftMargin, int rightMargin, int topMargin, int bottomMargin, int pdfPageWidth, int pdfPageHeight)
    {
        try
        {
            if (pdfPageWidth == 0)
            {
                pdfPageWidth = 297;
            }
            if (pdfPageHeight == 0)
            {
                pdfPageHeight = 210;
            }
            var asposeLicense = ConfigurationManager.AppSettings["AsposeLicense"];
            if (string.IsNullOrEmpty(asposeLicense) || !File.Exists(asposeLicense))
            {
                throw new Exception($"AsposeLicense not found");
            }

            License htmlLicense = new Aspose.Html.License();
            htmlLicense.SetLicense(asposeLicense);
            using (var streamProvider = new MemoryStreamProvider())
            {
                using (var document = new HTMLDocument(html, "."))
                {
                    var pdfSaveOptions = new Aspose.Html.Saving.PdfSaveOptions();
                    pdfSaveOptions.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(Aspose.Html.Drawing.Unit.FromMillimeters(pdfPageWidth), Aspose.Html.Drawing.Unit.FromMillimeters(pdfPageHeight)),
                        new Aspose.Html.Drawing.Margin(leftMargin, rightMargin, topMargin, bottomMargin));
                    Aspose.Html.Converters.Converter.ConvertHTML(document, pdfSaveOptions, streamProvider);
                    var memory = streamProvider.Streams.First();
                    memory.Seek(0, System.IO.SeekOrigin.Begin);
                    return memory.ToArray();
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Log(EntryType.Error, ex);
            throw;
        }
    }
}

class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
{
    // List of MemoryStream objects created during the document rendering
    public System.Collections.Generic.List<MemoryStream> Streams { get; } = new System.Collections.Generic.List<MemoryStream>();
    public Stream GetStream(string name, string extension)
    {
        // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats.
        MemoryStream result = new MemoryStream();
        Streams.Add(result);
        return result;
    }
    public Stream GetStream(string name, string extension, int page)
    {
        // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
        MemoryStream result = new MemoryStream();
        Streams.Add(result);
        return result;
    }
    public void ReleaseStream(Stream stream)
    {
        //  Here you can release the stream filled with data and, for instance, flush it to the hard-drive
    }
    public void Dispose()
    {
        // Releasing resources
        foreach (var stream in Streams)
            stream.Dispose();
    }
}

}

@rezgar.rashid

We are checking it and will get back to you shortly.