你需要在服务器上安装Adobe PDF以使用iTextSharp吗?
我在开发机器上开发了一个解决方案:
Response.BinaryWrite
推送到合并的PDF浏览器 在本地DEV上效果很好。 当推送到服务器时,浏览器窗口中会出现一些“二进制乱码”。
Adobe或Foxit Reader未安装在服务器上,但它安装在本地开发机器上。 我的理解是,iTextSharp允许你不需要安装PDF阅读器,但它呢? 或者,也许这是一个IIS的东西.pdf不列为文件类型...
以下是一些示例代码:
// 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:usersshanedocumentsvisual studio 2010ProjectsPDFMultiPrintTesterPDFMultiPrintTesterTEST1.pdf");
PDFs.Add(@"c:usersshanedocumentsvisual studio 2010ProjectsPDFMultiPrintTesterPDFMultiPrintTesterTEST2.pdf");
PDFs.Add(@"c:usersshanedocumentsvisual studio 2010ProjectsPDFMultiPrintTesterPDFMultiPrintTesterTEST3.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;
}
我的理解是,iTextSharp允许你不需要安装PDF阅读器,但它呢?
iTextSharp用于生成PDF文件。 它与浏览这些文件的方式无关。 如果您在浏览应用程序的客户端计算机上没有安装PDF阅读器,则此响应中的PDF文件不会在此客户端计算机上获得除乱码之外的任何内容。
不幸的是,你没有在服务器上显示用于生成这个PDF文件的代码,所以很难说这个问题是否与它有关。 重要的是将响应的ContentType设置为application/pdf
并发送一个有效的PDF文件到响应中。 在客户端上解释此响应的方式很大程度上取决于正在使用的浏览器以及此客户端计算机上安装的不同插件和PDF阅读器。
您可能需要将Response.ContentType
设置为application/pdf
。 查看相关SO帖子。
当你渲染Content-Disposition: inline
它使用Adobe插件 - “Adobe PDF链接助手”(或FoxIt Reader)在IE中。 由于您的服务器上可能没有此ActiveX插件( AcroIEHelperShim.dll
),因为它没有内联解释器,所以它只会将内容字节呈现为text/html
。
最后算出来了。 您不需要在服务器上安装Adobe PDF阅读器或福昕阅读器。 你只需要在服务器上安装iTextReader(通过安装,我的意思是你的解决方案中存在程序集)。 你需要的是IIS中的MIME类型。 我们必须添加该MIME类型,并在此之后立即开始工作。 有趣的是,即使是这样,Chrome仍然能够弄清楚并正确渲染。 我假设IIS将正确的标题放置在与该MIME类型关联的位置,并且没有发生。 IE8无法弄清楚。
链接地址: http://www.djcxy.com/p/8183.html上一篇: Do you need Adobe PDF installed on server to work with iTextSharp?