簡體   English   中英

如何在 PDFsharp 中使用資源圖像?

[英]How do I use a Resources Image with PDFsharp?

我正在使用PDFsharp庫對 PDF 文件進行一些簡單的操作。
我有以下代碼將圖像從文件夾復制到現有的PDF文檔中 - 它按預期工作:

public void AddImagePDF()
{
  this.DrawPage(this.PDFdoc.Pages[0]);
  this.DrawPage(this.PDFdoc.Pages[1]);
  this.DrawPage(this.PDFdoc.Pages[2]);
}
private void DrawPage(PdfPage page)
{
  XGraphics gfx = XGraphics.FromPdfPage(page);
  DrawPng(gfx);
}
private void DrawPng(XGraphics gfx)
{

  XImage imageMu = XImage.FromFile(@"C:\Images\AnImage.png");
  double width = imageMu.PixelWidth * 7.0 / imageMu.HorizontalResolution;
  double height = imageMu.PixelHeight * 7.0 / imageMu.HorizontalResolution;
  gfx.DrawImage(imageMu,500,30,width,height); 

  this.PDFdoc.Save(this.DestinationFullPath);
}

為了使解決方案更具便攜性,我已將圖像文件AnImage.png移動到項目資源中 - 此處:

Properties.Resources.AnImage

但是為了使用資源文件而不是保存在 C 驅動器中的文件,我需要對代碼進行哪些更改?

您可以在獲取圖像資源的流后使用XImage.FromStream

順便說一句:如果您只創建一次XImage並將其用於所有頁面,它會更有效率並且可能會創建更小的 PDF。

您可以使用FromGdiPlusImagepdfsharp方法,如下所示:

XImage imageMu = XImage.FromGdiPlusImage(Properties.Resources.AnImage);

答案來自另一篇 Stack Overflow 帖子,答案在:

如何在.NET中獲取Properties.Resources.Image的路徑

如果正如您在評論中所說,您不能使用FromGdiPlusImage則可以選擇將其作為流加載,這是從另一篇 Stack Overflow 帖子中提取的:

System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("Properties.Resources.AnImage.png");
Ximage yourImage = XImage.FromStream(file);

從 C# 中項目的資源區域加載圖像- David Icardi 的回答

將資源轉換為字節:

            using System;
            using System.Drawing;
            using System.IO;
            using ITNETAWF.Properties; //WinForms Project

            using PDFDocument = MigraDoc.DocumentObjectModel.Document;
            using PDFImage = MigraDoc.DocumentObjectModel.Shapes.Image;
            using PDFRelativeHorizontal = MigraDoc.DocumentObjectModel.Shapes.RelativeHorizontal;
            using PDFRelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical;
            using PDFSection = MigraDoc.DocumentObjectModel.Section;
            using PDFShapePosition = MigraDoc.DocumentObjectModel.Shapes.ShapePosition;
            using PDFWrapStyle = MigraDoc.DocumentObjectModel.Shapes.WrapStyle;


            namespace PdfSharpImage
            {
                public class Class1
                {
                    private void CreatePage()
                    {
                        byte[] zbytData;
                        Bitmap zbmpData;
                        string zstrDataB64;
                        MemoryStream zmstFlujoMemoria;
                        PDFDocument document;
                        PDFImage image;
                        PDFSection section;


                        // Each MigraDoc document needs at least one section.
                        document = new PDFDocument();
                        section = document.AddSection();

                        // Put a logo in the header
                        //---------------------------------------------------------------------------------------------------------------------------------------------------------------//
                        //  Logo MFG 225x32.png is an image in the .resx file 
                        //  Logo MFG 225x32.png  width/height=107/32 pixel  width/height=2.83/0.85 cm  width/height=1.11/0.33 inch
                        //  
                        //  Resources.resx file Code:
                        //  <data name="Logo MFG 225x32" type="System.Resources.ResXFileRef, System.Windows.Forms">
                        //    <value>..\Resources\Logo MFG 225x32.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
                        //  </data>
                        //---------------------------------------------------------------------------------------------------------------------------------------------------------------//
                        zbmpData = Resources.ResourceManager.GetObject("Logo MFG 225x32") as Bitmap;
                        zbytData = new byte[13750]; // 13750 - The size in bytes of the Image
                        zmstFlujoMemoria = new MemoryStream(zbytData);
                        zbmpData.Save(zmstFlujoMemoria, System.Drawing.Imaging.ImageFormat.Bmp);
                        zstrDataB64 = String.Format("base64:{0}", Convert.ToBase64String(zbytData));
                        //image = section.Headers.Primary.AddImage("../../PowerBooks.png");
                        image = section.Headers.Primary.AddImage(zstrDataB64);
                        //image.Height = "2.5cm";
                        image.Height = "1.11cm";
                        image.LockAspectRatio = true;
                        image.RelativeVertical = PDFRelativeVertical.Line;
                        image.RelativeHorizontal = PDFRelativeHorizontal.Margin;
                        image.Top = PDFShapePosition.Top;
                        image.Left = PDFShapePosition.Right;
                        image.WrapFormat.Style = PDFWrapStyle.Through;
                    }
                }
            }
            // http://www.pdfsharp.net/wiki/Invoice-sample.ashx
            // http://www.pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM