private static void Main() { if (File.Exists(ResultFileName)) { File.Delete(ResultFileName); } using (PdfStreamWriter fileWriter = new PdfStreamWriter(File.OpenWrite(ResultFileName))) { fileWriter.Settings.DocumentInfo.Author = "Progress Software"; fileWriter.Settings.DocumentInfo.Title = "Merged document"; fileWriter.Settings.DocumentInfo.Description = "This big document is generated with PdfStreamWriter class in less than a second, with minimal memory footprint and optimized result file size."; using (PdfFileSource fileSource = new PdfFileSource(File.OpenRead(InputFileName))) { PdfPageSource pageToMerge = fileSource.Pages[0]; for (int i = 0; i < MergedDocumentPagesCount; i++) { fileWriter.WritePage(pageToMerge); } } } ProcessStartInfo psi = new ProcessStartInfo() { FileName = ResultFileName, UseShellExecute = true }; Process.Start(psi); }
private static void MergeDifferentDocumentsPages(string resultFileName) { string[] documentsToMerge = { InputFileSoundVideoAnd3D, InputFileBarChart, InputFileCentaurFeatures, InputFileMultipageDocument }; string resultFile = Path.Combine(ResultDirName, resultFileName); using (PdfStreamWriter fileWriter = new PdfStreamWriter(File.OpenWrite(resultFile))) { foreach (string documentName in documentsToMerge) { using (PdfFileSource fileToMerge = new PdfFileSource(File.OpenRead(documentName))) { foreach (PdfPageSource pageToMerge in fileToMerge.Pages) { fileWriter.WritePage(pageToMerge); } } } } }
private static void SplitDocumentPages(string resultFileFormat) { string documentToSplit = InputFileMultipageDocument; using (PdfFileSource fileToSplit = new PdfFileSource(File.OpenRead(documentToSplit))) { for (int i = 0; i < fileToSplit.Pages.Length; i++) { PdfPageSource page = fileToSplit.Pages[i]; string splitDocumentName = Path.Combine(ResultDirName, string.Format(resultFileFormat, i + 1)); using (PdfStreamWriter fileWriter = new PdfStreamWriter(File.OpenWrite(splitDocumentName))) { fileWriter.WritePage(page); } } } }
private static void PrependAndAppendPageContent(string resultFileName) { string initialDocument = InputFileMultipageDocument; string resultDocument = Path.Combine(ResultDirName, resultFileName); using (PdfStreamWriter fileWriter = new PdfStreamWriter(File.OpenWrite(resultDocument))) { using (PdfFileSource fileSource = new PdfFileSource(File.OpenRead(initialDocument))) { RadFixedPage backgroundContentOwner = GenerateBackgroundImageContent(InputFileBackgroundImage); RadFixedPage foregroundContentOwner = GenerateForegroundTextContent("WATERMARK"); foreach (PdfPageSource pageSource in fileSource.Pages) { using (PdfPageStreamWriter pageWriter = fileWriter.BeginPage(pageSource.Size, pageSource.Rotation)) { using (pageWriter.SaveContentPosition()) { double xCenteringTranslation = (pageSource.Size.Width - backgroundContentOwner.Size.Width) / 2; double yCenteringTranslation = (pageSource.Size.Height - backgroundContentOwner.Size.Height) / 2; pageWriter.ContentPosition.Translate(xCenteringTranslation, yCenteringTranslation); pageWriter.WriteContent(backgroundContentOwner); } pageWriter.WriteContent(pageSource); using (pageWriter.SaveContentPosition()) { double xCenteringTranslation = (pageSource.Size.Width - foregroundContentOwner.Size.Width) / 2; double yCenteringTranslation = (pageSource.Size.Height - foregroundContentOwner.Size.Height) / 2; pageWriter.ContentPosition.Translate(xCenteringTranslation, yCenteringTranslation); pageWriter.WriteContent(foregroundContentOwner); } } } } } }
private static void FitAndPositionMultiplePagesOnSinglePage(string resultFileName) { string initialDocument = InputFileMultipageDocument; string resultDocument = Path.Combine(ResultDirName, resultFileName); using (PdfStreamWriter fileWriter = new PdfStreamWriter(File.OpenWrite(resultDocument))) { using (PdfFileSource fileSource = new PdfFileSource(File.OpenRead(initialDocument))) { double halfWidth = fileSource.Pages[0].Size.Width / 2; double halfHeight = fileSource.Pages[0].Size.Height / 2; Vector[] translateDirections = new Vector[] { new Vector(0, 0), new Vector(1, 0), new Vector(0, 1), new Vector(1, 1) }; PdfPageStreamWriter resultPage = null; for (int i = 0; i < fileSource.Pages.Length; i++) { if (i % 4 == 0) { if (i > 0) { resultPage.Dispose(); } resultPage = fileWriter.BeginPage(fileSource.Pages[0].Size, fileSource.Pages[0].Rotation); resultPage.ContentPosition.Scale(0.5, 0.5); } Vector direction = translateDirections[i % 4]; resultPage.ContentPosition.Translate(direction.X * halfWidth, direction.Y * halfHeight); PdfPageSource sourcePage = fileSource.Pages[i]; resultPage.WriteContent(sourcePage); } resultPage.Dispose(); } } }