将base64位图插入rtf

时间:2012-12-12 23:13:34

标签: javascript image base64 rtf

我有一张从签名捕获中捕获的图像。 sig cap返回图像的base64字符串。

我想从javascript中创建一个包含此图像的rtf文档字符串。

我在这里:

var datapair = $sigdiv.jSignature("getData");

返回(缩短因为很长时间在SO上发布):

 ["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABNMAAAE2CAYAAABLINTeAAAgAElEQVR4Xu3dCbQtVXkgYPMYRVBAxTi0PkQDmCCgxiESeaBtCK0IGqKJEUzAuUFNtF1Jx/CcVnezYhSMtigYwJjYTgyRaFqNF+chYdBoVEQejgFRURNh4UD/v55qy/Lce88595w6tau+Wutf9wxVe//72/XOved/NfzCLSwECBAgQIAAAQIE2hO4W3S1edTdrvFzn4grI/599NqB8XOPWjor66R2SLy/LeI/phjCA2Ldjy5w/arpW43G+oE1+lptvONsLo92rp8ib6sSIECAAAECCxD4hQW0qUkCQIECBAgQIAAAQIECBBoWUAxrWVw3REgQIAAAQIECBAgQIAAAQIECJQroJhW7tzJnAABAgQIECBAgAABAgQIECBAoGUBxbSWwXVHgAABAgQIECBAgAABAgQIECBQroBiWrlzJ3MCBAgQIECAAAECBAgQIECAAIGWBRTTWgbXHQECBAgQIECAAAECBAgQIECAQLkCimnlzp3MCRAgQIAAAQIECBAgQIAAAQIEWhZQTGsZXHcECBAgQIAAAQIECBAgQIAAAQLlCiimlTt3MidAgAABAgQIECBAgAABAgQIEGhZQDGtZXDdESBAgAABAgQIECBAgAABAgQIlCugmFbu3MmcAAECBAgQIECAAAECBAgQIECgZQHFtJbBdUeAAAECBAgQIECAAAECBAgQIFCugGJauXMncwIECBAgQIAAAQIECBAgQIAAgZYFFNNaBtcdAQIECBAgQIAAAQIECBAgQIBAuQKKaeXOncwJECBAgAABAgQIECBAgAABAgRaFlBMaxlcdwQIECBAgAABAgQIECBAgAABAuUKKKaVO3cyJ0CAAAECBAgQIECAAAECBAgQaFlAMa1lcN0RIECAAAECBAgQIECAAAECBAiUK6CYVu7cyZwAAQIECBAgQIAAAQIECBAgQKBlAcW0lsF1R4AAAQIECBAgQIAAAQIECBAgUK6AYlq5cydzAgQIECBAgAABAgQIECBAgACBlgX+H9F/EpGfbmFGAAAAAElFTkSuQmCC"]

然后我尝试使用以下字符串形成rtf doc。

string rtf = @"{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard ";
            rtf += picture;
            rtf += " }";
            return rtf;

图片在哪里(缩短因为要在SO上发帖很长):

var imageData = "iVBORw0KGgoAAAANSUhEUgAABNMAAAE2CAYAAABLINTeAAAgAElEQVR4Xu3dC9A1ZX0YcMOHYlEI0GhMk9EPiLU6NULEptUqL0irphb5IB0ZIxeVljEtl6ixM5kAH5dMR8dLwMY28cIltdgIcnHUdtTwUmsTdUCZxNRqlA9jvTSZfp/FCyKX/v961i7Led93z3nP2bPP7u/M/OfsOWd3n//ze5b3fOfPs7s/8QgPAgQIECBAgAABAgQIECBAgAABAgRaCfxEq7WsRIAAAQIECBAgQIAAAQIECBAgQIDAIxTTHAQECBAgQIAAAQIECBAgQIAAAQIEWgooprWEshoBAgQIECBAgAABAgQIECBAgAABxTTHAAECBAgQIECAAAECBAgQIECAAIGWAoppLaGsRoAAAQIECBAgQIAAAQIECBAgQEAxzTFAgAABAgQIECBAgAABAgQIECBAoKWAYlpLKKsRIECAAAECBAgQIECAAAECBAgQUExzDBAgQIAAAQIECBAgQIAAAQIECBBoKaCY1hLKagQIECBAgAABAgQIECBAgAABAgQU0xwDBAgQIECAAAECBAgQIECAAAECBFoKKKa1hLIaAQIECBAgQIAAAQIECBAgQIAAAcU0xwABoJjWMbjmCBAgQIAAAQIECBAgQIAAAQIEyhVQTCt37GROgAABAgQIECBAgAABAgQIECDQsYBiWsfgmiNAgAABAgQIECBAgAABAgQIEChXQDGt3LGTOQECBAgQIECAAAECBAgQIECAQMcCimkdg2uOAAECBAgQIECAAAECBAgQIECgXAHFtHLHTuYECBAgQIAAAQIECBAgQIAAAQIdCyimdQyuOQIECBAgQIAAAQIECBAgQIAAgXIFFNPKHTuZEyBAgAABAgQIECBAgAABAgQIdCygmNYxuOYIECBAgAABAgQIECBAgAABAgTKFVBMK3fsZE6AAAECBAgQIECAAAECBAgQINCxgGJax+CaI0CAAAECBAgQIECAAAECBAgQKFdAMa3csZM5AQIECBAgQIAAAQIECBAgQIBAxwKKaR2Da44AAQIECBAgQIAAAQIECBAgQKBcAcW0csdO5gQIECBAgAABAgQIECBAgAABAh0L/D9S+8lzAOsdUQAAAABJRU5ErkJggg==";
var picture = "{\pict\pngblip\picw1693\pich1693\picwgoal960\pichgoal960" + imagedata;

然后我将字符串分配给Rich Text Box(我试图证明rtf文件有效的示例winforms应用程序)。

this.richTextBox1.Rtf = rtf;

没有显示任何内容=(

1 个答案:

答案 0 :(得分:2)

我发现你要做的事情有两个主要问题。 首先,RTF要求媒体是十六进制编码,而不是Base64。因此,首先将图像数据转换为十六进制字符串。然后,您需要更改在RTF中创建图片的方式。一个微小的变化,但它应该有所作为。将您的var图片更改为:

var picture = "{\pict\picw1693\pich1693\picwgoal960\pichgoal960\pngblip" + imagedata;

\ pngblip告诉RTF读者下一张图片数据。