【发布时间】:2019-09-17 13:08:40
【问题描述】:
我将 2 个 PDF 文件合并为一个,但即使没有明确的错误,它也不会合并任何内容。我已经尝试了很多,但我仍然无法做到正确。这是我正在使用的项目中的文件夹。允许写入权限,并且允许在其中写入和创建文件。
public static void MergeFiles(string destinationFile, string[] sourceFiles) {
try {
sourceFiles = new string[2] {
HostingEnvironment.MapPath(@"/Downloads/Certificates/InspectionReport(78).pdf"),
HostingEnvironment.MapPath(@"/Downloads/Certificates/InspectionReport(78).pdf")
};
//outputPdfPath = Path.GetFileName("~/Downloads/Certificates/119.FDV-3686.pdf");
destinationFile = HostingEnvironment.MapPath(@"/Downloads/Certificates/InspectionReport(78).pdf");
int f = 0;
// we create a reader for a certain document
PdfReader reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
//Console.WriteLine("There are " + n + " pages in the original file.");
// step 1: creation of a document-object
Document document = new Document(reader.GetPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
// step 3: we open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
// step 4: we add content
while (f < sourceFiles.Length) {
int i = 0;
while (i < n) {
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270) {
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else {
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
//Console.WriteLine("Processed page " + i);
}
f++;
if (f < sourceFiles.Length) {
reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
n = reader.NumberOfPages;
//Console.WriteLine("There are " + n + " pages in the original file.");
}
}
// step 5: we close the document
document.Close();
}
catch(Exception e) {
string strOb = e.Message;
}
}
【问题讨论】:
-
您尝试写入同时尝试读取的文件。使用不同的结果文件名。
标签: c# asp.net asp.net-mvc-4 c#-4.0 itext