iText 및 iTextSharp의 객체는 자동으로 다양한 간격, 패딩 및 여백을 처리하는 추상화입니다. 불행히도 이것은 귀하가 doc.Add()
으로 전화 할 때 문서의 기존 여백을 고려한다는 것을 의미합니다. (또한, 당신도 다른 이미지가 상대적 추가됩니다 아무것도 추가 발생합니다.)
이 하나 개의 솔루션 그냥 여백 제거하는 것입니다
대신
doc.SetMargins(0, 0, 0, 0);
를, 그것은을 더욱 쉽게 추가 할 수 이미지를 PdfWriter.GetInstance()
으로 전화하여 얻은 PdfWriter
개체로 직접 전송하십시오. 현재 버리고 그 객체를 저장하지 않는 만에 당신은 쉽게 라인을 변경할 수있어 :
writer.DirectContent.AddImage(img);
: 당신은 AddImage()
방법을 PdfWriter
의 DirectContent
속성에 액세스하고 호출 할 수 있습니다 그리고
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(saveFileDialog1.FileName,FileMode.Create));
이 작업을 수행하기 전에 다음을 호출하여 이미지의 위치를 지정해야합니다.
img.SetAbsolutePosition(0, 0);
다음은 전체 작동 C# 2010 Wi 위의 DirectContent
방법을 보여주는 nForms 앱 iTextSharp 5.1.1.0을 타겟팅합니다. 동적으로 두 개의 빨간색 화살표를 사용하여 크기가 다른 두 개의 이미지를 세로 및 가로로 늘입니다. 코드는 분명히 표준 이미지 로딩을 사용하므로 많은 것을 생략 할 수는 있지만 완전한 예제를 전달하고자했습니다. 자세한 내용은 코드의 메모를 참조하십시오.
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
PDF 규격 (부록 C 섹션 2)에 따르면 1.6 규격 PDF의 최소 크기는 3x3이고 최대 크기는 14,400x14,4000입니다. 이 크기는 "기본 사용자 공간의 단위"에 있습니다. 변경하지 않으면 1/72 인치입니다. 일반적으로 픽셀 단위로 단위를 생각하는 것이 가장 좋습니다. "사용자 공간"과 "단위"에 대해 좀 더 배우고 싶다면이 글을 참고하십시오 : http://stackoverflow.com/a/8245450/231316 –
언뜻보기에, 흠 잡을 데없이 일하는 것 같습니다. 귀하의 게시물은 대단히 도움이됩니다! 감사! – SharpAffair