#1
Posted
:
Wednesday, August 21, 2019 1:32:37 PM(UTC)
Groups: Registered
Posts: 2
I am trying to load a PDFDocument from a PDF, then edit that PDF and save the edited PDF to my machine.
For my example I have a PDF that has 1 page and some text on it, I see the text in the PDFDocument as Page Objects.
I want to delete all text from the PDF and save it, just to demonstrate that I can edit a PDF and save the result .
Code:
var path = new DirectoryInfo(Path.Combine("..|StaticFiles|TestPDF.pdf".Split('|')));
var pdfDocument = new PDFDocument(path.ToString());
var firstPage = pdfDocument.Pages.Select(e => e.PageNumber).Min();
var lastPage = pdfDocument.Pages.Select(e => e.PageNumber).Max();
pdfDocument.ParsePages(PDFParsePagesOptions.All, firstPage, lastPage);
foreach (var page in pdfDocument.Pages)
{
page.Objects.Clear();
}
var path2 = new DirectoryInfo(Path.Combine("..|StaticFiles|TestPDF1.pdf".Split('|')));
if (System.IO.File.Exists(path2.ToString()))
{
// So we won't append to it twice
System.IO.File.Delete(path2.ToString());
}
using (RasterCodecs codecs = new RasterCodecs())
{
// For each page...
for (int i = 1; i <= pdfDocument.Pages.Count; i++)
{
// Get the page as a raster image
using (RasterImage image = pdfDocument.GetPageImage(codecs, i))
{
// Save it to the destination file
codecs.Save(image, path2.ToString(), RasterImageFormat.RasPdf, 24, 1, 1, -1, CodecsSavePageMode.Append);
}
}
}
This code will save the PDF in the selected location, but the pdf is not altered. How can I edit the PDFDocument and Save its result?
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.