我如何將Windows窗體轉換爲與表單上相同設計的pdf(pdf文檔)格式將Windows窗體轉換爲pdf文件
1
A
回答
2
我測試了這個,它的工作原理。我不喜歡它創建臨時文件的事實。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
private System.IO.Stream streamToPrint;
string streamType;
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt
(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = image.Width;
int height = image.Height;
if ((width/e.MarginBounds.Width) > (height/e.MarginBounds.Height))
{
width = e.MarginBounds.Width;
height = image.Height * e.MarginBounds.Width/image.Width;
}
else
{
height = e.MarginBounds.Height;
width = image.Width * e.MarginBounds.Height/image.Height;
}
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
}
private void button1_Click(object sender, EventArgs e)
{
String filename = System.IO.Path.GetTempFileName();
Graphics g1 = this.CreateGraphics();
Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
MyImage.Save(filename, ImageFormat.Png);
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
StartPrint(fileStream, "Image");
fileStream.Close();
if (System.IO.File.Exists(filename))
{
System.IO.File.Delete(filename);
}
}
public void StartPrint(Stream streamToPrint, string streamType)
{
this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
this.streamToPrint = streamToPrint;
this.streamType = streamType;
System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
PrintDialog1.AllowSomePages = true;
PrintDialog1.ShowHelp = true;
PrintDialog1.Document = printDocument1;
DialogResult result = PrintDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();
}
}
public Form1()
{
InitializeComponent();
}
}
}
0
0
這裏可能同樣的問題被問過,有這個問題的一些有用的提醒:Save WinForm to PDF & print multipage WinForm
相關問題
- 1. 如何將Windows窗體轉換爲可執行文件
- 2. 使用PdfSharp將C#中的窗體轉換爲PDF使用PdfSharp轉換爲PDF
- 3. 轉換Windows窗體到Windows Metro窗體
- 4. 將html文件轉換爲PDF文件?
- 5. 將.jrxml文件轉換爲.pdf文件
- 6. 如何將Windows窗體保存爲pdf
- 7. 將pdf文件轉換爲word文檔
- 8. 將文本文件轉換爲pdf
- 9. 在Windows服務器上將Word文檔文件轉換爲PDF
- 10. 操作PDF文件,Windows窗體C#
- 11. 將Windows文件轉換爲Linux文件
- 12. 將文本轉換爲PDF
- 13. 將pdf轉換爲文本
- 14. 將文檔轉換爲pdf
- 15. 如何將Windows窗體庫轉換爲Windows手機庫
- 16. 將xml文件轉換爲pdf c#
- 17. 將HTML文件轉換爲PDF
- 18. 將pdf轉換爲word doc文件
- 19. PHP:將html文件轉換爲pdf
- 20. 如何將gp4文件轉換爲pdf
- 21. codeigniter將excel文件轉換爲pdf
- 22. 如何將.CATDrawing文件轉換爲.pdf
- 23. 將可打印文件轉換爲PDF
- 24. 將C#錶轉換爲PDF文件
- 25. 將給定的文件轉換爲PDF
- 26. 將jpeg圖片轉換爲pdf文件
- 27. 將android佈局轉換爲PDF文件
- 28. 可能將HTM文件轉換爲PDF
- 29. 將doc文件轉換爲PDF
- 30. pdfbox將圖像轉換爲pdf文件
這造成.XPS格式及pdf格式不和 – MayureshP 2011-01-27 07:36:22