【问题标题】:save Inkcanvas control content as pdf将 Inkcanvas 控件内容保存为 pdf
【发布时间】:2011-07-29 14:19:16
【问题描述】:

我想将 InkCanvas 控件的输出保存为 PDF 文件,我可以将其保存为 .isf 和 .bmp,我想将其保存为 .pdf 如何?

【问题讨论】:

    标签: c# wpf pdf pdf-generation inkcanvas


    【解决方案1】:

    您可以从pdfsharp-codeplex下载PDF库 或pdfsharp.net 并检查图形示例图像部分 所以您可以先将其保存为 .jpg 然后将其保存为 pdf

    【讨论】:

      【解决方案2】:

      //首先创建 Ink Canvas 的多个实例并添加到 List** 中,然后以 PDF 格式保存一个画布一个图像。

      [DebuggerDisplay("[{Scene}]Strokes:{Strokes.Count}, Children:{Children.Count}")]
      public class InkCanvas_SandeepJadhav : InkCanvas
      {
      }
      public List<InkCanvas_SandeepJadhav> listCanvas = new List<InkCanvas_SandeepJadhav>();
      
            public void saveMultiInkCanvasToPdf()
              {
                  string subpath = Directory.GetCurrentDirectory();          
                  SaveFileDialog saveFileDialog12 = new SaveFileDialog();
                  saveFileDialog12.Filter = "Pdf File|*.pdf";
                  saveFileDialog12.Title = "Save Pdf File";
                  saveFileDialog12.InitialDirectory = subpath;
                  saveFileDialog12.ShowDialog();
                  // If the file name is not an empty string open it for saving.  
                  if (saveFileDialog12.FileName == "") return;
                  subpath = saveFileDialog12.FileName.Substring(0, saveFileDialog12.FileName.Length - saveFileDialog12.SafeFileName.Length);
      
                  string extension = saveFileDialog12.FileName.Remove(subpath.IndexOf(subpath), subpath.Length);
                  string[] allStr = extension.Split('.');
      
                  if (allStr[1] == "pdf")
                  {
                      using (var stream = new FileStream(saveFileDialog12.FileName, FileMode.Append, FileAccess.Write, FileShare.None))
                      {
                          iTextSharp.text.Document document = new iTextSharp.text.Document();
                          document.SetPageSize(iTextSharp.text.PageSize.A4);
                          iTextSharp.text.pdf.PdfWriter.GetInstance(document, stream);
                          if (!document.IsOpen())
                              document.Open();
      
                          for (int i = 0; i < listCanvas.Count; i++)
                          {
                              RenderTargetBitmap rtb = new RenderTargetBitmap((int)listCanvas[i].Width, (int)listCanvas[i].Height, 96d, 96d, PixelFormats.Default);
                              rtb.Render(listCanvas[i]);
                              // draw the ink strokes onto the bitmap
                              DrawingVisual dvInk = new DrawingVisual();
                              DrawingContext dcInk = dvInk.RenderOpen();
                              dcInk.DrawRectangle(listCanvas[i].Background, null, new Rect(0d, 0d, listCanvas[i].Width, listCanvas[i].Height));
                              foreach (System.Windows.Ink.Stroke stroke in listCanvas[i].Strokes)
                              {
                                  stroke.Draw(dcInk);
                              }
                              dcInk.Close();
      
                              //save bitmap to file                          
                              MemoryStream fs = new MemoryStream();
                              System.Windows.Media.Imaging.JpegBitmapEncoder encoder1 = new JpegBitmapEncoder();
                              encoder1.Frames.Add(BitmapFrame.Create(rtb));
                              encoder1.Save(fs);
                              byte[] tArr = fs.ToArray();
                              fs.Close();
      
                              iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(tArr);
                              image.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
                              image.ScaleToFit(document.PageSize.Width - 10, document.PageSize.Height - 10);//Resize image depend upon your need
                              document.Add(image);
                              document.NewPage();
                              fs.Close();
                          }
                          document.Close();
                      }
                  }
      
              }
      

      【讨论】:

      • 需要解释!请不要只是复制粘贴代码。
      • 我刚刚回答了如何在一个 pdf 文件中保存多个 inkcanvas。我在 MemoryStream 中渲染了 inkcanvas,然后将 MemoryStream 添加到 Image Object 中,并将每个图像对象写入文档对象中。
      • 如果您遇到任何问题。让我知道。
      • 及以上仅由我创建的整个代码。不要复制粘贴。
      • 感谢您的代码!我是新手,我真的不知道如何在我的项目中尝试您的代码。你能提供一个如何使用它的例子吗?谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多