如何使用itextsharp创建新PDF而不从另一个pdf文件向另一个pdf文件添加页面。我有元数据附加到一个pdf,所以我只想添加其他pdf页面,以便第一个pdf元数据应保持原样。如何使用itextsharp向其他pdf文件附加一个pdf
问候 Himvj
如何使用itextsharp创建新PDF而不从另一个pdf文件向另一个pdf文件添加页面。我有元数据附加到一个pdf,所以我只想添加其他pdf页面,以便第一个pdf元数据应保持原样。如何使用itextsharp向其他pdf文件附加一个pdf
问候 Himvj
假设你有2个PDF文件:file1.pdf
和file2.pdf
你想连接和生成的PDF保存到file1.pdf
(通过替换它的内容),你可以尝试以下方法:
using (var output = new MemoryStream())
{
var document = new Document();
var writer = new PdfCopy(document, output);
document.Open();
foreach (var file in new[] { "file1.pdf", "file2.pdf" })
{
var reader = new PdfReader(file);
int n = reader.NumberOfPages;
PdfImportedPage page;
for (int p = 1; p <= n; p++)
{
page = writer.GetImportedPage(reader, p);
writer.AddPage(page);
}
}
document.Close();
File.WriteAllBytes("file1.pdf", output.ToArray());
}
你可以试试这个它添加整个文件的元数据
public static void MergeFiles(string destinationFile, string[] sourceFiles)
{
try
{
//1: Create the MemoryStream for the destination document.
using (MemoryStream ms = new MemoryStream())
{
//2: Create the PdfCopyFields object.
PdfCopyFields copy = new PdfCopyFields(ms);
// - Set the security and other settings for the destination file.
//copy.Writer.SetEncryption(PdfWriter.STRENGTH128BITS, null, "1234", PdfWriter.AllowPrinting | PdfWriter.AllowCopy | PdfWriter.AllowFillIn);
copy.Writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
// - Create an arraylist to hold bookmarks for later use.
ArrayList outlines = new ArrayList();
int pageOffset = 0;
int f = 0;
//3: Import the documents specified in args[1], args[2], etc...
while (f < sourceFiles.Length)
{
// Grab the file from args[] and open it with PdfReader.
string file = sourceFiles[f];
PdfReader reader = new PdfReader(file);
// Import the pages from the current file.
copy.AddDocument(reader);
// Create an ArrayList of bookmarks in the file being imported.
// ArrayList bookmarkLst = SimpleBookmark.GetBookmark(reader);
// Shift the pages to accomidate any pages that were imported before the current document.
// SimpleBookmark.ShiftPageNumbers(bookmarkLst, pageOffset, null);
// Fill the outlines ArrayList with each bookmark as a HashTable.
// foreach (Hashtable ht in bookmarkLst)
// {
// outlines.Add(ht);
// }
// Set the page offset to the last page imported.
//copy.Writer.SetPageSize(rec);
pageOffset += reader.NumberOfPages;
f++;
}
//4: Put the outlines from all documents under a new "Root" outline and
// set them for destination document
// copy.Writer.Outlines = GetBookmarks("Root", ((Hashtable)outlines[0])["Page"], outlines);
//5: Close the PdfCopyFields object.
copy.Close();
//6: Save the MemoryStream to a file.
MemoryStreamToFile(ms, destinationFile);
}
}
catch (System.Exception e)
{
System.Console.Error.WriteLine(e.Message);
System.Console.Error.WriteLine(e.StackTrace);
System.Console.ReadLine();
}
}
public static void MemoryStreamToFile(MemoryStream MS, string FileName)
{
using (FileStream fs = new FileStream(@FileName, FileMode.Create))
{
byte[] data = MS.ToArray();
fs.Write(data, 0, data.Length);
fs.Close();
}
}
嗨Hala,它覆盖了file1.pdf的页面并复制了file2.pdf的页面,我想从file2.pdf的file1.pdf中添加页面。 – himvj 2012-04-11 13:05:01
喜达林感谢在t他回复,但在这种情况下,我的元数据附加到file1.pdf将被删除。我想只是将file2页追加到file1而不创建新文件。因此file1元数据将保留。 – himvj 2012-04-09 07:32:56
@himvj,也许你可以在overwrtie之前从file1.pdf中提取元数据,然后重写后面的数据。 – char1es 2013-10-29 14:07:54