我已經開發了我的開發機器上的解決方案,它:您是否需要在服務器上安裝Adobe PDF才能使用iTextSharp?
- 通過C#
- 將它們合併在一起
- 做了
Response.BinaryWrite
推到瀏覽器的合併打開PDF文件的文件路徑服務器端PDF
對本地DEV很有效。當推送到服務器時,瀏覽器窗口中會出現一些「二進制亂碼」。
Adobe或Foxit Reader未安裝在服務器上,但它安裝在本地開發機器上。我的理解是,iTextSharp允許你不需要安裝PDF閱讀器,但它呢?或者,也許這就是.PDF未列出的文件類型的IIS的事情......
下面是一些示例代碼:
// First set up the response and let the browser know a PDF is coming
context.Response.Buffer = true;
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "inline");
List<string> PDFs = new List<string>();
PDFs.Add(@"c:\users\shane\documents\visual studio 2010\Projects\PDFMultiPrintTester\PDFMultiPrintTester\TEST1.pdf");
PDFs.Add(@"c:\users\shane\documents\visual studio 2010\Projects\PDFMultiPrintTester\PDFMultiPrintTester\TEST2.pdf");
PDFs.Add(@"c:\users\shane\documents\visual studio 2010\Projects\PDFMultiPrintTester\PDFMultiPrintTester\TEST3.pdf");
// Second, some setup stuff
System.IO.MemoryStream MemStream = new System.IO.MemoryStream();
iTextSharp.text.Document doc = new iTextSharp.text.Document();
iTextSharp.text.pdf.PdfReader reader = default(iTextSharp.text.pdf.PdfReader);
int numberOfPages = 0;
int currentPageNumber = 0;
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, MemStream);
doc.Open();
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
iTextSharp.text.pdf.PdfImportedPage page = default(iTextSharp.text.pdf.PdfImportedPage);
int rotation = 0;
foreach (string f in PDFs)
{
// Third, append all the PDFs--THIS IS THE MAGIC PART
byte[] sqlbytes = null;
sqlbytes = ReadFile(f);
reader = new iTextSharp.text.pdf.PdfReader(sqlbytes);
numberOfPages = reader.NumberOfPages;
currentPageNumber = 0;
while ((currentPageNumber < numberOfPages))
{
currentPageNumber += 1;
doc.SetPageSize(PageSize.LETTER);
doc.NewPage();
page = writer.GetImportedPage(reader, currentPageNumber);
rotation = reader.GetPageRotation(currentPageNumber);
if ((rotation == 90) | (rotation == 270))
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(currentPageNumber).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
}
// Finally Spit the stream out
if (MemStream == null)
{
context.Response.Write("No Data is available for output");
}
else
{
doc.Close();
context.Response.BinaryWrite(MemStream.GetBuffer());
context.Response.End();
MemStream.Close();
}
}
}
public static byte[] ReadFile(string filePath)
{
byte[] buffer;
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
finally
{
fileStream.Close();
}
return buffer;
}
服務器請求的URI是什麼? – SliverNinja 2012-02-28 20:26:07
我將所有這些邏輯放在一個.ashx文件中,所以我只需導航到該頁面。例如,它看起來像這樣:http:// localhost:13215/PDFPrint.ashx – Shane 2012-02-28 20:31:36
什麼瀏覽器+版本你看到亂碼? – SliverNinja 2012-02-28 20:33:57