Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MigraDoc RTF Document does not print Images (png) #11

Closed
dnehl opened this issue May 10, 2023 · 7 comments
Closed

MigraDoc RTF Document does not print Images (png) #11

dnehl opened this issue May 10, 2023 · 7 comments
Labels

Comments

@dnehl
Copy link

dnehl commented May 10, 2023

Resources

Issue.zip
nehl-it-icon

Version

I used the latest preview:
<PackageReference Include="PDFsharp" Version="6.0.0-preview-2" /> <PackageReference Include="PDFsharp-MigraDoc" Version="6.0.0-preview-2" />

Reporting an Issue Here

Expected Behavior

the logo, in this case "nehl-it-icon" should be printed in the pdf and in the rtf document, but it only works with PDF

Actual Behavior

The image is only visible in the pdf document. In Rtf it shows caracters but nothing else.

Steps to Reproduce the Behavior

Start the Project in the Issue.zip file and check the difference between word and pdf document

@ThomasHoevel ThomasHoevel added the investigation Under investigation label May 10, 2023
@ThomasHoevel
Copy link
Member

After a little code change, images in RTF work.
Code page 1252 is not UTF8, so remove this:

//public class MyEncodingProvider : EncodingProvider
//{
//    public override Encoding GetEncoding(int codepage)
//    {
//        if (codepage == 1252)
//        {
//            // Return your custom encoding object here
//            return Encoding.UTF8;
//        }
//        return null;
//    }

//    public override Encoding GetEncoding(string name)
//    {
//        // Implement this method if you want to support encoding names
//        return null;
//    }
//}

Register the encoder for code page 1252 (this should be included in the RTF Renderer soon, but with 6.0.0-preview-2 it must be called explicitly).

        //Encoding.RegisterProvider(new MyEncodingProvider());
        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

@dnehl
Copy link
Author

dnehl commented May 17, 2023

I tried to register the provider in the startup and also before I create the RTF document, but it still did not work. Now the image ins, not there and also not strange symbols.
Do I have to config the runtime env or the project file in a specific way?

@ThomasHoevel
Copy link
Member

I do not know about any special requirements. We just use .NET 6 and I simply call RegisterProvider early in the code before creating the RTF Renderer.

The issue submission template worked with the changes shown above.

@dnehl
Copy link
Author

dnehl commented May 17, 2023

Ok I found the issue, maybe you can help me to solve this as well.
I want to download the image first and add the byte array to the document.
When I create the pdf document everything, but the rtf document does not contain the image.

I am using the following code to download the image and create a byte array.

public static async Task<string> GetImage(string imageName, CancellationToken cancellationToken = default)
    {
        var httpClient = new HttpClient();
        var url = $"{BaseUrl}/nehl-it-icon.png";
        var response = await httpClient.GetAsync(
            url,
            cancellationToken);

        if (!response.IsSuccessStatusCode)
        {
            return string.Empty;
        }

        return LoadImageFromBytes(await response.Content.ReadAsByteArrayAsync(cancellationToken));
    }

    protected static string LoadImageFromBytes(byte[] cData)
    {
        return $"base64:{Convert.ToBase64String(cData)}";
    }

var imageFilePath = await GetImage("Generali.png");

var image = document.LastSection.Headers.Primary.AddImage(imageFilePath);

@ThomasHoevel
Copy link
Member

The "BASE64" notation does not currently work with the RTF Renderer. This is a different issue we are researching.

At the moment I'd recommend storing the image in a temporary file if you have to download it from the web.

@dnehl
Copy link
Author

dnehl commented May 17, 2023

Ok, thanks for the feedback!
Is there an open issue, where I can watch it?

@ThomasHoevel ThomasHoevel added support and removed investigation Under investigation labels May 23, 2023
@ThomasHoevel
Copy link
Member

BASE64 support for RTF Renderer has been fixed.
Fix will be included with version 6.0.0-preview-3 coming soon (May or June 2023 or so).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants