Exception while writing header/footer to the aspose generated pdf file

Hi ,

I am getting the below stack trace when I am trying to write a header/foot the to the pdf document.


06:18:53,009 ERROR [stderr] (http–0.0.0.0-8080-4) java.lang.NullPointerException
06:18:53,015 ERROR [stderr] (http–0.0.0.0-8080-4) at com.aspose.pdf.Font.(Unknown Source)
06:18:53,021 ERROR [stderr] (http–0.0.0.0-8080-4) at com.aspose.pdf.internal.od.a.a(Unknown Source)
06:18:53,028 ERROR [stderr] (http–0.0.0.0-8080-4) at com.aspose.pdf.TextSegment.setText(Unknown Source)
06:18:53,035 ERROR [stderr] (http–0.0.0.0-8080-4) at com.aspose.pdf.TextFragment.setText(Unknown Source)
06:18:53,040 ERROR [stderr] (http–0.0.0.0-8080-4) at com.aspose.pdf.TextStamp.put(Unknown Source)
06:18:53,044 ERROR [stderr] (http–0.0.0.0-8080-4) at com.aspose.pdf.Page.addStamp(Unknown Source)
06:18:53,049 ERROR [stderr] (http–0.0.0.0-8080-4) at

I am attaching the sample file input text file (original8_tika_14_incompatible.rtf)
Note: This behaviour is true for all the input formats .rtf, .txt or .tiff.

1. I am converting the document to pdf document stream.

public ByteArrayInputStream convertToPdf(InputStream docInputStream) throws PDFConversionException {

// Save the document in PDF format.

ByteArrayOutputStream dstStream = null;

try {

final Document doc = new Document(docInputStream);

dstStream = new ByteArrayOutputStream();

doc.save(dstStream, SaveFormat.PDF);

}

catch (Exception e) {

s_Logger.error(" Error : "+e.getMessage()+" occured while processing file for pdf conversion");

throw new PDFConversionException("Invalid document model for processing");

}

final ByteArrayInputStream srcStream = new ByteArrayInputStream(dstStream.toByteArray());

return srcStream;

}

2. Then I am adding header and footer to the generated input stream.
private InputStream addHeaderFooter(final ByteArrayInputStream docInputStream, final DocumentModel pdfDocInfo) {

// open document

final com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(docInputStream);

// create formatted text stamp

final FormattedText headerText = new FormattedText(getPageHeader(pdfDocInfo));

final TextStamp headerStamp = getHeaderStyle(headerText);

final FormattedText footerText = new FormattedText(getPageFooter(pdfDocInfo));

final TextStamp footerStamp = getFooterStyle(footerText);

// iterate through all pages of PDF file

for (int Page_counter =1; Page_counter<=pdfDocument.getPages().size(); Page_counter++) {

//add stamp to all pages of PDF file

pdfDocument.getPages().get_Item(Page_counter).addStamp(headerStamp); This is the line throwing an Exception.

pdfDocument.getPages().get_Item(Page_counter).addStamp(footerStamp);

pdfDocument.getPages().get_Item(Page_counter).addStamp(

getPageNo(Page_counter, pdfDocument.getPages().size()));

}

// Getting the input stream of modified PDF document

final ByteArrayOutputStream dstStream = new ByteArrayOutputStream();

pdfDocument.save(dstStream, com.aspose.pdf.SaveFormat.Pdf);

final ByteArrayInputStream srcStream = new ByteArrayInputStream(dstStream.toByteArray());

return srcStream;

}

Where the helper function is to generate header/footer text is below.

private String getPageHeader(final DocumentModel pdfDocInfo) {

final String sPatientFristName = pdfDocInfo.getPatientFirstName();

final String sPatientLastName = pdfDocInfo.getPatientLastName();

final String sPatientMiddleName = pdfDocInfo.getPatientMiddleName();

final StringBuilder sb = new StringBuilder();

sb.append(sPatientLastName +", "+ sPatientFristName +" " +sPatientMiddleName.substring(0, 1).toUpperCase());

sb.append(" DOB:" +pdfDocInfo.getDateOfBirth());

sb.append(" GENDER: "+pdfDocInfo.getGender());

sb.append(" EMPI: "+pdfDocInfo.getEMPI());

return sb.toString();

}


And

private TextStamp getHeaderStyle(final FormattedText headerText) {

final TextStamp headerStamp = new TextStamp(headerText);

//set properties of the stamp

headerStamp.setTopMargin(10);

headerStamp.setLeftMargin(10.0);

headerStamp.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Left);

headerStamp.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Top);

//set text properties

headerStamp.getTextState().setFontSize(11.0F);

headerStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);

return headerStamp;

}


Environment is Jboss/Java 1.6
I am making the request through apache http client lib. below is the sample code.

FileBody fileBody = new FileBody(request.getDocument(), mimeType) ;
multipartEntity.addPart("document", fileBody);
multipartEntity.addPart("DateOfBirth", new StringBody(request.getDateOfBirth(), charSet));
multipartEntity.addPart("Gender", new StringBody(request.getDateOfBirth(), charSet));
multipartEntity.addPart("empi", new StringBody(request.getEMPI(), charSet));
String ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36";
httpPost.setHeader("User-Agent", ua);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Accept-Encoding", "gzip,deflate,sdch");
httpPost.setHeader("Accept-Language", "en-US,en;q=0.8");
httpPost.setHeader("Cache-Control", "no-cache");
httpPost.setHeader("Connection", "keep-alive");
multipartEntity.addPart("DateOfBirth", new StringBody(request.getDateOfBirth(), charSet));

multipartEntity.addPart("Gender", new StringBody(request.getGender(), charSet));

multipartEntity.addPart("empi", new StringBody(request.getEMPI(), charSet));

Please let me know.

Hi Kalpesh,


Thanks for contacting support.

We are working over this scenario and will get back to you soon. We are sorry for your inconvenience.

Hi We find out the root cause of the issue,

aspose is not able to write ‘㹕쁽⯊ᄩ ï ֚길ᐩ髒 and similar strings to the pdf as header/footer.

Can you please let us know what all char set aspose supports?

Hi Kalpesh,


Thanks for sharing the details.

I have tried replicating the issue using Aspose.Pdf for Java 9.1.0 where I have used the following code snippet (based on original code which you have shared) and I am unable to notice any exception. For your reference, I have also attached the resultant PDF generated over my end.

[Java]

com.aspose.pdf.Document
pdfDocument =
new com.aspose.pdf.Document(“c:/pdftest/original8_tika_14_incompatible.pdf”);<o:p></o:p>

//create formatted text stamp

com.aspose.pdf.facades.FormattedText headerText = new com.aspose.pdf.facades.FormattedText(getPageHeader(), java.awt.Color.red, "c:/Windows/Fonts/ARIALUNI.TTF", com.aspose.pdf.facades.EncodingType.Winansi, true, 14);

TextStamp headerStamp = getHeaderStyle(headerText);

com.aspose.pdf.facades.FormattedText footerText = new com.aspose.pdf.facades.FormattedText("getPageFooter(pdfDocInfo)");

//TextStamp footerStamp = getFooterStyle(footerText);

//iterate through all pages of PDF file

for (int Page_counter = 1; Page_counter <= pdfDocument.getPages().size(); Page_counter++)

{

//add stamp to all pages of PDF file

pdfDocument.getPages().get_Item(Page_counter).addStamp(headerStamp); //This is the line throwing an Exception.

//pdfDocument.getPages().get_Item(Page_counter).addStamp(footerStamp);

//pdfDocument.getPages().get_Item(Page_counter).addStamp(new com.aspose.pdf.facades.Stamp()) getPageNo(Page_counter, pdfDocument.getPages().size()));

}

pdfDocument.save("c:/pdftest/Stamped_original8_tika_14_incompatible.pdf", com.aspose.pdf.SaveFormat.Pdf);

static String getPageHeader() {

final String sPatientFristName = "First Name";

final String sPatientLastName = "Last Name";

final String sPatientMiddleName = "MiddleName";

final StringBuilder sb = new StringBuilder();

sb.append(sPatientLastName +", "+ sPatientFristName +" " +sPatientMiddleName.substring(0, 1).toUpperCase());

sb.append(" '㹕쁽⯊ᄩ ï ֚길ᐩé«' ");

sb.append(" GENDER: ");

sb.append(" EMPI: ");

return sb.toString();

}

static TextStamp getHeaderStyle(com.aspose.pdf.facades.FormattedText headerText) {

final TextStamp headerStamp = new TextStamp(headerText);

//set properties of the stamp

headerStamp.setTopMargin(10);

headerStamp.setLeftMargin(10.0);

headerStamp.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Left);

headerStamp.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Top);

//set text properties

headerStamp.getTextState().setFontSize(11.0F);

headerStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);

return headerStamp;

}