Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Wednesday, August 21, 2019 1:32:37 PM(UTC)

hasmyr  
hasmyr

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?
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

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.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.039 seconds.