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:
//Trying to get full text content of the PDF. Right now only get trimmed text.
var pdfDocument = new Document(fileContentStream);
var textAbsorber = new TextAbsorber();
pdfDocument.Pages.Accept(textAbsorber);
var extractedText = textAbsorber.Text;
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.
I have been unsuccessfully trying to do this as follows:
//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?
Thank you,
Jon Ediger
Question fully fleshed out at forum url:
Hi Jon,
Thanks for contacting support and sorry for the delayed response.
We are working over this query and will get back to you soon.
Is there any update on my question?
Thank you,
Jon Ediger
Hi Jon,
Thanks for your patience and sorry for the delayed response.
Please note that Aspose.Pdf.Generator.Pdf class is used to create a new PDF document from scratch and I am afraid it does not support the feature to load an existing PDF file and perform any manipulation. Please visit the following links for information on
- Extract Text from all the Pages using Text Device
- Add Header in a PDF File (Facades)
- Add Footer in a PDF File (Facades)
- Set Privileges on an Existing PDF File (Facades)
Furthermore, before you purchase our product, you may request a 30 days Temporary license to test our product without any limitations. Please visit Get a temporary license
In case of any further query, please feel free to contact.