如何使用PDFsharp将动态生成的位图插入PDF文档?

3

我正在尝试使用PDFsharp将动态生成的QR码位图插入到PDF文档中。 我不想将位图保存到文件中,而只想将其插入到PDF中。 我遇到的问题是DrawImage命令需要一个字符串来定位图像文件。 但我不想保存文件,我只想将其插入到PDF文档中。 是否有一种方法可以做到这一点?

var QRCode_BMP = _generalCode.QR_CodeGenerator(AddReviewPath); //This generates the bitmap
MemoryStream streamQR = new MemoryStream();
QRCode_BMP.Save(streamQR, System.Drawing.Imaging.ImageFormat.Jpeg); //save bitmap into memory stream in jpeg format System.Drawing.Image QR_Jpeg = System.Drawing.Image.FromStream(streamQR);// save memory stream to image file
XImage xImage = XImage.FromGdiPlusImage(QR_Jpeg);
gfx = XGraphics.FromPdfPage(page);

DrawImage(gfx, xImage, 0, 0, 100, 100); //This is not working

QRCode_BMP.Dispose();
streamQR.Close();
gfx.Dispose();

请提供更多的信息,而不仅仅是“这个不起作用”。 - I liked the old Stack Overflow
DrawImage命令实际上不是在寻找xImage,而是需要一个文件位置的字符串。正如我上面提到的,我不想将文件保存到磁盘。如果您有任何想法,肯定会受到赞赏。 - user2789697
使用正确的DrawImage()方法会有所帮助。尝试使用gfx.DrawImage(...)并传递您的xImage。 - I liked the old Stack Overflow
3个回答

2
这是我让它工作的方式;
        PdfDocument pdf = PdfGenerator.GeneratePdf("<b>some html here</b>", PageSize.A4);

        QRCodeGenerator qrGenerator = new QRCodeGenerator();
        QRCodeData qrCodeData = qrGenerator.CreateQrCode("some text here", QRCodeGenerator.ECCLevel.Q);
        QRCode qrCode = new QRCode(qrCodeData);

        Bitmap qrCodeImage = qrCode.GetGraphic(10);


        PdfPage page = pdf.Pages[0]; //I will add it to 1st page

        // Get an XGraphics object for drawing
        XGraphics gfx = XGraphics.FromPdfPage(page);

        XImage image = XImage.FromGdiPlusImage(qrCodeImage); //you can use XImage.FromGdiPlusImage to get the bitmap object as image (not a stream)
        gfx.DrawImage(image, 50, 50, 150, 150);
//save your pdf, dispose other objects

1
XImage.FromGdiPlusImage不存在。 - JustMe
如果您想使用特定于GDI +的方法(如XImage.FromGdiPlusImage()),则必须使用PDFsharp的GDI +版本构建。 - Grault
您可以使用PdfSharp 1.50中提供的XImage.FromStream()。只需创建一个MemoryStream并用qrCodeImage.Save(qrImageStream, ImageFormat.Png)填充即可。 - Benjamin Freitag

1
你可以使用 XImage.FromStream 来使用创建的流。在此之前,你需要使用 QRCode_BMP 创建一个二维码,并从 QR_Jpeg 创建一个 XImage,但是由于代码中没有指出 QR_Jpeg 的来源,因此无法正常工作。
另外,值得注意的是,在 QR 代码中,JPEG 不是一个好的选择,建议使用 BMP 格式,并且 PDFsharp 将使用无损压缩。请提供一个完整的样例程序。

0

使用像@I liked the old Stack Overflow中提到的XImage.FromStream,在您发布的代码中,您应该能够直接使用:

var QRCode_BMP = _generalCode.QR_CodeGenerator(AddReviewPath); //This generates the bitmap
MemoryStream streamQR = new MemoryStream();
QRCode_BMP.Save(streamQR, System.Drawing.Imaging.ImageFormat.Jpeg); //save bitmap into memory stream in jpeg format System.Drawing.Image QR_Jpeg = System.Drawing.Image.FromStream(streamQR);// save memory stream to image file
//XImage xImage = XImage.FromGdiPlusImage(QR_Jpeg); // <-- Removed
gfx = XGraphics.FromPdfPage(page);
gfx.DrawImage(XImage.FromStream(streamQR), 0, 0, 100, 100); // <-- Added

//DrawImage(gfx, xImage, 0, 0, 100, 100); //This is not working // <-- Removed

QRCode_BMP.Dispose();
streamQR.Close();
gfx.Dispose();

关于使用流进行 XGraphics.DrawImage(PDFSharp库)的一般用法,这里是我用来将SVG矢量路径数据打印到PDF的一些代码:

System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();

// YouTube like button SVG vector path data:
path.Data = Geometry.Parse("M12.42,14A1.54,1.54,0,0,0,14,12.87l1-4.24C15.12,7.76,15,7,14,7H10l1.48-3.54A1.17,1.17,0,0,0,10.24,2a1.49,1.49,0,0,0-1.08.46L5,7H1v7ZM9.89,3.14A.48.48,0,0,1,10.24,3a.29.29,0,0,1,.23.09S9,6.61,9,6.61L8.46,8H14c0,.08-1,4.65-1,4.65a.58.58,0,0,1-.58.35H6V7.39ZM2,8H5v5H2Z");
// Visual check of the path:
// https://yqnn.github.io/svg-path-editor/

// Color area bordered through path of SVG vector:
path.Fill = new SolidColorBrush(Colors.Black);

// Upscale path, if final image is blurry:
double scale = 2;
path.RenderTransform = new ScaleTransform(scale, scale);

Rect bounds = path.Data.GetRenderBounds(null);

// Increase render bounds (here: "+ 4"), if parts of the path in the final image are cut off (test it with "scale = 1" before upscaling):
bounds.Width = (bounds.Width + 4) * scale;
bounds.Height = (bounds.Height + 4) * scale;

path.Measure(bounds.Size);
path.Arrange(bounds);

RenderTargetBitmap bitmap = new RenderTargetBitmap(
    (int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(path);

// Transparent areas are replaced with black pixels when using a "BmpBitmapEncoder", so instead use a "PngBitmapEncoder":
PngBitmapEncoder encoderPng = new PngBitmapEncoder();
encoderPng.Frames.Add(BitmapFrame.Create(bitmap));
using (MemoryStream stream = new MemoryStream())
{
    encoderPng.Save(stream);
    
    // Draw image to PDF using "XGraphics.DrawImage":
    gfx.DrawImage(XImage.FromStream(stream), 10, 100, 14.05, 12.05);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接