一直在努力。
我有一个物理文件(pdf)和一个由iTextSharp(pdf)生成的文件,我的目标是合并它们并将其输出到浏览器。
顺便说一下,我正在使用ASP.NET MVC 4
所以,在我的控制器中,我有类似的东西:
public ActionResult Index()
{
MemoryStream memoryStream = new MemoryStream();
var path = Server.MapPath("~/Doc/../myfile.pdf"); // This is my physical file
var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
GenerateFile(); // This is my generated file thru iTextSharp
Response.AddHeader("Content-Disposition", "inline");
memoryStream.Position = 0;
return new FileStreamResult(// Maybe merged file goes here? not sure.
,"application/pdf");
}
private void GenerateFile()
{
MemoryStream stream = new MemoryStream();
var document = new Document(/*some settings here*/);
PdfWriter.GetInstance(document, stream).CloseStream = false;
document.Open();
// generate pdf here
document.Close();
}
是否可以将生成的pdf设置为第一个(或者它将生成多少页)页面,然后附加物理文件?
非常感谢任何帮助。感谢
答案 0 :(得分:0)
如果有任何帮助,我已经使用PDFSharp做了类似的事情(合并物理和代码生成的PDF)。
PdfDocument document = new PdfDocument();
PdfDocument physicalDoc = PdfSharp.Pdf.IO.PdfReader.Open(filepath);
PdfPage coverPage = physicalDoc.Pages[0];
document.AddPage(coverPage);
然后添加您自己生成的页面可以完成:
PdfPage generatedPage = new PdfPage();
XGraphics g = XGraphics.FromPdfPage(generatedPage);
g.DrawRectangle(color, x, y, width, height);
g.DrawString("This release document describes the contents of..."
,font, textColor, x, y);
document.AddPage(generatedPage)
答案 1 :(得分:0)
我可以提供c#
的示例首先,您需要安装第三方库:例如pdfium.net sdk
你可以通过nuget来做到这一点 安装包pdfium.net.sdk
public void MergeDocument()
{
//Initialize the SDK library
//You have to call this function before you can call any PDF processing functions.
PdfCommon.Initialize();
//Open and load a PDF document in which will be merged other files
using (var mainDoc = PdfDocument.Load(@"c:\test001.pdf"))
{
//Open one PDF document.
using (var doc = PdfDocument.Load(@"c:\doc1.pdf"))
{
//Import all pages from document
mainDoc.Pages.ImportPages(
doc,
string.Format("1-{0}", doc.Pages.Count),
mainDoc.Pages.Count
);
}
//Open another PDF document.
using (var doc = PdfDocument.Load(@"c:\doc2.pdf"))
{
//Import all pages from document
mainDoc.Pages.ImportPages(
doc,
string.Format("1-{0}", doc.Pages.Count),
mainDoc.Pages.Count
);
}
mainDoc.Save(@"c:\ResultDocument.pdf", SaveFlags.NoIncremental);
}
//Release all resources allocated by the SDK library
PdfCommon.Release();
}