none
silverPDF - Silverlight PDF library如何支持中文? RRS feed

  • 问题

  • 我试了试silverPDF - Silverlight PDF library,

    http://silverpdf.codeplex.com/

    Project Description
    silverPDF is a Silverlight library that makes it easier for developers to create PDF files on the client side.

    silverPDF is sponsored by Aleyant Systems (http://www.aleyant.com)

    silverPDF is based on two excellent open source projects: PdfSharp (http://www.pdfsharp.net) and iTextSharp (http://itextsharp.sourceforge.net)


    感觉挺好,可惜它不支持CJK字体,如何解决?

    我想可以先退而采用这样的办法:

    先把有关CJK部分的内容,采用WriteableBitmap动态产生图片形式,然后再把图片放到PDF文件中去。


    private WriteableBitmap RenderedTextBitmap(string text, double fontSize)
            {
                // setup the textblock we will render to a bitmap
                TextBlock txt1 = new TextBlock();
                txt1.Text = text;
                txt1.FontSize = fontSize; // set the font size before using the Actual Width / Height

                // create our first bitmap we will render to
                WriteableBitmap bitmap = new WriteableBitmap((int)txt1.ActualWidth,
                    (int)txt1.ActualHeight);

                // put a black textblock under the white one to create a simple dropshadow
                txt1.Foreground = new SolidColorBrush(Colors.Black);
                bitmap.Render(txt1, new TranslateTransform() { X = -2, Y = -2 });

                txt1.Foreground = new SolidColorBrush(Colors.White);
                bitmap.Render(txt1, new TranslateTransform());

                // invalidate the bitmap so that it is rendered
                bitmap.Invalidate();

                return bitmap;

            }


    private static void SaveToFile(WriteableBitmap bitmap, Stream fs)
            {
                int width = bitmap.PixelWidth;
                int height = bitmap.PixelHeight;
                int bands = 3;
                byte[][,] raster = new byte[bands][,];


                for (int i = 0; i < bands; i++)
                {
                    raster[i] = new byte[width, height];
                }

                for (int row = 0; row < height; row++)
                {
                    for (int column = 0; column < width; column++)
                    {
                        int pixel = bitmap.Pixels[width * row + column];
                        raster[0][column, row] = (byte)(pixel >> 16);
                        raster[1][column, row] = (byte)(pixel >> 8);
                        raster[2][column, row] = (byte)pixel;
                    }
                }

                ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
                FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);

                //Encode the Image as a JPEG
                MemoryStream stream = new MemoryStream();
                FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
                encoder.Encode();

                //Back to the start
                stream.Seek(0, SeekOrigin.Begin);

                //Get teh Bytes and write them to the stream
                byte[] binaryData = new Byte[stream.Length];
                long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
                fs.Write(binaryData, 0, binaryData.Length);

            }

    void DrawImage(XGraphics gfx, int number)
            {
                BeginBox(gfx, number, "DrawImage (original)");

                XImage image = FromFile(jpegSamplePath);


                MemoryStream fs=new MemoryStream();
                SaveToFile(RenderedTextBitmap("waspcn上海", 16), fs);
                XImage image = PdfSharp.Drawing.XImage.FromStream(fs);


                // Left position in point
                double x = (250 - image.PixelWidth * 72 / image.HorizontalResolution) / 2;
                gfx.DrawImage(image, x, 0);

                EndBox(gfx);
            }

    编译通过,但是运行出错!

    哪位高手看看,问题出在哪里?
    2010年2月2日 14:04

答案

全部回复