/// <summary> /// Demonstrate reading a PDF Document from a .NET Stream object. /// </summary> /// <param name="filename">The filename of the PDF document to read.</param> void ReadFromStream(String path, String output) { // Create a .NET FileStream object, opened using the path argument. // A FileStream is used here for demonstration only, but the technique // works just as well for MemoryStream, or other streams which support // seeking. In practice, when dealing with files it is usually more // appropriate to pass the path directly to the Document constructor. FileStream fs = new FileStream(path, FileMode.Open); // A document is then opened, using the FileStream as its data source. using (Document d = new Document(fs)) { // Add a watermark to have some visible change to the PDF WatermarkParams wp = new WatermarkParams(); wp.TargetRange.PageSpec = PageSpec.AllPages; WatermarkTextParams wtp = new WatermarkTextParams(); wtp.Text = "This PDF was opened\nfrom a Stream"; d.Watermark(wtp, wp); // The document can not simply be saved at this point. // d.Save(SaveFlags.Incremental); // This will throw an exception. // Instead, the document can be saved to a file. Saving the document // to a file causes the document to use the file as its data source. d.Save(SaveFlags.Full, output); // Make another minor change. d.Creator = "DLE StreamIO Sample"; // Since the document is now backed by a file, an incremental save is okay. d.Save(SaveFlags.Incremental); } }
static void Main(string[] args) { Console.WriteLine("Watermark Sample:"); // ReSharper disable once UnusedVariable using (Library lib = new Library()) { Console.WriteLine("Initialized the library."); String sInput = Library.ResourceDirectory + "Sample_Input/sample.pdf"; String sWatermark = Library.ResourceDirectory + "Sample_Input/ducky.pdf"; String sOutput = "Watermark-out.pdf"; if (args.Length > 0) { sInput = args[0]; } if (args.Length > 1) { sWatermark = args[1]; } if (args.Length > 2) { sOutput = args[2]; } Console.WriteLine("Adding watermark from " + sWatermark + " to " + sInput + " and saving to " + sOutput); Document doc = new Document(sInput); Document watermarkDoc = new Document(sWatermark); WatermarkParams watermarkParams = new WatermarkParams(); watermarkParams.Opacity = 0.8f; watermarkParams.Rotation = 45.3f; watermarkParams.Scale = 0.5f; watermarkParams.TargetRange.PageSpec = PageSpec.EvenPagesOnly; doc.Watermark(watermarkDoc.GetPage(0), watermarkParams); watermarkParams.TargetRange.PageSpec = PageSpec.OddPagesOnly; WatermarkTextParams watermarkTextParams = new WatermarkTextParams(); Color color = new Color(109.0f / 255.0f, 15.0f / 255.0f, 161.0f / 255.0f); watermarkTextParams.Color = color; watermarkTextParams.Text = "Multiline\nWatermark"; Font f = new Font("Courier", FontCreateFlags.Embedded | FontCreateFlags.Subset); watermarkTextParams.Font = f; watermarkTextParams.TextAlign = HorizontalAlignment.Center; doc.Watermark(watermarkTextParams, watermarkParams); doc.EmbedFonts(); doc.Save(SaveFlags.Full | SaveFlags.Linearized, sOutput); } }