LEADTOOLS Support
General
General Questions
How to Edit PDFDocument and Save Result?
#1
Posted
:
Wednesday, August 21, 2019 1:33:40 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?
#2
Posted
:
Thursday, August 22, 2019 8:01:16 AM(UTC)
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.htmlYou 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.htmlIf you have any further questions please feel free to reach back out to us.
Thanks
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS Support
General
General Questions
How to Edit PDFDocument and Save 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.