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:33:40 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.

#2 Posted : Thursday, August 22, 2019 8:01:16 AM(UTC)
Matthew Bresson

Groups: Registered, Tech Support, Administrators
Posts: 105

Was thanked: 3 time(s) in 3 post(s)

Hello,

Your code does clear the objects from your PDF Document (I tested this personally), however we do not support saving the modified object list. This is why you are just seeing a rasterized PDF copy of your original PDF.

If you want to clear the page your best option would be to replace the specified PDF with a blank page. You can achieve this Document Factory class.
https://www.leadtools.co...dox/documentfactory.html
You will have to create a new blank document and then add that document page to the existing PDF. The link below provides a long code snippet that can assist you in this process if you choose to go this route.
https://www.leadtools.co...x/documentfactory-create(createdocumentoptions).html

Another option is to load the PDF as SVG try to modify the document accordingly.
https://www.leadtools.co...dh/svg/introduction.html

If you have any further questions please feel free to reach back out to us.

Thanks
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS
 
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.058 seconds.