diff --git a/Annotation/Add-a-popup-annotation-to-an-existing-PDF-document/.NET/Add-a-popup-annotation-to-an-existing-PDF-document/Program.cs b/Annotation/Add-a-popup-annotation-to-an-existing-PDF-document/.NET/Add-a-popup-annotation-to-an-existing-PDF-document/Program.cs index e689118d..ced00818 100644 --- a/Annotation/Add-a-popup-annotation-to-an-existing-PDF-document/.NET/Add-a-popup-annotation-to-an-existing-PDF-document/Program.cs +++ b/Annotation/Add-a-popup-annotation-to-an-existing-PDF-document/.NET/Add-a-popup-annotation-to-an-existing-PDF-document/Program.cs @@ -4,33 +4,27 @@ using Syncfusion.Pdf.Interactive; using Syncfusion.Pdf.Parsing; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/input.pdf"), FileMode.Open, FileAccess.Read); - -//Load the PDF document. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Creates a rectangle. -RectangleF rectangle = new RectangleF(10, 40, 30, 30); - -//Creates a new popup annotation. -PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rectangle, "Test popup annotation"); -popupAnnotation.Border.Width = 4; -popupAnnotation.Border.HorizontalRadius = 20; -popupAnnotation.Border.VerticalRadius = 30; - -//Sets the pdf popup icon. -popupAnnotation.Icon = PdfPopupIcon.NewParagraph; - -//Adds the annotation to loaded page. -loadedDocument.Pages[0].Annotations.Add(popupAnnotation); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Create FileStream object to read the input PDF file +using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/input.pdf"), FileMode.Open, FileAccess.Read)) { - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); + //Load the PDF document from the input stream + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream)) + { + //Get the first page of the PDF document + PdfPageBase loadedPage = loadedDocument.Pages[0]; + //Create a new popup annotation + PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(new RectangleF(100, 100, 20, 20), "Comment"); + //Set the icon for the popup annotation + popupAnnotation.Icon = PdfPopupIcon.Comment; + //Set the color for the popup annotation + popupAnnotation.Color = new PdfColor(Color.Yellow); + //Add the annotation to the page + loadedPage.Annotations.Add(popupAnnotation); + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the PDF document to file stream. + loadedDocument.Save(outputFileStream); + } + } } - -//Close the document. -loadedDocument.Close(true); \ No newline at end of file diff --git a/Compression/Compress-the-images-in-an-existing-PDF-document/.NET/Compress-the-images-in-an-existing-PDF-document/Program.cs b/Compression/Compress-the-images-in-an-existing-PDF-document/.NET/Compress-the-images-in-an-existing-PDF-document/Program.cs index 59b42940..4cd87ac3 100644 --- a/Compression/Compress-the-images-in-an-existing-PDF-document/.NET/Compress-the-images-in-an-existing-PDF-document/Program.cs +++ b/Compression/Compress-the-images-in-an-existing-PDF-document/.NET/Compress-the-images-in-an-existing-PDF-document/Program.cs @@ -3,31 +3,39 @@ using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/imageDoc.pdf"), FileMode.Open, FileAccess.Read); - -//Load the existing PDF document. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Create a new compression option. -PdfCompressionOptions options = new PdfCompressionOptions(); - -//Enable the compress image. -options.CompressImages = true; - -//Set the image quality. -options.ImageQuality = 50; - -//Assign the compression option to the document -loadedDocument.Compress(options); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) -{ - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); -} - -//Close the document. -loadedDocument.Close(true); +// Open a file stream to read the input PDF file. +using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/imageDoc.pdf"), FileMode.Open, FileAccess.Read)) +{ + // Create a new PdfLoadedDocument object from the file stream. + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream)) + { + // Create a new PdfCompressionOptions object. + PdfCompressionOptions options = new PdfCompressionOptions(); + + // Enable image compression and set image quality. + options.CompressImages = true; + options.ImageQuality = 50; + + // Enable font optimization. + options.OptimizeFont = true; + + // Enable page content optimization. + options.OptimizePageContents = true; + + // Remove metadata from the PDF. + options.RemoveMetadata = true; + + // Compress the PDF document. + loadedDocument.Compress(options); + + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the PDF document to file stream. + loadedDocument.Save(outputFileStream); + } + //Close the document. + loadedDocument.Close(true); + } +} diff --git a/Digital Signature/Add-a-digital-signature-to-an-existing-document/.NET/Add-a-digital-signature-to-an-existing-document/Program.cs b/Digital Signature/Add-a-digital-signature-to-an-existing-document/.NET/Add-a-digital-signature-to-an-existing-document/Program.cs index 49ab6124..e5134f25 100644 --- a/Digital Signature/Add-a-digital-signature-to-an-existing-document/.NET/Add-a-digital-signature-to-an-existing-document/Program.cs +++ b/Digital Signature/Add-a-digital-signature-to-an-existing-document/.NET/Add-a-digital-signature-to-an-existing-document/Program.cs @@ -6,32 +6,41 @@ using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; -//Load the PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); +//Open existing PDF document as stream +using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) +{ + // Load the existing PDF document + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream); -//Gets the page. -PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; + // Gets the first page of the document + PdfPageBase page = loadedDocument.Pages[0]; -//Creates a signature field with properties. -PdfSignatureField signatureField = new PdfSignatureField(page, "SignatureField"); -signatureField.Bounds = new RectangleF(0, 0, 100, 100); -signatureField.Signature = new PdfSignature(); + // Load the certificate from a PFX file with a private key + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); + PdfCertificate pdfCert = new PdfCertificate(certificateStream, "password123"); -//Adds certificate to the signature field. -FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); -signatureField.Signature.Certificate = new PdfCertificate(certificateStream, "syncfusion"); -signatureField.Signature.Reason = "I am author of this document"; + // Create a signature + PdfSignature signature = new PdfSignature(loadedDocument, page, pdfCert, "Signature"); -//Adds the field. -loadedDocument.Form.Fields.Add(signatureField); + // Set signature information + signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(200, 100)); + signature.ContactInfo = "johndoe@owned.us"; + signature.LocationInfo = "Honolulu, Hawaii"; + signature.Reason = "I am the author of this document."; -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) -{ - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); -} + // Load the image for the signature field + FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/signature.png"), FileMode.Open, FileAccess.Read); + PdfBitmap signatureImage = new PdfBitmap(imageStream); + + // Draw the image on the signature field + signature.Appearance.Normal.Graphics.DrawImage(signatureImage, new RectangleF(0, 0, signature.Bounds.Width, signature.Bounds.Height)); + + // Save the document to a file stream + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + loadedDocument.Save(outputFileStream); + } -//Close the document. -loadedDocument.Close(true); \ No newline at end of file + //Close the document. + loadedDocument.Close(true); +} \ No newline at end of file diff --git a/Forms/Add-a-textbox-field-to-a-new-PDF-document/.NET/Add-a-textbox-field-to-a-new-PDF-document/Program.cs b/Forms/Add-a-textbox-field-to-a-new-PDF-document/.NET/Add-a-textbox-field-to-a-new-PDF-document/Program.cs index ae069b02..2d8fbc96 100644 --- a/Forms/Add-a-textbox-field-to-a-new-PDF-document/.NET/Add-a-textbox-field-to-a-new-PDF-document/Program.cs +++ b/Forms/Add-a-textbox-field-to-a-new-PDF-document/.NET/Add-a-textbox-field-to-a-new-PDF-document/Program.cs @@ -1,28 +1,47 @@ // See https://aka.ms/new-console-template for more information +using Syncfusion.Drawing; using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Interactive; -//Create a new PDF document. -PdfDocument document = new PdfDocument(); +// Create a new PDF document +using (PdfDocument pdfDocument = new PdfDocument()) +{ + // Add a new page to the PDF document + PdfPage pdfPage = pdfDocument.Pages.Add(); -//Add a new page to the PDF document. -PdfPage page = document.Pages.Add(); + // Set the standard font + PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16); -//Create a textbox field and add the properties. -PdfTextBoxField textBoxField = new PdfTextBoxField(page, "FirstName"); -textBoxField.Bounds = new Syncfusion.Drawing.RectangleF(0, 0, 100, 20); -textBoxField.ToolTip = "First Name"; + // Draw the string "Job Application" + pdfPage.Graphics.DrawString("Job Application", font, PdfBrushes.Black, new PointF(250, 0)); -//Add the form field to the document. -document.Form.Fields.Add(textBoxField); + // Change the font size for the form fields + font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) -{ - //Save the PDF document to file stream. - document.Save(outputFileStream); -} + // Draw the string "Name" + pdfPage.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(10, 20)); + + // Create a text box field for name + PdfTextBoxField nameField = new PdfTextBoxField(pdfPage, "Name"); + nameField.Bounds = new RectangleF(10, 40, 200, 20); + nameField.ToolTip = "Name"; + pdfDocument.Form.Fields.Add(nameField); -//Close the document. -document.Close(true); + // Draw the string "Email address" + pdfPage.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(10, 80)); + + // Create a text box field for email address + PdfTextBoxField emailField = new PdfTextBoxField(pdfPage, "Email address"); + emailField.Bounds = new RectangleF(10, 100, 200, 20); + emailField.ToolTip = "Email address"; + pdfDocument.Form.Fields.Add(emailField); + + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the PDF document to file stream. + pdfDocument.Save(outputFileStream); + } +} diff --git a/HTML to PDF/Blink/Convert-website-URL-to-PDF-document/.NET/Convert-website-URL-to-PDF-document/Program.cs b/HTML to PDF/Blink/Convert-website-URL-to-PDF-document/.NET/Convert-website-URL-to-PDF-document/Program.cs index 23fc3419..3c30cc0d 100644 --- a/HTML to PDF/Blink/Convert-website-URL-to-PDF-document/.NET/Convert-website-URL-to-PDF-document/Program.cs +++ b/HTML to PDF/Blink/Convert-website-URL-to-PDF-document/.NET/Convert-website-URL-to-PDF-document/Program.cs @@ -6,18 +6,8 @@ //Initialize HTML to PDF converter. HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); -BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings(); -if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) -{ - //Set command line arugument to run without the sandbox. - blinkConverterSettings.CommandLineArguments.Add("--no-sandbox"); - blinkConverterSettings.CommandLineArguments.Add("--disable-setuid-sandbox"); -} -//Assign Blink converter settings to HTML converter. -htmlConverter.ConverterSettings = blinkConverterSettings; -//Convert URL to PDF document. -PdfDocument document = htmlConverter.Convert("https://www.google.com"); - +//Convert URL to PDF document. +PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com"); //Create file stream. using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) { diff --git a/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF.sln b/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF.sln new file mode 100644 index 00000000..ab6be30d --- /dev/null +++ b/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert_Image_to_PDF", "Convert_Image_to_PDF\Convert_Image_to_PDF.csproj", "{AFAD39C6-48B5-4416-B8FD-F532728F46FC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AFAD39C6-48B5-4416-B8FD-F532728F46FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AFAD39C6-48B5-4416-B8FD-F532728F46FC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AFAD39C6-48B5-4416-B8FD-F532728F46FC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AFAD39C6-48B5-4416-B8FD-F532728F46FC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {53851137-F7F3-4EFF-BA66-898C77ADF8E3} + EndGlobalSection +EndGlobal diff --git a/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Convert_Image_to_PDF.csproj b/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Convert_Image_to_PDF.csproj new file mode 100644 index 00000000..c988efe5 --- /dev/null +++ b/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Convert_Image_to_PDF.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Data/Autumn Leaves.jpg b/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Data/Autumn Leaves.jpg new file mode 100644 index 00000000..3b2b7a60 Binary files /dev/null and b/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Data/Autumn Leaves.jpg differ diff --git a/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Output/.gitkeep b/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Program.cs b/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Program.cs new file mode 100644 index 00000000..cec8b91f --- /dev/null +++ b/Images/Convert_Image_to_PDF/.NET/Convert_Image_to_PDF/Program.cs @@ -0,0 +1,27 @@ +using Syncfusion.Pdf; + +// Create an instance of the ImageToPdfConverter class +ImageToPdfConverter imageToPdfConverter = new ImageToPdfConverter(); + +// Set the page size for the PDF +imageToPdfConverter.PageSize = PdfPageSize.A4; + +// Set the position of the image in the PDF +imageToPdfConverter.ImagePosition = PdfImagePosition.TopLeftCornerOfPage; + +// Create a file stream to read the image file +using (FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Autumn Leaves.jpg"), FileMode.Open, FileAccess.Read)) +{ + // Convert the image to a PDF document using the ImageToPdfConverter + PdfDocument pdfDocument = imageToPdfConverter.Convert(imageStream); + + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the PDF document to file stream. + pdfDocument.Save(outputFileStream); + } + + // Close the document + pdfDocument.Close(true); +} \ No newline at end of file diff --git a/Merge PDFs/Extend-the-margin-of-PDF-pages-while-merging-PDFs/.NET/Extend-the-margin-of-PDF-pages-while-merging-PDFs/Program.cs b/Merge PDFs/Extend-the-margin-of-PDF-pages-while-merging-PDFs/.NET/Extend-the-margin-of-PDF-pages-while-merging-PDFs/Program.cs index abf79ad5..2f73f46a 100644 --- a/Merge PDFs/Extend-the-margin-of-PDF-pages-while-merging-PDFs/.NET/Extend-the-margin-of-PDF-pages-while-merging-PDFs/Program.cs +++ b/Merge PDFs/Extend-the-margin-of-PDF-pages-while-merging-PDFs/.NET/Extend-the-margin-of-PDF-pages-while-merging-PDFs/Program.cs @@ -3,39 +3,34 @@ using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; -//Create a new PDF document. -PdfDocument finalDoc = new PdfDocument(); - -//Create new instance for the document margin. -PdfMargins margin = new PdfMargins(); -margin.All = 40; - -//Set margin. -finalDoc.PageSettings.Margins = margin; - -//Get stream from the PDF documents. -FileStream stream1 = new FileStream(Path.GetFullPath(@"Data/File1.pdf"), FileMode.Open, FileAccess.Read); -FileStream stream2 = new FileStream(Path.GetFullPath(@"Data/File2.pdf"), FileMode.Open, FileAccess.Read); - -//Create a PDF stream for merging. -Stream[] streams = { stream1, stream2 }; - -//Create merging options. -PdfMergeOptions mergeOptions = new PdfMergeOptions(); - -//Enable the Extend Margin -mergeOptions.ExtendMargin = true; - -//Merge PDFDocument. -PdfDocumentBase.Merge(finalDoc, mergeOptions, streams); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Create a new PDF document +using (PdfDocument outputDocument = new PdfDocument()) { - //Save the PDF document to file stream. - finalDoc.Save(outputFileStream); + //Create a new instance for the document margin + PdfMargins documentMargins = new PdfMargins(); + //Set the margin to 50 points on all sides + documentMargins.All = 50; + //Set the document margins + outputDocument.PageSettings.Margins = documentMargins; + //Load the first PDF document + using (FileStream firstPDFStream = new FileStream(Path.GetFullPath(@"Data/File1.pdf"), FileMode.Open, FileAccess.Read)) + { + //Load the second PDF document + using (FileStream secondPDFStream = new FileStream(Path.GetFullPath(@"Data/File2.pdf"), FileMode.Open, FileAccess.Read)) + { + //Create a list of streams to merge + Stream[] streams = { firstPDFStream, secondPDFStream }; + //Create a merge options object + PdfMergeOptions mergeOptions = new PdfMergeOptions(); + //Enable the extend margin option + mergeOptions.ExtendMargin = true; + //Merge the PDF documents + PdfDocumentBase.Merge(outputDocument, mergeOptions, streams); + //Save the document to a memory stream + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + outputDocument.Save(outputFileStream); + } + } + } } - -//Close the documents. - -finalDoc.Close(true); diff --git a/Merge PDFs/Merge-multiple-documents-from-stream/.NET/Merge-multiple-documents-from-stream/Program.cs b/Merge PDFs/Merge-multiple-documents-from-stream/.NET/Merge-multiple-documents-from-stream/Program.cs index d119df8a..db1b64b2 100644 --- a/Merge PDFs/Merge-multiple-documents-from-stream/.NET/Merge-multiple-documents-from-stream/Program.cs +++ b/Merge PDFs/Merge-multiple-documents-from-stream/.NET/Merge-multiple-documents-from-stream/Program.cs @@ -2,26 +2,22 @@ using Syncfusion.Pdf; -//Creates a PDF document. -PdfDocument finalDoc = new PdfDocument(); - -//Get stream from the PDF documents. -FileStream stream1 = new FileStream(Path.GetFullPath(@"Data/file1.pdf"), FileMode.Open, FileAccess.Read); -FileStream stream2 = new FileStream(Path.GetFullPath(@"Data/file2.pdf"), FileMode.Open, FileAccess.Read); - -//Creates a PDF stream for merging. -Stream[] streams = { stream1, stream2 }; - -//Merges documents. -PdfDocumentBase.Merge(finalDoc, streams); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) -{ - //Save the PDF document to file stream. - finalDoc.Save(outputFileStream); +//Create a PDF document +using (PdfDocument finalDocument = new PdfDocument()) +{ + //Get the stream from an existing PDF document + using (FileStream firstStream = new FileStream(Path.GetFullPath(@"Data/file1.pdf"), FileMode.Open, FileAccess.Read)) + using (FileStream secondStream = new FileStream(Path.GetFullPath(@"Data/file2.pdf"), FileMode.Open, FileAccess.Read)) + { + //Create a PDF stream for merging + Stream[] streams = { firstStream, secondStream }; + //Merge PDF documents + PdfDocumentBase.Merge(finalDocument, streams); + //Save the document into a stream + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + finalDocument.Save(outputFileStream); + } + } } -//Close the document. -finalDoc.Close(); - diff --git a/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF.sln b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF.sln new file mode 100644 index 00000000..743ddd60 --- /dev/null +++ b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Merge-specific-range-of-pages-from-different-PDF", "Merge-specific-range-of-pages-from-different-PDF\Merge-specific-range-of-pages-from-different-PDF.csproj", "{E5C1CEDD-DC26-4D6F-B45B-B0374485B753}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E5C1CEDD-DC26-4D6F-B45B-B0374485B753}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E5C1CEDD-DC26-4D6F-B45B-B0374485B753}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E5C1CEDD-DC26-4D6F-B45B-B0374485B753}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E5C1CEDD-DC26-4D6F-B45B-B0374485B753}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4082BE5E-68D6-4F71-B152-E01C124D2998} + EndGlobalSection +EndGlobal diff --git a/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Data/File1.pdf b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Data/File1.pdf new file mode 100644 index 00000000..6d10bb53 Binary files /dev/null and b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Data/File1.pdf differ diff --git a/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Data/File2.pdf b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Data/File2.pdf new file mode 100644 index 00000000..67a44946 Binary files /dev/null and b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Data/File2.pdf differ diff --git a/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Merge-specific-range-of-pages-from-different-PDF.csproj b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Merge-specific-range-of-pages-from-different-PDF.csproj new file mode 100644 index 00000000..e7b9ebab --- /dev/null +++ b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Merge-specific-range-of-pages-from-different-PDF.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + Merge_specific_range_of_pages_from_different_PDF + enable + enable + + + + + + + diff --git a/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Output/.gitkeep b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Program.cs b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Program.cs new file mode 100644 index 00000000..b99d9b0e --- /dev/null +++ b/Merge PDFs/Merge-specific-range-of-pages-from-different-PDF/.NET/Merge-specific-range-of-pages-from-different-PDF/Program.cs @@ -0,0 +1,30 @@ +using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf; + +//Create a new PDF document +using (PdfDocument finalDocument = new PdfDocument()) +{ + //Open the first PDF file + using (FileStream firstStream = new FileStream(Path.GetFullPath(@"Data/File1.pdf"), FileMode.Open, FileAccess.Read)) + { + //Load the first PDF document + PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(firstStream); + //Import a range of pages from the first PDF document into the final document + //The range starts at page 2 and ends at the last page + finalDocument.ImportPageRange(loadedDocument1, 1, loadedDocument1.Pages.Count - 1); + //Open the second PDF file + using (FileStream secondStream = new FileStream(Path.GetFullPath(@"Data/File2.pdf"), FileMode.Open, FileAccess.Read)) + { + //Load the second PDF document + PdfLoadedDocument loadedDocument2 = new PdfLoadedDocument(secondStream); + //Import a range of pages from the second PDF document into the final document. + //The range starts at page 2 and ends at page 5 + finalDocument.ImportPageRange(loadedDocument2, 2, 5); + //Save the final document into a memory stream + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + finalDocument.Save(outputFileStream); + } + } + } +} \ No newline at end of file diff --git a/Merge PDFs/Optimize-the-PDF-resources-when-merging-PDF-documents/.NET/Optimize-the-PDF-resources-when-merging-PDF-documents/Program.cs b/Merge PDFs/Optimize-the-PDF-resources-when-merging-PDF-documents/.NET/Optimize-the-PDF-resources-when-merging-PDF-documents/Program.cs index fd2e862d..abc2ea0d 100644 --- a/Merge PDFs/Optimize-the-PDF-resources-when-merging-PDF-documents/.NET/Optimize-the-PDF-resources-when-merging-PDF-documents/Program.cs +++ b/Merge PDFs/Optimize-the-PDF-resources-when-merging-PDF-documents/.NET/Optimize-the-PDF-resources-when-merging-PDF-documents/Program.cs @@ -2,31 +2,28 @@ using Syncfusion.Pdf; -//Creates a PDF document. -PdfDocument finalDoc = new PdfDocument(); - -//Get stream from an existing PDF documents. -FileStream stream1 = new FileStream(Path.GetFullPath(@"Data/File1.pdf"), FileMode.Open, FileAccess.Read); -FileStream stream2 = new FileStream(Path.GetFullPath(@"Data/File2.pdf"), FileMode.Open, FileAccess.Read); - -//Creates a PDF stream for merging. -Stream[] streams = { stream1, stream2 }; - -//Create the merge option. -PdfMergeOptions mergeOptions = new PdfMergeOptions(); - -//Enable Optimize Resources -mergeOptions.OptimizeResources = true; - -//Merges PDFDocument -PdfDocumentBase.Merge(finalDoc, mergeOptions, streams); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) -{ - //Save the PDF document to file stream. - finalDoc.Save(outputFileStream); -} - -//Close the document. -finalDoc.Close(true); \ No newline at end of file +//Create a new PDF document +using (PdfDocument outputDocument = new PdfDocument()) +{ + //Load the first PDF document + using (FileStream firstPDFStream = new FileStream(Path.GetFullPath(@"Data/File1.pdf"), FileMode.Open, FileAccess.Read)) + { + //Load the second PDF document + using (FileStream secondPDFStream = new FileStream(Path.GetFullPath(@"Data/File2.pdf"), FileMode.Open, FileAccess.Read)) + { + //Create a list of streams to merge + Stream[] streams = { firstPDFStream, secondPDFStream }; + //Create a merge options object + PdfMergeOptions mergeOptions = new PdfMergeOptions(); + //Enable the optimize resources option + mergeOptions.OptimizeResources = true; + //Merge the PDF documents + PdfDocumentBase.Merge(outputDocument, mergeOptions, streams); + //Save the document to a memory stream + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + outputDocument.Save(outputFileStream); + } + } + } +} \ No newline at end of file diff --git a/OCR/.NET/Perform-OCR-for-the-entire-PDF-document/Perform-OCR-for-the-entire-PDF-document/Program.cs b/OCR/.NET/Perform-OCR-for-the-entire-PDF-document/Perform-OCR-for-the-entire-PDF-document/Program.cs index 07832a04..a5279f00 100644 --- a/OCR/.NET/Perform-OCR-for-the-entire-PDF-document/Perform-OCR-for-the-entire-PDF-document/Program.cs +++ b/OCR/.NET/Perform-OCR-for-the-entire-PDF-document/Perform-OCR-for-the-entire-PDF-document/Program.cs @@ -3,29 +3,28 @@ using Syncfusion.OCRProcessor; using Syncfusion.Pdf.Parsing; - -//Initialize the OCR processor with tesseract binaries folder path. +// Initialize the OCR processor using (OCRProcessor processor = new OCRProcessor()) { - //Get stream from an existing PDF document. - FileStream stream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open); - - //Load the PDF document. - PdfLoadedDocument document = new PdfLoadedDocument(stream); - - //Set OCR language. - processor.Settings.Language = Languages.English; - - //Perform OCR with input document and tessdata (Language packs). - processor.PerformOCR(document); - - //Create file stream. + // Load the existing PDF document + using (FileStream stream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open)) + { + PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(stream); + + // Set OCR language to process + processor.Settings.Language = Languages.English; + + // Process OCR by providing the PDF document + processor.PerformOCR(pdfLoadedDocument); + + //Create file stream. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) { //Save the PDF document to file stream. - document.Save(outputFileStream); + pdfLoadedDocument.Save(outputFileStream); } - - //Close the document. - document.Close(true); + + //Close the document. + pdfLoadedDocument.Close(true); + } } diff --git a/PDF Conformance/Get-PDF-to-PDFA-conversion-progress/.NET/Get-PDF-to-PDFA-conversion-progress/Program.cs b/PDF Conformance/Get-PDF-to-PDFA-conversion-progress/.NET/Get-PDF-to-PDFA-conversion-progress/Program.cs index cd583438..a308f6f2 100644 --- a/PDF Conformance/Get-PDF-to-PDFA-conversion-progress/.NET/Get-PDF-to-PDFA-conversion-progress/Program.cs +++ b/PDF Conformance/Get-PDF-to-PDFA-conversion-progress/.NET/Get-PDF-to-PDFA-conversion-progress/Program.cs @@ -3,27 +3,22 @@ using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - -//Load an existing PDF document. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Subscribe to the PdfAConversionProgress event to track the PDF to PDF/A conversion process -loadedDocument.PdfAConversionProgress += new PdfLoadedDocument.PdfAConversionProgressEventHandler(pdfAConversion_TrackProgress); - -loadedDocument.ConvertToPDFA(PdfConformanceLevel.Pdf_A1B); - -//Save the document -FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write); -loadedDocument.Save(outputStream); - -//Close the document -loadedDocument.Close(true); - - -//Event handler for Track PDF to PDF/A conversion process -void pdfAConversion_TrackProgress(object sender, PdfAConversionProgressEventArgs arguments) +// Load an existing PDF document +using (FileStream inputPdfStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) { - Console.WriteLine(String.Format("PDF to PDF/A conversion process " + arguments.ProgressValue + "% completed")); -} + // Initialize PdfLoadedDocument with the input file stream + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPdfStream); + + // Convert the loaded document to PDF/A format with specified conformance level + loadedDocument.ConvertToPDFA(PdfConformanceLevel.Pdf_A1B); + + // Save the converted PDF/A document to a new file + using (FileStream outputPdfStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write)) + { + // Save the modified PDF document to the output file stream + loadedDocument.Save(outputPdfStream); + } + + // Close the loaded document to release resources + loadedDocument.Close(true); +} \ No newline at end of file diff --git a/Pages/Splitting-PDF-file-into-individual-pages/.NET/Splitting-PDF-file-into-individual-pages/Program.cs b/Pages/Splitting-PDF-file-into-individual-pages/.NET/Splitting-PDF-file-into-individual-pages/Program.cs index 0aa716ad..fd9d340b 100644 --- a/Pages/Splitting-PDF-file-into-individual-pages/.NET/Splitting-PDF-file-into-individual-pages/Program.cs +++ b/Pages/Splitting-PDF-file-into-individual-pages/.NET/Splitting-PDF-file-into-individual-pages/Program.cs @@ -3,27 +3,28 @@ using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - -//Load the PDF document. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream, true); - -for (int i = 0; i < loadedDocument.Pages.Count; i++) +// Create the FileStream object to read the input PDF file +using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { - - //Creates a new document. - PdfDocument document = new PdfDocument(); - - //Imports the pages from the loaded document. - document.ImportPage(loadedDocument, i); - - //Create a File stream. - using (var outputFileStream = new FileStream(Path.GetFullPath(@"Output/" + i + ".pdf"), FileMode.Create, FileAccess.Write)) { - //Save the document to stream. - document.Save(outputFileStream); + // Load the PDF document from the input stream + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream)) + { + // Iterate over the pages of the loaded document + for (int pageIndex = 0; pageIndex < loadedDocument.PageCount; pageIndex++) + { + // Create a new PdfDocument object to hold the output + using (PdfDocument outputDocument = new PdfDocument()) + { + // Import the page from the loadedDocument to the outputDocument + outputDocument.ImportPage(loadedDocument, pageIndex); + + // Create the FileStream object to write the output PDF file + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/" + pageIndex + ".pdf"), FileMode.Create, FileAccess.Write)) + { + // Save the outputDocument into the outputFileStream + outputDocument.Save(outputFileStream); + } + } + } } - //Close the document. - document.Close(true); - -} +} \ No newline at end of file diff --git a/Redaction/Removing-sensitive-content-from-the-PDF-document/.NET/Removing-sensitive-content-from-the-PDF-document/Program.cs b/Redaction/Removing-sensitive-content-from-the-PDF-document/.NET/Removing-sensitive-content-from-the-PDF-document/Program.cs index 868d3162..bc49831b 100644 --- a/Redaction/Removing-sensitive-content-from-the-PDF-document/.NET/Removing-sensitive-content-from-the-PDF-document/Program.cs +++ b/Redaction/Removing-sensitive-content-from-the-PDF-document/.NET/Removing-sensitive-content-from-the-PDF-document/Program.cs @@ -6,29 +6,23 @@ using Syncfusion.Pdf.Redaction; //Create stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - -//Load the existing PDF document. -PdfLoadedDocument document = new PdfLoadedDocument(docStream); - -//Get the first page from the document. -PdfLoadedPage page = document.Pages[0] as PdfLoadedPage; - -//Create a redaction object. -PdfRedaction redaction = new PdfRedaction(new RectangleF(343, 147, 60, 17), Syncfusion.Drawing.Color.Black); - -//Add a redaction object into the redaction collection of loaded page. -page.AddRedaction(redaction); - -//Redact the contents from the PDF document. -document.Redact(); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) { - //Save the PDF document to file stream. - document.Save(outputFileStream); + //Load the PDF document from stream + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream)) + { + PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage; + //Create a PDF redaction for the page + PdfRedaction redaction = new PdfRedaction(new RectangleF(340, 120, 140, 20), Color.Black); + //Add a redaction object into the redaction collection of loaded page. + loadedPage.AddRedaction(redaction); + //Redact the contents from the PDF document. + loadedDocument.Redact(); + + //Save the redacted PDF document to the memory stream + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + loadedDocument.Save(outputFileStream); + } + } } - -//Close the document. -document.Close(true); diff --git a/Security/Protect-an-existing-PDF-document/.NET/Protect-an-existing-PDF-document/Program.cs b/Security/Protect-an-existing-PDF-document/.NET/Protect-an-existing-PDF-document/Program.cs index 47b3fe0b..5932f775 100644 --- a/Security/Protect-an-existing-PDF-document/.NET/Protect-an-existing-PDF-document/Program.cs +++ b/Security/Protect-an-existing-PDF-document/.NET/Protect-an-existing-PDF-document/Program.cs @@ -3,29 +3,30 @@ using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); +// Load the PDF document from a file stream +using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) +{ + // Load the PDF document + PdfLoadedDocument document = new PdfLoadedDocument(inputFileStream); -//Load the PDF document. -PdfLoadedDocument document = new PdfLoadedDocument(docStream); + // Gets a security object for the document + PdfSecurity security = document.Security; -//PDF document security. -PdfSecurity security = document.Security; + // Configure key size and encryption algorithm + security.KeySize = PdfEncryptionKeySize.Key256Bit; + security.Algorithm = PdfEncryptionAlgorithm.AES; -//Specifies encryption key size, algorithm and permission. -security.KeySize = PdfEncryptionKeySize.Key256Bit; -security.Algorithm = PdfEncryptionAlgorithm.AES; + // Assign owner and user passwords + security.OwnerPassword = "owner123"; + security.UserPassword = "user123"; -//Provide owner and user password. -security.OwnerPassword = "ownerPassword256"; -security.UserPassword = "userPassword256"; + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the PDF document to file stream. + document.Save(outputFileStream); + } -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) -{ - //Save the PDF document to file stream. - document.Save(outputFileStream); + // Close the document + document.Close(true); } - -//Close the document. -document.Close(true); diff --git a/Table/PdfGrid/Create-table-from-data-source-in-a-PDF/.NET/Create-table-from-data-source-in-a-PDF/Program.cs b/Table/PdfGrid/Create-table-from-data-source-in-a-PDF/.NET/Create-table-from-data-source-in-a-PDF/Program.cs index b7db28da..5fef03a9 100644 --- a/Table/PdfGrid/Create-table-from-data-source-in-a-PDF/.NET/Create-table-from-data-source-in-a-PDF/Program.cs +++ b/Table/PdfGrid/Create-table-from-data-source-in-a-PDF/.NET/Create-table-from-data-source-in-a-PDF/Program.cs @@ -5,38 +5,31 @@ using Syncfusion.Pdf.Grid; using System.Data; -//Create a new PDF document. -PdfDocument document = new PdfDocument(); - -//Add a page. -PdfPage page = document.Pages.Add(); - -//Create a PdfGrid. -PdfGrid pdfGrid = new PdfGrid(); - -//Create a DataTable. -DataTable dataTable = new DataTable(); - -//Add columns to the DataTable. -dataTable.Columns.Add("ID"); -dataTable.Columns.Add("Name"); - -//Add rows to the DataTable. -dataTable.Rows.Add(new object[] { "E01", "Clay" }); -dataTable.Rows.Add(new object[] { "E02", "Thomas" }); - -//Assign data source. -pdfGrid.DataSource = dataTable; - -//Draw grid to the page of PDF document. -pdfGrid.Draw(page, new PointF(10, 10)); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +//Create a new PDF document +using (PdfDocument document = new PdfDocument()) { - //Save the PDF document to file stream. - document.Save(outputFileStream); + //Add a page + PdfPage page = document.Pages.Add(); + + //Create a PdfGrid + PdfGrid pdfGrid = new PdfGrid(); + + //Add values to the list + List data = new List(); + data.Add(new { ID = "E01", Name = "Clay" }); + data.Add(new { ID = "E02", Name = "Thomas" }); + data.Add(new { ID = "E03", Name = "John" }); + + //Assign the data source + pdfGrid.DataSource = data; + + //Draw the grid to the page of PDF document + pdfGrid.Draw(page, new PointF(10, 10)); + + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the PDF document to file stream. + document.Save(outputFileStream); + } } - -//Close the document. -document.Close(true); \ No newline at end of file diff --git a/Tagged PDF/Add-tag-for-the-text-element-in-PDF-document/.NET/Add-tag-for-the-text-element-in-PDF-document/Program.cs b/Tagged PDF/Add-tag-for-the-text-element-in-PDF-document/.NET/Add-tag-for-the-text-element-in-PDF-document/Program.cs index e693c2fa..4610488d 100644 --- a/Tagged PDF/Add-tag-for-the-text-element-in-PDF-document/.NET/Add-tag-for-the-text-element-in-PDF-document/Program.cs +++ b/Tagged PDF/Add-tag-for-the-text-element-in-PDF-document/.NET/Add-tag-for-the-text-element-in-PDF-document/Program.cs @@ -4,44 +4,42 @@ using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; -//Creates new PDF document. -PdfDocument document = new PdfDocument(); - -//Set the document title. -document.DocumentInformation.Title = "PdfTextElement"; +// Create new PDF document +using (PdfDocument document = new PdfDocument()) +{ + // Set the document title + document.DocumentInformation.Title = "PdfTextElement"; -//Creates new page. -PdfPage page = document.Pages.Add(); + // Create a new page + PdfPage page = document.Pages.Add(); -//Initialize the structure element with tag type paragraph. -PdfStructureElement structureElement = new PdfStructureElement(PdfTagType.Paragraph); + // Initialize the structure element with tag type paragraph + PdfStructureElement paragraphStructure = new PdfStructureElement(PdfTagType.Paragraph); -//Represents the text that is exact replacement for PdfTextElement. -structureElement.ActualText = "Simple paragraph element"; + // Represents the text that is the exact replacement for PdfTextElement + paragraphStructure.ActualText = "Simple paragraph element"; -string text = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base."; + string paragraphText = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European, and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base."; -//Initialize the PDF text element. -PdfTextElement element = new PdfTextElement(text); + // Initialize the PDF text element + PdfTextElement textElement = new PdfTextElement(paragraphText); -//Adding tag to the text element. -element.PdfTag = structureElement; + // Adding tag to the text element + textElement.PdfTag = paragraphStructure; -//Creates font for the text element. -element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); + // Create font for the text element + textElement.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); -//Create brush for the text element. -element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93)); + textElement.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93)); -//Draws text element. -PdfLayoutResult result = element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width, 200)); + // Draw text element with tag + textElement.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width, 200)); -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) -{ - //Save the PDF document to file stream. - document.Save(outputFileStream); + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the PDF document to file stream. + document.Save(outputFileStream); + } } -//Close the document. -document.Close(true); diff --git a/Text Extraction/Extract-text-from-the-entire-PDF-document/.NET/Extract-text-from-the-entire-PDF-document/Program.cs b/Text Extraction/Extract-text-from-the-entire-PDF-document/.NET/Extract-text-from-the-entire-PDF-document/Program.cs index ea0b6039..127b9dbe 100644 --- a/Text Extraction/Extract-text-from-the-entire-PDF-document/.NET/Extract-text-from-the-entire-PDF-document/Program.cs +++ b/Text Extraction/Extract-text-from-the-entire-PDF-document/.NET/Extract-text-from-the-entire-PDF-document/Program.cs @@ -3,26 +3,22 @@ using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - -//Load the PDF document. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Loading page collections. -PdfLoadedPageCollection loadedPages = loadedDocument.Pages; - -string extractedText = string.Empty; - -//Extract text from existing PDF document pages. -foreach (PdfLoadedPage loadedPage in loadedPages) +// Open existing PDF document stream. +using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) { - extractedText += loadedPage.ExtractText(); + // Load the PDF document. + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream)) + { + string extractedText = string.Empty; + + // Extract all text from PDF document pages. + foreach (PdfLoadedPage page in loadedDocument.Pages) + { + extractedText += page.ExtractText(); + } + + //Write the extracted text in console window. + Console.WriteLine("Extracted text from the entire document: " + extractedText); + Console.ReadLine(); + } } - -//Close the document. -loadedDocument.Close(true); - -//Write the extracted text in console window. -Console.WriteLine("Extracted text from the entire document: " + extractedText); -Console.ReadLine(); diff --git a/Watermark/Add-text-watermark-in-an-existing-PDF-document/.NET/Add-text-watermark-in-an-existing-PDF-document/Program.cs b/Watermark/Add-text-watermark-in-an-existing-PDF-document/.NET/Add-text-watermark-in-an-existing-PDF-document/Program.cs index e409c894..b5b97f4a 100644 --- a/Watermark/Add-text-watermark-in-an-existing-PDF-document/.NET/Add-text-watermark-in-an-existing-PDF-document/Program.cs +++ b/Watermark/Add-text-watermark-in-an-existing-PDF-document/.NET/Add-text-watermark-in-an-existing-PDF-document/Program.cs @@ -5,38 +5,41 @@ using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing; -//Get stream from an existing PDF document. -FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); - -//Load the PDF document. -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); - -//Load the page. -PdfPageBase loadedPage = loadedDocument.Pages[0]; - -//Create graphics for PDF page. -PdfGraphics graphics = loadedPage.Graphics; - -//Set the font. -PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); - -//Watermark text. -PdfGraphicsState state = graphics.Save(); - -//Set transparency and rotate transform to page graphics. -graphics.SetTransparency(0.25f); -graphics.RotateTransform(-40); - -//Draw watermark text in the PDF document. -graphics.DrawString("Imported using Essential PDF", font, PdfPens.Red, PdfBrushes.Red, new PointF(-150, 450)); - -//Create file stream. -using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) +// Create FileStream object to read the input PDF file +using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) { - //Save the PDF document to file stream. - loadedDocument.Save(outputFileStream); + // Load the PDF document from the input stream + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream)) + { + // Get the first page of the PDF document + PdfPageBase loadedPage = loadedDocument.Pages[0]; + + // Create a PDF font to add a watermark text. + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 32); + + // Measure the watermark text and get the size + string watermarkText = "Created using Syncfusion PDF library"; + SizeF watermarkTextSize = font.MeasureString(watermarkText); + + // Move the graphics to the center of the page + loadedPage.Graphics.Save(); + loadedPage.Graphics.TranslateTransform(loadedPage.Size.Width / 2, loadedPage.Size.Height / 2); + + // Rotate the graphics to 45 degrees + loadedPage.Graphics.RotateTransform(-45); + + // Draw the watermark text + loadedPage.Graphics.DrawString(watermarkText, font, PdfBrushes.Red, new PointF(-watermarkTextSize.Width / 2, -watermarkTextSize.Height / 2)); + + // Restore the graphics + loadedPage.Graphics.Restore(); + + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the PDF document to file stream. + loadedDocument.Save(outputFileStream); + } + } } -//Close the document. -loadedDocument.Close(true); -