Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Apr 18, 2019 1:54 pm

Anyway to extract and replace editable text in a PDF file in java.

Also anyway to find/identify any specific image and replace it?

Thanks alot.

Safder
 
Posts: 21
Joined: Fri Sep 28, 2018 8:08 am

Fri Apr 19, 2019 9:37 am

Hi,

Thanks for your inquiry.
1) Our Spire.PDF for Java supports extracting text from PDF. Sorry there is no direct way to replace text, yet you could cover the old text with new text to achieve the effect. Please refer to the following code.
Code: Select all
public static void main(String[] args) throws Exception {
            PdfDocument pdf = new PdfDocument();
            pdf.loadFromFile("input/input.pdf");
            //Extract text
            ExtractText(pdf);
            //Replace text
            ReplaceText(pdf);
            //save to file
            pdf.saveToFile("output/Result.pdf");
            pdf.close();
    }
    private static void ExtractText(PdfDocument pdf) throws Exception{
        StringBuilder builder=new StringBuilder();
        for (PdfPageBase page : (Iterable<PdfPageBase>) pdf.getPages())  {
            builder.append(page.extractText(true));
        }
        //write to txt
        FileWriter writer;
        writer = new FileWriter("output/ExtractText.txt");
        writer.write(builder.toString());
        writer.flush();
    }
    private static void ReplaceText(PdfDocument pdf) throws Exception {
        PdfTextFind[] result = null;
        for (PdfPageBase page : (Iterable<PdfPageBase>) pdf.getPages())  {
            result = page.findText("OldText").getFinds();
            for (PdfTextFind find : result) {
                //cover the old text with new text
                find.applyRecoverString("NewText", Color.white, true);
            }
        }
    }

2) The following sample code shows how to replace image.
Code: Select all
        //Load the PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("input/input.pdf");
        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);
        //Load an image
        PdfImage image = PdfImage.fromFile("input/sample.jpg");
        //Replace the first image in the first page with the loaded image
        page.replaceImage(0, image);
        //Save the file
        pdf.saveToFile("output/ReplaceImage.pdf");

If there is any question, please feel free to write back.

Sincerely,
Nancy
E-iceblue support team
User avatar

nancy.yang
 
Posts: 184
Joined: Wed Apr 03, 2019 2:33 am

Fri Apr 19, 2019 11:41 am

Thanks.

For the replace function, original "font formatting(Bold, Italic, Underline)" is replaced by the default format ? (i.e. It doesn't perfectly replace the Font.)

Is there any way around it

Safder
 
Posts: 21
Joined: Fri Sep 28, 2018 8:08 am

Mon Apr 22, 2019 7:01 am

Hi,

Thanks for your feedback.
I have noticed that the new text could not keep the same font and style as the original text. I will post the issue to our Dev team for investigation. If it is resolved or there is any update for you, I will let you know. Besides, could you please provide your input Pdf file and tell us the text you want to replace? So that we can better build the bug according to your document. You could send it to me via email ([email protected]). Thanks in advance.

Sincerely,
Nancy
E-iceblue support team
User avatar

nancy.yang
 
Posts: 184
Joined: Wed Apr 03, 2019 2:33 am

Mon Apr 29, 2019 9:39 am

Alright but can you simple just update me over here . As soon as you have any update regarding this .

Safder
 
Posts: 21
Joined: Fri Sep 28, 2018 8:08 am

Mon Apr 29, 2019 11:00 am

Hi,

Thank you for your inquiry.
Sorry that there is no update at present, our dev team is still working on it. Please spare us more time. I will keep you informed regarding any available updates. Thanks for your understanding.

Sincerely,
Nancy
E-iceblue support team
User avatar

nancy.yang
 
Posts: 184
Joined: Wed Apr 03, 2019 2:33 am

Wed Aug 28, 2019 9:17 am

Hey Spire.PDF for java went paid suddenly? i used the library from maven

Safder
 
Posts: 21
Joined: Fri Sep 28, 2018 8:08 am

Wed Aug 28, 2019 9:57 am

Hi Safder,

Thanks for your inquiry.
Now installing from Maven has some troubles, we are adjusting it.
Please download free Spire.PDF V2.6.3 from our website, then add the .jar in your project:
https://www.e-iceblue.com/Download/pdf- ... -free.html

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Sat Oct 12, 2019 2:00 am

//直线
Point lineSta = new Point((int) (sx + p + z), 0);
Point lineEnd = new Point((int) (sx + l - z - p), 0);
page.getCanvas().drawLine(pen, lineSta, lineEnd);
用java在ptf上画线条,线条的两个端点(x,y)坐标只能是整数,要如何设置端点坐标可以是 小数呢?

qyq971999
 
Posts: 4
Joined: Thu Oct 10, 2019 10:27 am

Sat Oct 12, 2019 9:56 am

你好,

感谢来信。
请参考以下代码在Java中将端点值坐标设置成小数。

Code: Select all
Point2D pStart=new Point2D.Float(123.12f,12.33f);
Point2D pEnd=new Point2D.Float(100.12f,23.63f);
page.getCanvas().drawLine(pen, pStart, pEnd);


Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Tue Oct 15, 2019 5:22 am

谢谢,问题已经解决。
我有2个点,想画一个经过两点的一条弧线,能完成吗?

qyq971999
 
Posts: 4
Joined: Thu Oct 10, 2019 10:27 am

Tue Oct 15, 2019 6:48 am

您好,

感谢咨询。
请参考以下代码在Java中绘制一条弧线。

Code: Select all
float startAngle = 0;
float sweepAngle = 110;
Rectangle2D.Float rect3= new Rectangle2D.Float(0, 110, 50, 50);
page.getCanvas().drawArc(pen, rect3, startAngle, sweepAngle);


如果这个代码不能满足你的需求,请向我们提你期望的结果文档以便进一步调查。您可以在这里上传文件或者通过电子邮件发送给我们([email protected])。

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Mon Oct 21, 2019 3:10 am

我想更改PDF文件的生成路径,需要如何完成呢?

qyq971999
 
Posts: 4
Joined: Thu Oct 10, 2019 10:27 am

Mon Oct 21, 2019 8:53 am

你好,

请参考以下代码在Java中设置PDF文件的生成路径。

Code: Select all
String result="E:\\filePath\\filename.pdf";
doc.saveToFile(result, FileFormat.PDF);


如果我误解了,请详细描述你的需求。

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Wed Oct 23, 2019 3:12 am

请问:我想自定义设置纸张大小**mmx**mm,而不是固定的A*类纸张,如何完成呢?

qyq971999
 
Posts: 4
Joined: Thu Oct 10, 2019 10:27 am

Return to Spire.PDF

cron