如何使用itextsharp将一个pdf附加到其他pdf文件
如何在不使用itextsharp创建新pdf的情况下,将页面从另一个pdf文件附加到一个pdf文件。我将元数据附加到一个pdf,所以我只想添加其他pdf页面,以便第一个pdf元数据保持原样 问候如何使用itextsharp将一个pdf附加到其他pdf文件,pdf,append,Pdf,Append,如何在不使用itextsharp创建新pdf的情况下,将页面从另一个pdf文件附加到一个pdf文件。我将元数据附加到一个pdf,所以我只想添加其他pdf页面,以便第一个pdf元数据保持原样 问候 Himvj假设您有两个pdf文件:file1.pdf和file2.pdf,要将生成的pdf连接并保存到file1.pdf(通过替换其内容),您可以尝试以下操作: 使用(var输出=新内存流()) { var document=新文档(); var writer=新的PdfCopy(文档、输出); doc
Himvj假设您有两个pdf文件:
file1.pdf
和file2.pdf
,要将生成的pdf连接并保存到file1.pdf
(通过替换其内容),您可以尝试以下操作:
使用(var输出=新内存流())
{
var document=新文档();
var writer=新的PdfCopy(文档、输出);
document.Open();
foreach(新[]{“file1.pdf”、“file2.pdf”}中的var文件)
{
var reader=新的PdfReader(文件);
int n=reader.NumberOfPages;
PDF导入页面;
对于(int p=1;p假设您有两个pdf文件:file1.pdf
和file2.pdf
,要将生成的pdf连接并保存到file1.pdf
(通过替换其内容),您可以尝试以下操作:
使用(var输出=新内存流())
{
var document=新文档();
var writer=新的PdfCopy(文档、输出);
document.Open();
foreach(新[]{“file1.pdf”、“file2.pdf”}中的var文件)
{
var reader=新的PdfReader(文件);
int n=reader.NumberOfPages;
PDF导入页面;
对于(int p=1;p您可以尝试这样做,它会添加整个文档和元数据
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();
}
}
publicstaticvoidmergefiles(stringdestinationfile,string[]sourceFiles)
{
尝试
{
//1:为目标文档创建MemoryStream。
使用(MemoryStream ms=new MemoryStream())
{
//2:创建PdfCopyFields对象。
PdfCopyFields copy=新的PdfCopyFields(毫秒);
//-设置目标文件的安全性和其他设置。
//copy.Writer.SetEncryption(PdfWriter.strength128位,空,“1234”,PdfWriter.AllowPrinting | PdfWriter.AllowCopy | PdfWriter.AllowFillIn);
copy.Writer.ViewerPreferences=PdfWriter.PageModeUseOutlines;
//-创建一个arraylist以保存书签供以后使用。
ArrayList outlines=新的ArrayList();
int pageOffset=0;
int f=0;
//3:导入args[1]、args[2]等中指定的文档。。。
while(f
您可以尝试在添加整个文档和元数据时执行此操作
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();
}
}
publicstaticvoidmergefiles(stringdestinationfile,string[]sourceFiles)
{
尝试
{
//1:为目标文档创建MemoryStream。
使用(MemoryStream ms=new MemoryStream())
{
//2:创建PdfCopyFields对象。
PdfCopyFields copy=新的PdfCopyFields(毫秒);
//-设置目标文件的安全性和其他设置。
//copy.Writer.SetEncryption(PdfWriter.strength128位,空,“1234”,PdfWriter.AllowPrinting | PdfWriter.AllowCopy | PdfWriter.AllowFillIn);
抄写