Back to Devexpress

PdfDocumentProcessor.CreateEmptyDocument(Stream, PdfSaveOptions) Method

officefileapi-devexpress-dot-pdf-dot-pdfdocumentprocessor-dot-createemptydocument-x28-system-dot-io-dot-stream-devexpress-dot-pdf-dot-pdfsaveoptions-x29.md

latest11.6 KB
Original Source

PdfDocumentProcessor.CreateEmptyDocument(Stream, PdfSaveOptions) Method

Creates an empty PDF document with no pages with specified save options (encryption and signature settings) and writes it to a stream.

Namespace : DevExpress.Pdf

Assembly : DevExpress.Docs.v25.2.dll

NuGet Package : DevExpress.Document.Processor

Declaration

csharp
public void CreateEmptyDocument(
    Stream stream,
    PdfSaveOptions saveOptions
)
vb
Public Sub CreateEmptyDocument(
    stream As Stream,
    saveOptions As PdfSaveOptions
)

Parameters

NameTypeDescription
streamStream

A stream to write the empty PDF document.

| | saveOptions | PdfSaveOptions |

An object that contains settings to encrypt an empty document.

|

Remarks

Use this method to create an empty document from scratch. The PdfSaveOptions options allows you to specify the encryption settings (the PdfSaveOptions.EncryptionOptions property) to protect an empty document with a password and permissions.

Use the PDF document creation API to generate the document layout (e.g., append pages with graphics to the PDF document, generate bookmarks, and attach files). Refer to the following article for more information: Additional Content.

Note

When all operations with a created document are completed, close the document either by the PdfDocumentProcessor.CloseDocument method call or dispose of the PdfDocumentProcessor instance.

The PDF specification does not describe empty documents. For this reason, most third-party PDF viewers cannot open such files. This does not apply to the DevExpress WinForms PDF Viewer and WPF PDF Viewer, which are less demanding concerning the validity of opened documents, and are capable of opening documents containing no pages.

This CreateEmptyDocument method overload writes generated document content into the stream specified as this method’s parameter. The SaveDocument method call rewrites this stream.

Example

This example shows the PDF Document Creation API that is used to programmatically generate a document layout.

  • The custom DrawGraphics method draws content inside an empty PDF document.
  • The custom AddWatermark method generates a watermark with custom text and adds it to the created PDF document.

When you launch the app, the file is saved to your Documents folder (%userprofile%/Documents).

View Example

cs
using DevExpress.Drawing;
using DevExpress.Pdf;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;

namespace DocumentCreationAPI {
    class Program {
        static void Main(string[] args) {

            string docPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"Result.pdf");

            using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
                // Create an empty document.
                processor.CreateEmptyDocument(docPath);
                // Create and draw PDF graphics.
                using (PdfGraphics graph = processor.CreateGraphicsWorldSystem()) {
                    DrawGraphics(graph);
                    // Render a page with graphics.
                    processor.RenderNewPage(PdfPaperSize.Letter, graph);
                }
            }
            // Generate a watermark.
            AddWatermark("Not for sale",docPath,docPath);
            Process.Start(new ProcessStartInfo(docPath) { UseShellExecute = true });
        }

        // Draw graphics inside a PDF document.
        static void DrawGraphics(PdfGraphics graph) {
            // Draw text lines on the page.
            DXSolidBrush black = (DXSolidBrush)DXBrushes.Black;
            DXFont font1 = new DXFont("Times New Roman", 32, DXFontStyle.Bold);
            graph.DrawString("PDF Document Processor", font1, black, 140, 50);

            DXFont font2 = new DXFont("Arial", 20);
            graph.DrawString("Display, Print and Export PDF Documents", font2, black, 128, 120);

            DXFont font3 = new DXFont("Arial", 10);
            graph.DrawString("The PDF Document Processor is a non-visual component " +
                              "that provides the application programming interface of the PDF Viewer.", font3, black, 16, 180);
        }

        // Add a watermark with custom text.
        static void AddWatermark(string text,string fileName,string resultFileName) {
            using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) {
                string fontName = "Arial Black";
                int fontSize = 12;
                PdfStringFormat stringFormat = PdfStringFormat.GenericTypographic;
                stringFormat.Alignment = PdfStringAlignment.Center;
                stringFormat.LineAlignment = PdfStringAlignment.Center;
                documentProcessor.LoadDocument(fileName);
                using (DXSolidBrush brush = new DXSolidBrush(Color.FromArgb(63,Color.Black))) {
                    DXFont font = new DXFont(fontName,fontSize);
                    foreach (var page in documentProcessor.Document.Pages) {
                        var watermarkSize = page.CropBox.Width * 0.75;
                        using (PdfGraphics graphics = documentProcessor.CreateGraphicsPageSystem()) {
                            SizeF stringSize = graphics.MeasureString(text,font);
                            float scale = (float)(watermarkSize / (double)stringSize.Width);
                            graphics.TranslateTransform((float)(page.CropBox.Width * 0.5),(float)(page.CropBox.Height * 0.5));
                            graphics.RotateTransform((float)45.0);
                            graphics.TranslateTransform((float)(-stringSize.Width * scale * 0.5),(float)(-stringSize.Height * scale * 0.5));
                            DXFont actualFont = new DXFont(fontName,fontSize * scale);
                            RectangleF rect = new RectangleF(0,0,stringSize.Width * scale,stringSize.Height * scale);
                            graphics.DrawString(text,actualFont,brush,rect,stringFormat);

                            graphics.AddToPageForeground(page);
                        }
                    }
                }
                documentProcessor.SaveDocument(resultFileName);
            }
        }
    }
}
vb
Imports DevExpress.Drawing
Imports DevExpress.Pdf
Imports System
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO

Namespace DocumentCreationAPI

    Friend Class Program

        Shared Sub Main(ByVal args As String())

            Dim docPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Result.pdf")

            Using processor As PdfDocumentProcessor = New PdfDocumentProcessor()
                ' Create an empty document.
                processor.CreateEmptyDocument(docPath)
                ' Create and draw PDF graphics.
                Using graph As PdfGraphics = processor.CreateGraphics()
                    DrawGraphics(graph)
                    ' Render a page with graphics.
                    processor.RenderNewPage(PdfPaperSize.Letter, graph)
                End Using
            End Using
            ' Generate a watermark.
            AddWatermark("Not for sale", docPath, docPath)
            Process.Start(New ProcessStartInfo(docPath) With {.UseShellExecute = True})
        End Sub

        ' Draw graphics inside a PDF document.
        Private Shared Sub DrawGraphics(ByVal graph As PdfGraphics)
            ' Draw text lines on the page.
            Dim black As DXSolidBrush = CType(DXBrushes.Black, DXSolidBrush)
            Dim font1 As DXFont = New DXFont("Times New Roman", 32, DXFontStyle.Bold)
            graph.DrawString("PDF Document Processor", font1, black, 140, 50)

            Dim font2 As DXFont = New DXFont("Arial", 20)
            graph.DrawString("Display, Print and Export PDF Documents", font2, black, 128, 120)

            Dim font3 As DXFont = New DXFont("Arial", 10)
            graph.DrawString("The PDF Document Processor is a non-visual component " & "that provides the application programming interface of the PDF Viewer.", font3, black, 16, 180 )
        End Sub

        ' Add a watermark with custom text.
        Private Shared Sub AddWatermark(Text As String, fileName As String, resultFileName As String)
            Using documentProcessor As New PdfDocumentProcessor()
                Dim fontName As String = "Arial Black"
                Dim fontSize As Integer = 12
                Dim stringFormat As PdfStringFormat = PdfStringFormat.GenericTypographic
                stringFormat.Alignment = PdfStringAlignment.Center
                stringFormat.LineAlignment = PdfStringAlignment.Center
                documentProcessor.LoadDocument(fileName)
                Using brush As New SolidBrush(Color.FromArgb(63, Color.Black))
                    Using font As New Font(fontName, fontSize)
                        For Each page In documentProcessor.Document.Pages
                            Dim watermarkSize = page.CropBox.Width * 0.75
                            Using graphics As PdfGraphics = documentProcessor.CreateGraphicsPageSystem()
                                Dim stringSize As SizeF = graphics.MeasureString(Text, font)
                                Dim scale As Single = CSng(watermarkSize) / stringSize.Width
                                graphics.TranslateTransform(CSng(page.CropBox.Width * 0.5), CSng(page.CropBox.Height * 0.5))
                                graphics.RotateTransform(45.0)
                                graphics.TranslateTransform(CSng(-stringSize.Width * scale * 0.5), CSng(-stringSize.Height * scale * 0.5))
                                Using actualFont As Font = New Font(fontName, fontSize * scale)
                                    Dim rect As RectangleF = New RectangleF(0, 0, stringSize.Width * scale, stringSize.Height * scale)
                                    graphics.DrawString(Text, actualFont, brush, rect, stringFormat)
                                End Using
                                graphics.AddToPageForeground(page)
                            End Using
                        Next
                    End Using
                End Using
                documentProcessor.SaveDocument(resultFileName)
            End Using
        End Sub
    End Class
End Namespace

See Also

PdfDocumentProcessor Class

PdfDocumentProcessor Members

DevExpress.Pdf Namespace