Save PDF files in C#
Incremental to Stream
If you have a large PDF document in a Stream
object and you want to modify only a small part of that PDF, use the SaveToFileInc
method of the GdPicturePDF
class. This method enables the content of the PDF document to be updated incrementally without rewriting it entirely. As a result, small changes to a larger PDF document are saved faster.
The following code draws a black rectangle on the first page of a PDF document and saves it incrementally to a stream:
using GdPicturePDF gdPicturePDF = new GdPicturePDF();// Create a `Stream` object from a PDF document.Stream streamPDF = new FileStream(@"C:\temp\source.pdf", FileMode.Open);// Load a PDF document from the stream.gdPicturePDF.LoadFromStream(streamPDF);// Select the first page.gdPicturePDF.SelectPage(1);// Set the fill color to black.gdPicturePDF.SetFillColor(0, 0, 0);// Draw a filled rectangle on the entire page.gdPicturePDF.DrawRectangle( Left: 0, Top: 0, Width: gdPicturePDF.GetPageWidth(), Height: gdPicturePDF.GetPageHeight(), Fill: true, Stroke: false);// Save the PDF document incrementally.gdPicturePDF.SaveToStreamInc(streamPDF);
Using gdPicturePDF As GdPicturePDF = New GdPicturePDF() ' Create a `Stream` object from a PDF document. Dim streamPDF As Stream = New FileStream("C:\temp\source.pdf", FileMode.Open) ' Load a PDF document from the stream. gdPicturePDF.LoadFromStream(streamPDF) ' Select the first page. gdPicturePDF.SelectPage(1) ' Set the fill color to black. gdPicturePDF.SetFillColor(0, 0, 0) ' Draw a filled rectangle on the entire page. gdPicturePDF.DrawRectangle( Left:=0, Top:=0, Width:=gdPicturePDF.GetPageWidth(), Height:=gdPicturePDF.GetPageHeight(), Fill:=True, Stroke:=False) ' Save the PDF document incrementally. gdPicturePDF.SaveToStreamInc(streamPDF)End Using