Aspose Demos > Java Components > Aspose.Pdf.Kit for Java > Java > Change PDF Metadata

Change PDF Metadata - Aspose.Pdf.Kit

This demo shows how to Change Metadata information of input Pdf document. Using PdfFileInfo class of Aspose.Pdf.Kit component, developers can change the meta information of the input PDF document.

Fore more information, please visit Change PDF Information.

Enter PDF metadata and click Execute Demo below to see how demo takesinput PDF file adds metadata information and sends the resulting document to user for review.

Key Value
T itle
A uthor
S ubject
K eywords
C reator











JAVA

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

package com.aspose.pdf.kit.demos.jsf;

import java.io.Serializable;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.aspose.pdf.kit.*;

import javax.faces.context.FacesContext;
import javax.faces.event.*;
import javax.faces.component.UIComponent;

public class PdfInfoChange extends BaseDemo implements Serializable 
{
    // Default Constructor
    public PdfInfoChange() 
    {
        try
        {
            String path = "/resources/";

            FacesContext context = FacesContext.getCurrentInstance();
            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

            PdfFileInfo fileInfo = new PdfFileInfo(
                getFile(request, path + "Aspose.Pdf.Kit.pdf"));
            setTitle(fileInfo.getTitle());
            setSubject(fileInfo.getSubject());
            setAuthor(fileInfo.getAuthor());
            setKeywords(fileInfo.getKeywords());
            setCreator(fileInfo.getCreator());

        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    private String _title;
    public String getTitle() {
        return _title;
    }
    public void setTitle(String title) {
        _title = title;
    }
   
    private String _author;
    public String getAuthor() {
        return _author;
    }
    public void setAuthor(String author) {
        _author = author;
    }
   
    private String _keywords;
    public String getKeywords() {
        return _keywords;
    }
    public void setKeywords(String keywords) {
        _keywords = keywords;
    }
   
    private String _creator;
    public String getCreator() {
        return _creator;
    }
    public void setCreator(String creator) {
        _creator = creator;
    }
   
    private String _subject;
    public String getSubject() {
        return _subject;
    }
    public void setSubject(String subject) {
        _subject = subject;
    }
   
    /**
     * Execute request
     *
     * @return success
     */
    public Boolean execute(HttpServletRequest request, HttpServletResponse response) 
    {
        try
        {
            String path = "/resources/";

            PdfFileInfo fileInfo = new PdfFileInfo(
                getFile(request, path + "Aspose.Pdf.Kit.pdf"));
    
            if (getTitle() == null) {
              fileInfo.setTitle("");
            }
            else {
              fileInfo.setTitle(getTitle());
            }
            
            if (getAuthor() == null) {
                fileInfo.setAuthor("");
            } else {
                fileInfo.setAuthor(getAuthor());
            }

            if (getSubject()== null) {
                fileInfo.setSubject("");
            } else {
                fileInfo.setSubject(getSubject());
            }

            if (getKeywords() == null) {
                fileInfo.setKeywords("");
            } else {
                fileInfo.setKeywords(getKeywords());
            }

            if (getCreator() == null) {
                fileInfo.setCreator("");
            } else {
                fileInfo.setCreator(getCreator());
            }

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            fileInfo.saveNewInfo(outputStream);
            setResponse(FILE_NAME, outputStream);

            return true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return false;
    }
}

JAVA

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

package com.aspose.pdf.kit.demos.jsf;

import java.io.Serializable;

import java.io.*;
import java.util.*;
import java.net.URL;

import javax.servlet.*;
import javax.servlet.http.*;

import java.text.SimpleDateFormat;

import javax.faces.context.FacesContext;

import com.icesoft.faces.context.ByteArrayResource;
import com.icesoft.faces.context.Resource;

import java.lang.reflect.*;

public abstract class BaseDemo
{
    // Default Constructor
    public BaseDemo() {
        initLicense(new com.aspose.pdf.kit.License());
    }

    public abstract Boolean execute(HttpServletRequest request, HttpServletResponse response);

    protected String getFileName(HttpServletRequest request, String absoluteName)
        throws java.io.FileNotFoundException
    {
        URL url = getClass().getClassLoader().getResource(absoluteName);
        if (null==url && null!=request)
        {
            String path =
                request.getRealPath(absoluteName).replace("\\", "/");

            System.err.println("Failed to load " + absoluteName);
            System.err.println("Loading file from " + path);

            return path;
        }

        String path = new File(url.getPath()).getAbsolutePath();
        //return url.toString();
        return path;
    }

    protected InputStream getFile(HttpServletRequest request, String absoluteName)
        throws java.io.FileNotFoundException
    {
        InputStream is = getClass().getClassLoader().getResourceAsStream(absoluteName);
        if (null==is && null!=request)
        {
            String path =
                request.getRealPath(absoluteName).replace("\\", "/");

            System.err.println("Failed to load " + absoluteName);
            System.err.println("Loading file from " + path);
            is = new FileInputStream(path);
        }

        return is;
    }

    public String generate() {

        FacesContext context = FacesContext.getCurrentInstance();

        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

        try
        {
            System.out.println("BaseGenerate called for " + this.toString());

            if (execute(request, response))
            {
                //context.responseComplete();
            }


        }catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return "stay";
    }

    protected final String FILE_NAME = "result.pdf";

    public Resource getGeneratedfile() {
        byte[] res = getResult();
        if (null==res || res.length<1) {
            return null;
        }
        return new ByteArrayResource(res);
    }

    private String fileName = FILE_NAME;
    public String getFilename() {
        return fileName;
    }

    public void setFilename(String v) {

        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MMM-dd_hh_mm_ss-", Locale.US);
        String curDate = formatter.format(new Date());

        this.fileName = curDate + v;
    }

    public  byte[] getResult()
    {
        return response;
    }

    protected void setResponse(String fName, ByteArrayOutputStream out) throws IOException {
        setFilename(fName);
        response = out.toByteArray();
        out.close();
        // outputStream.write(this.response);
    }
    protected byte[] response;

    /**
     *
     * Initializes license (if demo license is available).
     * you may insert your licensing code here that would use obj's
     * setLicense based on InputStream rather than passing it on
     * to 3rd-party class.
     *
     */
    public static void initLicense(Object obj) {
        try {

            Class myclass = Class.forName("com.aspose.demos.Common");
            if (null==myclass) {
                System.out.println("initLicense not found");
                return ;
            }

            //Use reflection to list methods and invoke them
            Method[] methods = myclass.getMethods();
            Object object = myclass.newInstance();

            Class partypes[] = new Class[1];
            partypes[0] = Object.class;
           
            Method meth = myclass.getMethod("initLicense", partypes);
            if (null!=meth) {

                    System.out.println("Calling " + meth.getName());
                    Object arglist[] = new Object[1];
                    arglist[0] = obj;

                    meth.invoke(object, arglist);
            }
        } catch (ClassNotFoundException cnfe) {
            // Ignore
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

XHTML

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ice="http://www.icesoft.com/icefaces/component"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jstl/core"
      >

<ui:composition template="/WEB-INF/includes/templates/page-template.xhtml">
                        
    <ui:define name="pageTitle">
        Change PDF Info - Aspose.PDF Demos
    </ui:define>

    <ui:define name="page-content">
        <ui:decorate template="/WEB-INF/includes/templates/tabbed_container.xhtml">
            <ui:define name="example">

    

<table width="90%" align="center" cellspacing="0" cellpadding="0" border="0">
  <tr>
    <td width="19"><img src="../../../../Common/images/heading_lft.jpg" alt="" width="19" height="41" /></td>
    <td width="100%" class="demos-heading-bg"><h2 class="demos-heading-bg"> &nbsp;Change PDF Metadata - Aspose.Pdf.Kit</h2></td>
    <td width="19"><img src="../../../../Common/images/heading_rt.jpg" alt="" width="19" height="41" /></td>
  </tr>
</table>

<p class="componentDescriptionTxt">
  This demo shows how to <b> Change Metadata information </b> of input Pdf document.    

Using <a href="http://www.aspose.com/documentation/java-components/aspose.pdf.kit-for-java/com/aspose/pdf/kit/pdffileinfo.html">  PdfFileInfo</a> class of Aspose.Pdf.Kit component, developers can change the meta information of the input PDF document.

<br/><br/>
Fore more information, please visit <a href="http://www.aspose.com/documentation/java-components/aspose.pdf.kit-for-java/change-pdf-information.html"> Change PDF Information. </a>

 <br/><br/> Enter PDF metadata and click <b> Execute Demo </b> below to see how demo takes <a href="../resources/Aspose.Pdf.Kit.pdf">input PDF file</a> 
        adds metadata information and sends the resulting document to user for review.
        </p>

            <table align="Left" border="1">
                <tr>
                    <th scope="col" width="96">Key</th>
                    <th scope="col" width="88">Value</th>
                </tr>
                <tr>
                    <td>
                        <em><b>T</b>itle</em>
                    </td>
                    <td>
                        <h:inputText  value="#{infochange.title}" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <em><b>A</b>uthor</em>
                    </td>
                    <td>
                        <h:inputText  value="#{infochange.author}" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <em><b>S</b>ubject</em>
                    </td>
                    <td>
                        <h:inputText  value="#{infochange.subject}" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <em><b>K</b>eywords</em>
                    </td>
                    <td>
                        <h:inputText  value="#{infochange.keywords}" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <em><b>C</b>reator</em>
                    </td>
    <td>                    
        <h:inputText  value="#{infochange.creator}" />
    </td></tr>
</table>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
                
            

            <h:commandButton id="generate" 
                action="#{infochange.generate}"
                value="Execute Demo"> 
            </h:commandButton>

            <ice:outputResource id="outputResource"
                mimeType="application/pdf"
                resource="#{infochange.generatedfile}"
                fileName="#{infochange.filename}"
                shared="false" />
            </ui:define>
        </ui:decorate>
    </ui:define>
</ui:composition>
</html>