I am building up a word document programatically and the final thing for me to do is to add a header which contains an image to the left a title in the middle.
I have tried various different things but nothing is working. I have tried adding a table to the header range (this throws an exception). I have tried adding the image and then the text but this just makes the text appear and not the image. I have tried adding new fields to the header range but this doesn't work neither.
Could someone point me in the correct direction?
//Add header into the document
foreach (Section section in document.Sections)
{
//Get the header range and add the header details.
Range headerRange = section.Headers
[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.InlineShapes.AddPicture(#"C:\Development\Tools\CommentParser\CommentParser\HeaderLogo.png");
headerRange.Font.ColorIndex = WdColorIndex.wdBlue;
headerRange.Font.Size = 10;
headerRange.Text = "Title";
}
I have found the solution to my problem. It is rather silly if you ask me but if you have headerRange.Text before the image then it will display them both and the text will be to the right of the image. If I have headerRange.Text second as it is in my sample text then it will just display the text.
Although I have solved it, if someone could please explain why this happens then it would be much appreciated.
Related
Am trying to achieve certain PDF automation with Acrobat SDK. So far, with the help of examples, I was able to update fillable PDF forms programmatically. However, I am now facing a scenario where I need to number these fillable fields sequentially.
This is easy in case of text fields which accept string values. However, I cannot do the same with checkboxes or radiobuttons etc as they are expecting certain predefined values to make them check/un-check.
ItextSharp has PdfCanvas class which has BeginText method write text on a rectangular bounding box.
Do we have something similar in Adobe?. I have the cordinates from GetRect() method
see code below:
CAcroApp acroApp = new AcroAppClass();
CAcroAVDoc avDoc = new AcroAVDocClass();
CAcroPDDoc pDDoc = new AcroPDDocClass();
CAcroPDPage pDPage;
CAcroPDAnnot pDAnnot;
avDoc.Open("Fillable PDF", "");
pDDoc = (CAcroPDDoc)avDoc.GetPDDoc();// gets PDDoc from AVDoc
pDPage = (CAcroPDPage)pDDoc.AcquirePage(0);// gets the 1st page
pDAnnot = (CAcroPDAnnot)pDPage.GetAnnot(8);// Index 8 is checkbox
var x = pDAnnot.GetRect();// holds the left,right,top,bottom value
Since I couldn't find anything similar for inserting text into checkbox, I was thinking of using an alternate approach where I remove the checkbox from the PDF using RemoveAnnot() method and adding a textbox at the same position.
pDPage = (CAcroPDPage)pDDoc.AcquirePage(0)
pDPage.RemoveAnnot(8);// this removes the fillable checkbox and leaves a square figure in PDF
Now, Any idea on how can I add a textbox in same position?
Or in general, Is it possible to change the field type from checkBox to textbox ?
There are little to no examples on "Creating Fillable forms programmatically with Acrobat SDK"
Any suggestion or guidance will greatly help me .
I dont know how achieve this, I have to create an informative report with images. For this, I use an excell template (It helps me with the format and with the position of the text where to place the respective information). The images are generated perfectly. I convert this XLS to PDF with the property
DocumentToMemoryStream (excelTemplate, XlsxSaveOptions.XlsxDefault);
This report generate blank pages in the PDF. How can I remove these blank pages, before the excel is generated?
Without checking your spreadsheet it's impossible to tell from where do those empty pages come from, so can you upload your XLS?
Anyway, do you perhaps have some explicitly defined horizontal or vertical page breaks?
If yes, can you remove the ones you don't need?
Or, do you perhaps have empty columns at the end which have some kind of styling or formatting?
If yes, then you could try using something like this to remove those empty columns from exporting to PDF:
var workbook = ExcelFile.Load("input.xls");
var worksheet = workbook.Worksheets.ActiveWorksheet;
worksheet.NamedRanges.SetPrintArea(
worksheet.GetUsedCellRange(true));
var options = new PdfSaveOptions();
options.SelectionType = SelectionType.ActiveSheet;
workbook.Save("output.pdf", options);
I had a counter of the rows of the document I was writing on, thanks to the help of #Mario Z, I use this function, before the save and solve my problem of blank pages
wsimgs.NamedRanges.SetPrintArea(wsimgs.Cells.GetSubrange("A1",CellRange.RowColumnToPosition (countRow, 10)));
I'm creating a PdfFlowDocument using Xfinium.PDF for UWP. I need to insert a page break but only if a certain section (containing paragraphs and tables) does not fit on the current page. It's a kind of "keep together" feature.
How could I accomplish my task?
UPDATE: I've minimized the amount of content that I need to keep together to just a single table. Although it is a flow table, I understand I will need to convert it to a fixed-sized table to calculate its height. If I could somehow find out the current position on the page after rendering all flow content, I would be able to calculate how much space left on the page. If there is enough space for the table, I would render it on the page. If not, I would issue a page break and render it on the next page.
Is there a feature to find out the current position on the page after rendering the flow content?
You can force a page break by calling PdfFlowDocument.StartNewPage.
Size information for flow content is not available in the public API, you cannot test in your code if a content object fits the current available space.
We'll support this in the next release.
Create a table with a single row and cell. For the cell use a PdfFlowTableCompositeCell. It has a property Content which is a collection of flow content objects. On the row set EnableRowSplit = false.
var table = new PdfFlowTableContent(1);
//column width in %
table.Columns[0].Width = 100;
var row = table.Rows.Add();
row.EnableRowSplit = false;
var cell = new PdfFlowTableCompositeCell();
//text
cell.Content.Add(new PdfFlowTextContent(new PdfFormattedContent(KswModuleTranslator.PdfFaultReportStationAssuranceText)));
//image
var image = new PdfPngImage(new MemoryStream(signatureData.Signature));
var imgDings = new PdfFlowImageContent(image);
cell.Content.Add(imgDings);
//text
cell.Content.Add(new PdfFlowTextContent(new PdfFormattedContent(signatureInfo.SigneeName)));
row.Cells.Add(cell);
document.AddContent(table);
I'm filling a table cell line by line with a richTextBox's content with this code :
int length = richTextBoxes[0].Lines.Length;
for (int index = 1; index < length; index++) {
table.Cell(3, 2).Range.Text += richTextBoxes[0].Lines[index].ToString();
}
But it automatically passes a weird line before pasting the text, and there is this huge spacing between paragraphs between each lines and I wonder if it's possible to remove it.
Thank you
To change the line spacing of the paragraphs in a text cell:
table.Cell(3,2).Range.Paragraphs.LineSpacing
If this does not have the desired effect you should make sure that line spacing is the problem and not some character code that Word isn't dealing with the way you expect. Type lines in the table cell, without pressing Enter and see if they exhibit the same behavior as you're seeing with your code. If not, then your code is importing something you'll need to "cut off"...
The problem you're seeing may be due to the style applied to the table. If that's the case, then it would be more efficient (and correct from the Word POV) to apply the correct style to the table/cells than to apply the formatting directly.
I believe you are using Microsoft.Office.Interop.Word assembly?
How about setting the property LineSpacing of Paragraph interface? It is a float type.
It can be set after LineSpacingRule property is set to one of the following values: wdLineSpaceAtLeast, wdLineSpaceExactly or wdLineSpaceMultiple.
I hope it helps.
I am creating a word document(2010) using c#.
It consists of a large table weith cells and within the cells paragraphs.
I Have achieved this with a little with (i hope simple) a little niggly issue. In front of every piece of text there is a block of empty space.
I am attempting to get rid of this space with no luck.
To build the table I have pretty much followed this example.
http://msdn.microsoft.com/en-us/library/office/gg490656.aspx
I have tried fiddling with the sacing of the cells, of the table rows and of the table cells but i'm unsure if i am using the right properties.
I have tried appending new ContextualSpacing() { Val = false } to table row and cells with no luck. I've also tried new Spacing with no luck either!
Any ideas hints tips would be muchly appreciated
I found that setting the style to a style within word resolved the issue. This was a manual process and then i looked in to how to do this programatically.
DocumentFormat.OpenXml.Wordprocessing.TableStyle tableStyle2 = new DocumentFormat.OpenXml.Wordprocessing.TableStyle() { Val = "TableGrid" };
Append this item to the table and it all worked perfectly!