We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PdfFileInfo is not working

I am trying to add custom properties to PDF documents using PdfFileInfo class. It seems it only works if the document is saved before processing the custom properties. If the document is not saved and on using PdfDocument object it throws a null reference exception. Please find the below example:

       Document PdfDoc = new Document();
        if (PdfDoc != null)
        {
            PdfDoc.PageInfo.Width = 612.0;
            PdfDoc.PageInfo.Height = 792.0;
            PdfDoc.PageInfo.Margin = new MarginInfo();
            PdfDoc.PageInfo.Margin.Left = 50;
            PdfDoc.PageInfo.Margin.Right = 50;
            PdfDoc.PageInfo.Margin.Top = 55;
            PdfDoc.PageInfo.Margin.Bottom = 30;

            Page PdfPage = PdfDoc.Pages.Add();
            PdfPage.PageInfo.Width = 612.0;
            PdfPage.PageInfo.Height = 792.0;
            PdfPage.PageInfo.Margin = new MarginInfo();
            PdfPage.PageInfo.Margin.Top = 48;
            PdfPage.PageInfo.Margin.Bottom = 48;
            PdfPage.PageInfo.Margin.Left = 50;
            PdfPage.PageInfo.Margin.Right = 50;
            PdfPage.PageInfo.IsLandscape = false;

            FloatingBox FloatBox = new FloatingBox();
            FloatBox.Margin = new MarginInfo(50, 48, 50, 48);
            FloatBox.Margin.Bottom = 0;
            FloatBox.IsInNewPage = false;
            FloatBox.IsKeptWithNext = true;

            PdfPage.Paragraphs.Add(FloatBox);
            TextFragment TextFragment = new TextFragment();
            TextSegment segment = new TextSegment();
            segment.Text = "Created sample for testing File Info functionality";
            TextFragment.Segments.Add(segment);
            FloatBox.Paragraphs.Add(TextFragment);

            #region Issue
            Aspose.Pdf.Facades.PdfFileInfo fInfo = new Aspose.Pdf.Facades.PdfFileInfo(PdfDoc);
            fInfo.SetMetaInfo("Name", "Sridhar"); // getting null reference here
            #endregion

            PdfDoc.Save("Sample.pdf", SaveFormat.Pdf);
            
            #region work around
            Aspose.Pdf.Facades.PdfFileInfo fInfo = new Aspose.Pdf.Facades.PdfFileInfo("Sample.pdf");
            fInfo.SetMetaInfo("Name", "Sridhar"); 
            fInfo.Save("Sample.pdf");
            #endregion

In region “Issue” you can find that i am using existing PdfDoc object and it throws null reference exception while “SetMetaInfo”. I am using 11.7 Aspose PDF version, please help me on the same.

@yagnyasridhar,
Thank you for contacting support. We recommend our clients use the new Document Object Model (DOM) approach and you are mixing it with the Aspose.Pdf.Facades approach. Please use Aspose.Pdf.DocumentInfo class instead of the Aspose.Pdf.Facades.PdfFileInfo class and modify your code as below:

[C#]

// Specify document information
DocumentInfo docInfo = new DocumentInfo(PdfDoc);
docInfo.Add("Name", "Sridhar");
// save PDF
PdfDoc.Save("sample.pdf", SaveFormat.Pdf);

Please also refer to this help topic: PDF File Metadata

Best Regards,
Imran Rafique