I am trying to move existing code from ITextSharp to Aspose.Pdf. At the moment, it’s unclear if Aspose.Pdf supports what is needed. I still have the evaluation version, however before I can convince my company to buy a full license I need it working as a valid replacement, so any help that can get it working is greatly appreciated.
Basically, the code first gets all the text in the existing PDF for use later. I’m trying to do this as follows:
Right now, this results in extractedText being a truncated version of what’s in the pdf file.
Now, assuming that I got the full text, the code does some processing, but then it wants to take the original PDF file and clone it to a completely new PDF file with added header information, plus added security restrictions.
//Trying to take in an existing PDF file, add headers to every page of it, and save it out to a new
//pdf document with the security settings
//Results in ArgumentNullException on calling save to stream
var pdf = new Aspose.Pdf.Generator.Pdf();
try
{
pdf.Security = new Security
{
IsDefaultAllAllowed = false,
Is128BitsEncrypted = true,
IsScreenReadersAllowed = true,
IsPrintingAllowed = true,
IsDegradedPrintingAllowed = true,
IsFormFillingAllowed = false,
IsCopyingAllowed = false,
IsDocumentAssemblyingAllowed = false,
IsContentsModifyingAllowed = false,
IsAnnotationsModifyingAllowed = false
};
//set stream of incoming pdf document to the start so we can get page count
originalPdfStream.Seek(0, SeekOrigin.Begin);
reader = new Document(originalPdfStream);
var numPages = reader.Pages.Count;
//Add section to new pdf file so we can add headers to the new file
pdf.Sections = new Sections();
var section = pdf.Sections.Add();
var hf1 = new Aspose.Pdf.Generator.HeaderFooter(section);
var text = new Aspose.Pdf.Generator.Text(hf1,
string.Format(“NumberOfPages:{0}”, numPages));
hf1.Paragraphs.Add(text);
text = new Aspose.Pdf.Generator.Text(hf1, string.Format(“Signature:{0}”, encryptedData));
hf1.Paragraphs.Add(text);
text = new Aspose.Pdf.Generator.Text(hf1, string.Format(“Hash:{0}”, hash));
hf1.Paragraphs.Add(text);
//set stream to original pdf to the start
originalPdfStream.Seek(0, SeekOrigin.Begin);
using (var streamReader = new StreamReader(originalPdfStream))
{
//Import original pdf into new document. I cannot find information on how to do this right in
//Aspose, so I assumed
pdf.Import(streamReader, new Aspose.Pdf.Generator.ImportOptions());
}
//try to save changes. get the following exception.
pdf.Save(_signedFile);
}
finally
{
reader.Dispose();
}
/
System.ArgumentNullException : Value cannot be null.
Parameter name: name
at ..(String , String , )
at e..e( , )
at e..e( , , Int32 , & )
at e..e( , , Int32 , & )
at e..e( , , Int32 , & )
at e..e( , , Int32 , & )
at e..e( , , Int32 , & )
at e..(String )
at ..e(String )
at ..e(String , Pdf , Section , Text , Single , Single )
at ..e(Pdf , Section , Cell , Text , HeaderFooter , , )
at ..e(Pdf , Section , Table , Cell , Text , HeaderFooter , , )
at ..e(Pdf , Section , HeaderFooter , Table , Row , Cell , Text , , , Boolean )
at ..e(Pdf , Section , )
at ..e(Pdf )
at ..e( , Pdf )
at Aspose.Pdf.Generator.Pdf.Save(Stream stream)
/
First off, is there a way to take an old PDF, get it’s internal text, then create a completely new PDF with the old PDF data plus added headers and security restrictions?
Second off, could you please let me know how I’ve used Aspose incorrectly from the above code samples?