En

KNOWLEDGEBASE

How to save PDF to stream, directory, or web app in C#

If you want to know how to quickly and easily save PDF documents to a stream, directory, or web application with C#, we’ve got you covered.

Aspose.PDF for .NET is a robust and powerful API for manipulating PDF files and performing a range of tasks such as adding, replacing, or removing text and images, adding or removing bookmarks and watermarks, splitting, merging, extracting or inserting pages, custom font handling, and much more.

Save PDF in C sharp

Save the PDF file to a stream with C#

With our API you can easily save the created or manipulated PDF file to a stream by using overloads of Save methods.

public static void SaveDocumentStream()
{
    var originalFileName = Path.Combine(_dataDir, "SimpleResume.pdf");
    var modifiedFileName = Path.Combine(_dataDir, "SimpleResumeModified.pdf");

    var pdfDocument = new Aspose.Pdf.Document(originalFileName);
    // make some manipation, i.g add new empty page
    pdfDocument.Pages.Add();
    pdfDocument.Save(System.IO.File.OpenWrite(modifiedFileName));
}

Save the PDF file to a directory with C#

You can also save the PDF file to a directory using Save method of Document class. The document is saved by default in Aspose.PDF v.1.7 (*.pdf) format if you do not provide the format type (options).

public static void SaveDocument()
{
    var originalFileName = Path.Combine(_dataDir, "SimpleResume.pdf");
    var modifiedFileName = Path.Combine(_dataDir, "SimpleResumeModified.pdf");

    var pdfDocument = new Aspose.Pdf.Document(originalFileName);
    // make some manipation, i.g add new empty page
    pdfDocument.Pages.Add();
    pdfDocument.Save(modifiedFileName);
}

Save the PDF file in web applications with C#

To save the PDF file in web applications, you can use the methods suggested above. Also, the Document class has an overloaded method Save for use with the HttpResponse class.

var originalFileName = Path.Combine(_dataDir, "SimpleResume.pdf");
var pdfDocument = new Aspose.Pdf.Document(originalFileName);
// make some manipulation, i.g add a new empty page
pdfDocument.Pages.Add();
pdfDocument.Save(Response, originalFileName, ContentDisposition.Attachment, new PdfSaveOptions());

Save PDF/A or PDF/X format

PDF/A is an ISO-standardized version of the PDF that ensures a document can be reproduced exactly the same way, leaving your documents safe and accessible for the long term. PDF/A, unlike PDF, prohibits features unsuitable for long-term archiving, such as font linking or encryption.
Like before, the Save method is used to store the file, but the file must be prepared using the Convert method.

public static void SaveDocumentAsPDFx()
{
    var pdfDocument = new Aspose.Pdf.Document("..\\..\\..\\Samples\\SimpleResume.pdf");
    pdfDocument.Pages.Add();
    pdfDocument.Convert(new PdfFormatConversionOptions(PdfFormat.PDF_X_3));
    pdfDocument.Save("..\\..\\..\\Samples\\SimpleResume_X3.pdf");
}

We’re with you at every stage of your developing
If you need new features in your existing Aspose.PDF product or an API for new file formats, our experts are at your disposal. You can hire one or more dedicated developers to work with you on your project.
The Paid Consulting team makes sure you always get the best value.