iText和iTextSharp中的Document
對象是一種自動處理各種間距,填充和邊距的抽象。對您而言不幸的是,這也意味着當您撥打doc.Add()
時,它會考慮到文檔的現有邊距。 (另外,如果你碰巧添加其他圖像將相對於添加了。)
一個解決辦法是隻刪除邊距:
doc.SetMargins(0, 0, 0, 0);
相反,它更容易添加圖像直接轉到PdfWriter
對象,您可以通過撥打PdfWriter.GetInstance()
來獲得該對象。您目前正在扔掉,而不是存儲該對象,但你可以很容易地改變你的線路:
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(saveFileDialog1.FileName,FileMode.Create));
然後你就可以訪問PdfWriter
的DirectContent
屬性並調用其AddImage()
方法:
writer.DirectContent.AddImage(img);
在這樣做之前,您還必須通過調用絕對定位圖像:
img.SetAbsolutePosition(0, 0);
下面是一個完整的C#2010 Wi針對iTextSharp 5.1.1.0的nForms應用程序顯示上面的DirectContent
方法。它動態地創建兩個不同大小的圖像,兩個紅色箭頭橫向和縱向都伸展。你的代碼顯然只是使用標準的圖像加載,因此可以忽略很多這個,但我想提供一個完整的工作示例。有關更多詳細信息,請參閱代碼中的註釋。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
//File to write out
string outputFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Images.pdf");
//Standard PDF creation
using (FileStream fs = new FileStream(outputFilename, FileMode.Create, FileAccess.Write, FileShare.None)) {
//NOTE, we are not setting a document size here at all, we'll do that later
using (Document doc = new Document()) {
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
//Create a simple bitmap with two red arrows stretching across it
using (Bitmap b1 = new Bitmap(100, 400)) {
using (Graphics g1 = Graphics.FromImage(b1)) {
using(Pen p1 = new Pen(Color.Red,10)){
p1.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
p1.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
g1.DrawLine(p1, 0, b1.Height/2, b1.Width, b1.Height/2);
g1.DrawLine(p1, b1.Width/2, 0, b1.Width/2, b1.Height);
//Create an iTextSharp image from the bitmap (we need to specify a background color, I think it has to do with transparency)
iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(b1, BaseColor.WHITE);
//Absolutely position the image
img1.SetAbsolutePosition(0, 0);
//Change the page size for the next page added to match the source image
doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, b1.Width, b1.Height, 0));
//Add a new page
doc.NewPage();
//Add the image directly to the writer
writer.DirectContent.AddImage(img1);
}
}
}
//Repeat the above but with a larger and wider image
using (Bitmap b2 = new Bitmap(4000, 1000)) {
using (Graphics g2 = Graphics.FromImage(b2)) {
using (Pen p2 = new Pen(Color.Red, 10)) {
p2.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
p2.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
g2.DrawLine(p2, 0, b2.Height/2, b2.Width, b2.Height/2);
g2.DrawLine(p2, b2.Width/2, 0, b2.Width/2, b2.Height);
iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(b2, BaseColor.WHITE);
img2.SetAbsolutePosition(0, 0);
doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, b2.Width, b2.Height, 0));
doc.NewPage();
writer.DirectContent.AddImage(img2);
}
}
}
doc.Close();
}
}
}
this.Close();
}
}
}
謝謝 - 聽起來比原來的解決方案好得多。你有什麼想法這個方法可以處理的最大圖像大小是多少? – SharpAffair 2012-01-04 15:54:22
根據PDF規範(附錄C第2節),符合1.6的PDF的最小尺寸爲3x3,最大尺寸爲14,400x14,4000。請注意,這些尺寸以「默認用戶空間中的單位」表示,如果您不更改,則爲1/72英寸。通常最好將單位看作像素。如果你想了解更多關於「用戶空間」和「單位」的信息,請參閱這篇文章:http://stackoverflow.com/a/8245450/231316 – 2012-01-04 16:15:15
乍一看,似乎工作完美無缺。您的文章非常有幫助!謝謝! – SharpAffair 2012-01-04 20:23:14