Hello,
I did the following:
- Openend a form PDF and inserted values in the form fields and saved the stream
- then used that stream with Stamp.BindPDF to put that form PDF as a stamp on my other main PDF file
- After applying the stamp I opened the stamped PDF and only see the stamp without the filled out fields!!! Why?
Here is the code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Aspose.Pdf.Kit;
using System.IO;
using System.Collections.Generic;
using Aspose.Words;
using Aspose.Words.Fields;
namespace TestWebApp
{
public partial class StamperForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string currentDir = Server.MapPath("bin");
string footerPdf = Path.Combine(currentDir, "Footer.pdf");
string testPdf = Path.Combine(currentDir, "main.pdf");
string resultPdf = Path.Combine(currentDir, "test.pdf");
Dictionary<string, string> footerProperties = new Dictionary<string, string>();
footerProperties.Add("Title", "My test footer ");
footerProperties.Add("DOCID", "2010-123456");
footerProperties.Add("DOCVersion", "1.3");
footerProperties.Add("DOCStatus", "Published");
footerProperties.Add("PrintDate", "2010-03-02");
MemoryStream footerStream = GetFilledOutFooterStream(footerPdf, footerProperties);
footerStream.Seek(0, SeekOrigin.Begin);
Stamp aStamp = new Stamp();
// aStamp.BindPdf(Path.Combine(Server.MapPath("bin"), "temp.pdf"), 1);
aStamp.BindPdf(footerStream, 1);
aStamp.IsBackground = false;
PdfFileStamp stamper = new PdfFileStamp(testPdf, resultPdf);
stamper.AddStamp(aStamp);
stamper.Close();
footerStream.Close();
}
private MemoryStream GetFilledOutFooterStream(string footerPdfUri, Dictionary<string, string> footerProperties)
{
MemoryStream ms = new MemoryStream();
string extension = Path.GetExtension(footerPdfUri).TrimStart('.').ToLowerInvariant();
switch (extension)
{
case "pdf":
using (FileStream footerStream = File.OpenRead(footerPdfUri))
{
Aspose.Pdf.Kit.Form form = new Aspose.Pdf.Kit.Form(footerStream, ms);
foreach (string field in form.FieldsNames)
{
string fieldName = string.Empty;
int start = field.LastIndexOf(".");
int stop = field.LastIndexOf("[");
if (start != -1 && stop != -1)
fieldName = field.Substring(start + 1, stop - start - 1);
else
fieldName = field;
if (footerProperties.ContainsKey(fieldName))
{
form.FillField(field, footerProperties[fieldName]);
}
}
form.Save();
ms.Seek(0, SeekOrigin.Begin);
FileStream fs = File.OpenWrite(Path.Combine(Server.MapPath("bin"), "temp.pdf"));
byte[] buffer = ms.ToArray();
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
break;
case "doc":
Document doc = new Document(footerPdfUri);
FormFieldCollection ffCol = doc.Range.FormFields;
foreach (FormField field in ffCol)
{
if (field.Type == Aspose.Words.Fields.FieldType.FieldFormTextInput)
{
if (footerProperties.ContainsKey(field.Name))
{
field.Result = footerProperties[field.Name];
}
}
}
doc.Save(ms, SaveFormat.Pdf);
break;
default:
throw new InvalidOperationException("Footer file extension is not supported.");
}
return ms;
}
}
}