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

Notification

Icon
Error

Options
Go to last post Go to first unread
Kiran  
#1 Posted : Thursday, November 10, 2016 1:20:02 AM(UTC)
Kiran

Rank: Newbie

Groups: Registered
Joined: 11/9/2016(UTC)
Posts: 3
India
Location: Hyderabad

Hi.

Using Patagames.Pdf, can we edit Plain Text and Replace PDFTextobject in PDF File.
Example : AAAA word needs to replace with BBBB

Regards
Kiran.
Paul Rayman  
#2 Posted : Thursday, November 10, 2016 7:02:34 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 844

Thanks: 2 times
Was thanked: 103 time(s) in 101 post(s)
Unfortunately there is no a simple way to do that.
You can replace the text, it will be even shown in PdfViewer, but these changes can't be saved. So they would be lost after page disposing.
Guest  
#3 Posted : Friday, November 11, 2016 10:03:20 AM(UTC)
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 139

Was thanked: 4 time(s) in 4 post(s)
Hi

Let us know how to Replace the Text (AAAA TO BBBB) for viewing in Viewer using Patagames.PDF, after viewing the Replaced Text in Viewer, once print is done the changes will reflect or not ?
Paul Rayman  
#4 Posted : Saturday, November 12, 2016 10:27:10 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 844

Thanks: 2 times
Was thanked: 103 time(s) in 101 post(s)
The following code illustrates how to search and replace text in whole document and then print.
Please note, you need to upgrade the Pdfium.Net SDK to the latest version.
http://forum.patagames.c...released-on-Nov-13--2016

Code:

private void ReplaceTextAndPrint(PdfDocument pdfDocument)
{
	PrintDialog dlg = new PrintDialog();
	PdfPrintDocument print = new PdfPrintDocument(pdfDocument);
	print.BeforeRenderPage += (s, e) =>
		{
			foreach(var obj in e.Page.PageObjects)
			{
				var textObj = obj as PdfTextObject;
				if (textObj == null)
					continue;
				textObj.TextUnicode = textObj.TextUnicode.Replace("AAAA", "BBBB");
			}
		};
	dlg.Document = print;
	dlg.AllowCurrentPage = true;
	dlg.AllowSomePages = true;
	dlg.UseEXDialog = true;
	if (dlg.ShowDialog() == DialogResult.OK)
		print.Print();
}
Kiran  
#5 Posted : Sunday, November 13, 2016 6:42:48 AM(UTC)
Kiran

Rank: Newbie

Groups: Registered
Joined: 11/9/2016(UTC)
Posts: 3
India
Location: Hyderabad

The changes are not getting reflected after replacing the Text and Printing to PDF. is it possible to save to PDF ?

Regards
Kiran.
Paul Rayman  
#6 Posted : Sunday, November 13, 2016 10:42:30 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 844

Thanks: 2 times
Was thanked: 103 time(s) in 101 post(s)
As I told before there is no a simple way to save these chages. It can be printed or viewed in PdfViewer control only.

Edited by user Sunday, November 13, 2016 10:43:23 AM(UTC)  | Reason: Not specified

Guest  
#7 Posted : Sunday, November 13, 2016 11:27:14 PM(UTC)
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 139

Was thanked: 4 time(s) in 4 post(s)
After replacing the text The changes are not getting reflected while printing to current printer from PDF Viewer.
Paul Rayman  
#8 Posted : Sunday, November 13, 2016 11:59:08 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 844

Thanks: 2 times
Was thanked: 103 time(s) in 101 post(s)
Originally Posted by: Guest Go to Quoted Post
After replacing the text The changes are not getting reflected while printing to current printer from PDF Viewer.


The code above illustrates the printing only, without viewing in Pdfviewer.
If you want to view these changes in PdfViewer you should write the following code.
This happens because the printing engine and PdfViewer uses distinct instances of the PdfPage.

Code:

public Form1()
{
	PdfCommon.Initialize();
	InitializeComponent();

	pdfViewer1.BeforeDocumentChanged += (s, e) => 
	{
		if(pdfViewer1.Document!= null)
			pdfViewer1.Document.Pages.PageLoaded -= Pages_PageLoaded;
	};
	pdfViewer1.AfterDocumentChanged += (s, e) => 
	{
		if (pdfViewer1.Document != null)
			pdfViewer1.Document.Pages.PageLoaded += Pages_PageLoaded;
	};
}

private void Pages_PageLoaded(object sender, Patagames.Pdf.Net.EventArguments.PdfPageEventArgs e)
{
	foreach (var obj in e.Page.PageObjects)
	{
		var textObj = obj as PdfTextObject;
		if (textObj == null)
			continue;
		textObj.TextUnicode = textObj.TextUnicode.Replace("AAAA", "BBBB");
	}
}


So if you want to view the changes and then print them, then you need to combine these two samples to achieve your goal.

Also, if you want to print document using PdfToolStripMain (PdfToolbarMain for WPF), then you need to create a derived class from it and override the OnPrintClick method (https://pdfium.patagames.com/help/html/3e322275-202a-3ac4-bb09-b6d40b06d739.htm) within which you should write the code from the first sample (The call of the ReplaceTextAndPrint method)

Edited by user Monday, November 14, 2016 12:07:06 AM(UTC)  | Reason: Not specified

Guest  
#9 Posted : Thursday, February 23, 2017 6:39:40 AM(UTC)
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 139

Was thanked: 4 time(s) in 4 post(s)
Hi,

I'm trying to use this method of changing the text in a PDF but as soon as the replace is done the size of the BoundingBox is changed. How can I prevent this?
MeJonah  
#10 Posted : Thursday, November 14, 2019 4:29:54 PM(UTC)
MeJonah

Rank: Newbie

Groups: Registered
Joined: 2/18/2019(UTC)
Posts: 3
United States
Location: Minnesota

Sorry to raise this from the dead, but I'm trying to modify text while printing. If I reference e.Page.PageObjects at all, I get empty pages. This is enough to cause it to happen (modification of post #4 above):

Quote:

print.BeforeRenderPage += (s, e) =>
{
foreach(var obj in e.Page.PageObjects)
{
}
};


Any help?
Paul Rayman  
#11 Posted : Sunday, November 17, 2019 11:48:01 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 844

Thanks: 2 times
Was thanked: 103 time(s) in 101 post(s)
This is a bug.
Will be fixed in a few days.

Edited by user Monday, November 18, 2019 4:22:47 AM(UTC)  | Reason: Not specified

Paul Rayman  
#12 Posted : Wednesday, November 20, 2019 9:18:08 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 844

Thanks: 2 times
Was thanked: 103 time(s) in 101 post(s)
Users browsing this topic
Forum Jump  
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.