Is it possible to merge a fillable pdf file with a regular pdf file in asp.net C# using itextsharp. Any help would be greatly appreciated.
问题:
回答1:
Took me a long time to figure this out but here is a working example:
public static void MergeFiles(string destinationFile, string[] sourceFiles)
{
try
{
int f = 0;
String outFile = destinationFile;
Document document = null;
PdfCopy writer = null;
while (f < sourceFiles.Length) {
// Create a reader for a certain document
PdfReader reader = new PdfReader(sourceFiles[f]);
// Retrieve the total number of pages
int n = reader.NumberOfPages;
//Trace.WriteLine("There are " + n + " pages in " + sourceFiles[f]);
if (f == 0)
{
// Step 1: Creation of a document-object
document = new Document(reader.GetPageSizeWithRotation(1));
// Step 2: Create a writer that listens to the document
writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
// Step 3: Open the document
document.Open();
}
// Step 4: Add content
PdfImportedPage page;
for (int i = 0; i < n; )
{
++i;
page = writer.GetImportedPage(reader, i);
writer.AddPage(page);
}
PRAcroForm form = reader.AcroForm;
if (form != null)
writer.CopyAcroForm(reader);
f++;
}
// Step 5: Close the document
document.Close();
}
catch(Exception)
{
//handle exception
}
}
Hope this helps!
Source: http://www.ujihara.jp/iTextdotNET/examples/concat_pdf.java
回答2:
Yep.
PdfCopyFields sounds just about right. That way even if your "regular pdf file" has annotations of whatever sort (links, flash, whatever), they'll still work fine.
Alternatively, you could open the form with a PdfStamper, and add PdfImportedPages from the "regular" PDF. This would preserve everything about the original PDF (metadata, structure, doc-level scripts, etc), and suck in the pages (sans anything that wasn't page contents) from the "regular pdf".
There's no perfect answer here sadly, but there are a at least two viable options to choose from.
回答3:
You can use PdfCopyFields to merge any complex fillable pdf forms. It does support for all the styles, form fields and pdf functionalities Just try this code..
PdfReader readerfile1 = new PdfReader("File1");
PdfReader readerfile2 = new PdfReader("File2");
PdfCopyFields copyfile =
new PdfCopyFields(new FileStream("OutputFilewithFullPath", FileMode.Create));
copyfile.AddDocument(readerfile1);
copyfile.AddDocument(readerfile2);
copyfile.Close();
This will result in a new file, which you had specified and will be a complete fillable form.
回答4:
I realize this is quite an old thread, however I had a similar problem and here is a cleaner solution
public string GetMergedPdfPath(List<KeyValuePair<AccountHolder, string>> filePaths, string outputFilename)
{
string directory = ApplicationDeployment.IsNetworkDeployed ? ApplicationDeployment.CurrentDeployment.DataDirectory : NoticeConstants.Paths.TestDirectory;
string outputFilepath = directory + "\\" + outputFilename;
Document outputDocument = null;
PdfCopy outputCopier = null;
try
{
using (outputDocument = new Document())
{
using (FileStream fs = new FileStream(outputFilepath, FileMode.Create))
{
using (outputCopier = new PdfCopy(outputDocument, fs))
{
outputDocument.Open();
for (int i = 0; i < filePaths.Count; i++)
{
if (string.IsNullOrEmpty(filePaths[i].Value))
{
continue;
}
using (PdfReader reader = new PdfReader(filePaths[i].Value))
{
NoticeUtil.GetPdfPages(reader, ref outputCopier);
}
}
outputDocument.Close();
}
}
}
return outputFilepath;
}
catch (Exception ex)
{
Log.Error(ex.Message, ex);
return string.Empty;
}
}
private static List<PdfImportedPage> GetPdfPages(PdfReader reader, ref PdfCopy outputCopier)
{
List<PdfImportedPage> pages = new List<PdfImportedPage>();
try
{
for (int p = 1; p <= reader.NumberOfPages; p++)
{
outputCopier.AddPage(outputCopier.GetImportedPage(reader, p));
}
}
catch (Exception ex)
{
Log.Error(ex.Message, ex);
}
return pages;
}